When many beans in a context definition will have the same initialization and destroy method name, you can define a default-init-method
and default-destroy-method
in the attributes of the beans
element.
This way you don’t have to define individual init-method
and destroy-method
for each of your beans. When the beans do not supply the method that match the name of defined default-init-method
or default-destroy-method
nothing will happen to those beans.
Let’s see an example code below. Firs here is our simple bean, the AutoEngine
bean.
package org.kodejava.spring.core;
public class AutoEngine {
public void initialize() {
System.out.println("AutoEngine.initialize");
}
public void destroy() {
System.out.println("AutoEngine.destroy");
}
}
And then we define our Spring configuration file. You can see that there are default-init-method
and default-destroy-method
in the attribute of the beans
element.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
default-init-method="initialize" default-destroy-method="destroy">
<bean id="engine" class="org.kodejava.spring.core.AutoEngine" />
</beans>
And finally a small program to run our demo.
package org.kodejava.spring.core;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DefaultInitDestroyDemo {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("default-init-destroy.xml");
AutoEngine engine = (AutoEngine) context.getBean("engine");
// context.close will remove the bean from the container.
// This will call our bean destroy method.
context.close();
}
}
When you run this example you’ll get the following output printed on your screen:
AutoEngine.initialize
AutoEngine.destroy
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024