繁体   English   中英

Spring配置初始化方法

[英]Spring Configuration Init Method

我如何告诉 Spring 运行该 init 方法? 我需要获取 Proxied Async 类并用它进行一些初始化。

@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 "";
 }

}

你可以使用@PostConstruct来做到这一点

使用@PostConstruct注释以及:

  • <context:annotation-config />
  • <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

有关详细信息,请参见此处 这是一个 Java EE 注释,因此可能不适合您的环境。

  • 通常你可以对原始对象做一些事情。 你很少需要用代理做事——这样你就依赖于一些 spring 内部结构(它与动态代理一起工作的方式)
  • 如果您真的需要代理,那么我想您可以尝试使用BeanPostProcessor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM