Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring init-method params

Tags:

spring

I am new to spring and I wanted to ask whether or not it is possible to pass params to the init and destroy methods of a bean.

Thanks.

like image 686
Radu Avatar asked Sep 13 '11 11:09

Radu


People also ask

How can we specify Init method for spring bean?

To define setup and teardown for a bean, we simply declare the <bean> with initmethod and/or destroy-method parameters. The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation.

What is @bean destroyMethod close?

destroyMethod. The optional name of a method to call on the bean instance upon closing the application context, for example a close() method on a JDBC DataSource implementation, or a Hibernate SessionFactory object.

How do you execute a method after all beans are initialized?

One of the ways to run your code right after a Bean has been initialized is to use @PostConstract annotation. In the below code example the class MyBean is annotated with @Component annotation. This Bean will be created at application startup time. Note the use of @PostConstruct annotation.

What is init destroy?

Just like applets, servlets can define init() and destroy() methods. A servlet's init(ServletConfig) method is called by the server immediately after the server constructs the servlet's instance. Depending on the server and its configuration, this can be at any of these times: When the server starts.


1 Answers

No, you can't. If you need parameters, you will have to inject them as fields beforehand.

Sample Bean

public class Foo{

    @Autowired
    private Bar bar;

    public void init(){
        bar.doSomething();
    }

}

Sample XML:

<bean class="Foo" init-method="init" />
like image 178
Sean Patrick Floyd Avatar answered Sep 16 '22 14:09

Sean Patrick Floyd