how to set default beans init-method by annotations in spring 4?
配置文件实现的
<beans default-init-method="init">
<bean id="blogService" class="com.foo.DefaultBlogService">
</bean>
<bean id="anotherBean" class="com.foo.AnotherBean">
</bean>
</beans>
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