Spring Configuration Method

How can I tell Spring to run this 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 ""; } } 
+6
source share
3 answers

You can use @PostConstruct

+10
source

Use the @PostConstruct annotation together with:

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

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

+4
source
  • You can usually do things with the original object. You rarely have to do something with a proxy server - this way you rely on some of the internal elements of spring (how it works with dynamic proxies)
  • If you really need a proxy server, then I think you can try using BeanPostProcessor
0
source

Source: https://habr.com/ru/post/905146/


All Articles