How to set up init-method for a bean when spring is configured by annotation driven?

44,073

Solution 1

Use @PostConstruct as shown in below example. It is equivalent to init-method="initialize()"

@PostConstruct
public void initialize() {
    messages.put("English", "Welcome");
    messages.put("Deutsch", "Willkommen");
}

Solution 2

@Bean(initMethod="init")
public MyBean getMyBean() {
 ...
}

Solution 3

In spring container it is "init" method that being called the last,
@postconstruct called before afterPropertiesSet. so it is safer if someone miss use. "Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, are called as follows:

1.Methods annotated with @PostConstruct

2.afterPropertiesSet() as defined by the InitializingBean callback interface

  1. A custom configured init() method [https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-java-lifecycle-callbacks][1]

Although, today I would prefer to be more Spring independent and use either @Postconstract, or even configure default init method recognition. to have only meaningful method name indicate that it should be used for initialization - clear from frameworks, clear from annotations.

Solution 4

As @Pangea has put in , @PostConstruct is the best choice. You could also implement initializingBean and do the initialization in afterPropertiesSet method .Check here for this approach.

Solution 5

I realized that there have been multiple answers trying to solve the problem. But with the newly introduced @Configuration which is popularly used within Spring Boot. Things are changed a little bit.

If you are using @Bean annotation in @Configuration annotated class like:

@Configuration
class FooClass {
    @Bean
    public Bar bar() {
        return new Bar();
    }
}

If you want to use an automatically called method on the bean instance during initialization, you have two options below:

Option1:

@Configuration
class FooClass {
    @Bean(initMethod="init")
    public Bar bar() {
        return new Bar();
    }
}

Option2:

@Configuration
class FooClass {
    @Bean
    public Bar bar() {
        Bar bar = new Bar();
        bar.init();
        return bar;
    }
}

BUT, as is explain in @Bean Java Doc here:

 /**

     * The optional name of a method to call on the bean instance during initialization.

     * Not commonly used, given that the method may be called programmatically directly

     * within the body of a Bean-annotated method.

     * The default value is {@code ""}, indicating no init method to be called.

     */

The second is regarded as a better answer. See link here

Share:
44,073
Charles
Author by

Charles

Updated on April 25, 2020

Comments

  • Charles
    Charles about 4 years

    I use spring roo to build project and it's annotation driven, and there is no bean definition in XML file. All the configuration info is in *.aj file.

    And now I want to set up an init method for a bean which don't have a default constructor (that bean is from the third party and it has a constructor with arguments, and I cannot remove them or give a default constructor to it.)

    Is there anyone who can tell me how to do this, please?

    The reason I want to do this is because I want to use applicationContext.getBean("thatBeanName") to dynamically get the bean and use it. Because the bean don't have a default constructor, I always get the error: java.lang.NoSuchMethodException: com.to.that.bean.<init>() and this is why I want to add the init-method to the bean.