Reputation: 6179
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
Upvotes: 5
Views: 5143
Reputation: 3321
If I understand your question correctly, you want every bean to run its init method(if it has one) without declaring all of them in the configuration file. I think your own post has the answer already, it's default-init-method="init"
. In side the bean classes you want to initialize, implement a public void init()
method in each of them. They will all be called when the application starts up.
Upvotes: 0
Reputation: 63991
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
Upvotes: 6