簡體   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