java Spring配置初始化方法
我怎样才能让Spring运行init方法呢?我需要获取代理异步类,并用它进行一些初始化
@Configuration
@EnableAsync
public class Config {
@Bean
public AsyncBean asyncProxyBean(){
return new AsyncBean();
}
public void init(){
doStuffWithProxy(asyncProxyBean());
}
@Bean
public String thisIsHack(){ //this runs the init code but bean is a bit hacky
doStuffWithProxy(asyncProxyBean());
return "";
}
}
# 1 楼答案
你可以用@PostConstruct来做这个
# 2 楼答案
BeanPostProcessor
# 3 楼答案
使用
@PostConstruct
注释以及:<context:annotation-config />
或<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
详见here。这是一个JavaEE注释,因此可能不适合您的环境