DD.
DD.

Reputation: 21981

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

}

Upvotes: 10

Views: 20134

Answers (3)

JustinKSU
JustinKSU

Reputation: 4989

You could use @PostConstruct to do this

Upvotes: 17

Dave Newton
Dave Newton

Reputation: 160191

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.

Upvotes: 4

Bozho
Bozho

Reputation: 597076

  • 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

Upvotes: 0

Related Questions