how to set default beans init-method by annotations in spring 4?
annotations, dependency-injection, java, spring, spring-4
Solution
You could do the following:
@Configuration
public class SomeConfig {
@Bean(initMethod = "initMethodName")
public SomeBeanClass someBeanClass() {
return new SomeBeanClass();
}
}
You would repeat that for every bean you want to call `initMethodName` on.
You could take it one step further and implement a meta-annotation like
@Bean(initMethod = "initMethodNAme")
public @interface MyBean {
}
and simply use `@MyBean` instead of `@Bean(initMethod = "initMethodName")` in `SomeConfig`
Problem
i am learning using Spring 4 by Java annotations, and i could not find how to set default init-method to all beans that belong to specific configuration, without adding the @PostContruct annotation to initialize method at all clases and neither making them implement the InitializeBean interface... I just want to do something like this: ``` <beans default-init-method="init"> <bean id="blogService" class="com.foo.DefaultBlogService"> </bean> <bean id="anotherBean" class="com.foo.AnotherBean"> </bean> </beans> ``` So, i want to do exactly this by Java annotations, i want to set default beans configurations on bean's configuration container. Is that possible? Regards EDIT: What i actually want to do is tell spring to run the "initialize" method by default on all beans that i create inside a BeansConfigurations class. It means, put some annotation or something that establish that all contained beans will run this initialize method by default. But as i said before, i don't want to touch beans classes, i mean, i don't want to add @PostConstructor annotation to every initialize method for each bean class and i don't want every bean to implements the InitializeBean interface either