简体   繁体   中英

Spring Configuration Init Method

How can I tell Spring to run that init method? I need to get the Proxied Async class and do some initialization with it.

@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来做到这一点

Use the @PostConstruct annotation along with:

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

See here for details. This is a Java EE annotation, so may not be appropriate in your environment.

  • usually you can do the things to the original object. You rarely need to do things with the proxy - that way you rely on some spring internals (the way it works with dynamic proxies)
  • if you really need the proxy, then I guess you can try using a BeanPostProcessor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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