Introduction to Spring Lecture 3 - Configuration and Management of Bean s in Spring

Keywords: Spring Attribute xml Eclipse

stay Getting Started with Spring: A Quick Start to the Spring Framework In this lecture, we have started Spring quickly. Now let's start with the first lecture and explain the configuration and management of Bean s in Spring, but it's all based on XML files.Warm Tip: If beginners are learning the framework of Spring for the first time, it is still advisable to start with Spring, so beginners can see the first introduction to Spring that I wrote, and some of the code I wrote in this lecture is based on the first one. When beginners suddenly see this, they will feel very unfit.Of.

Bean Configuration

Configuration of id and name attributes in <bean>tag

From the Spring configuration file we have written before, we can see that there is an id attribute in the <bean>tag, but it also has a name attribute similar to the id attribute, from which we can get the configuration object.

  • ID attribute: There are multiple bean tags in the Spring configuration file, but their ID attribute values cannot be the same.When a Bean is named, an ID constraint (unique constraint) is used in the constraint, and the name must start with a letter. Letters, numbers, hyphens, underscores, periods, colons, and so on can be used, but the value of the ID attribute must not have special symbols.
  • Name attribute: Without the unique constraint in the constraint, the value of the name attribute can theoretically be duplicated, but it is not possible in actual development, and special characters can appear in it.In fact, this is only possible when Spring and Struts1 frameworks are integrated, for example:

Configuration of Bean's life cycle

By configuring init-method on the <bean> tag as the method to execute when the beans are initialized, destroy-method as the method to execute when the beans are destroyed, if you want to execute the method configured in the destroy-method attribute when the beans are destroyed, the beans must be created singly (the default is the singleton creation), andThe methods configured in the destroy-method property can only be executed if the factory is closed.Here's a case to show you how Bean's life cycle is.
First, create a com.meimeixia.spring.demo02 package in the src directory and create an interface named CustomerDao under that package.

package com.meimeixia.spring.demo02;

public interface CustomerDao {
	
	public void save();

}

Then, an implementation class for the CustomerDao interface, CustomerDaoImpl.java, is created under the package com.meimeixia.spring.demo02.

package com.meimeixia.spring.demo02;

public class CustomerDaoImpl implements CustomerDao {
	
	public void setup() {
		System.out.println("CustomerDaoImpl Initialized......");
	}

	@Override
	public void save() {
		System.out.println("CustomerDaoImpl In save Method Executed......");	
	}
	
	/*
	 * This method will not be called until the factory is closed
	 */
	public void destroy() {
		System.out.println("CustomerDaoImpl Destroyed......");
	}

}

Next, the CustomerDaoImpl implementation class is handed over to Spring for management, which is the creation of configuration objects in the configuration file.

<?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">
        
	<!-- Spring Getting Started Configuration -->
	<bean id="userDao" class="com.meimeixia.spring.demo01.UserDaoImpl">
		<!-- DI: Dependent Injection -->
		<property name="name" value="Li Er" />
	</bean>
	
	<!-- Spring Of bean Configuration of life cycle -->
	<bean id="customerDao" class="com.meimeixia.spring.demo02.CustomerDaoImpl" init-method="setup" destroy-method="destroy" />
	
</beans>

Next, create a SpringDemo02 unit test class under the package com.meimeixia.spring.demo02.

package com.meimeixia.spring.demo02;

import org.junit.Test;
import org.springframework.context.ApplicationContext;

public class SpringDemo02 {

	/*
	 * Lifecycle configuration
	 */
	@Test
	public void demo01() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
		customerDao.save();
	}
	
}

Finally, by running the demo01 unit test method above, the Eclipse console prints the following.

Hey!How can the destroy method configured in the destroy-method property not be executed?This is because our factory class has not been closed, how should the factory class be closed?You can call the close method in this implementation class of the ClassPathXmlApplicationContext of the ApplicationContext interface.Therefore, the demo01 test method in the SpringDemo02 unit test class should be modified to look like the following.

package com.meimeixia.spring.demo02;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo02 {

	/*
	 * Lifecycle configuration
	 */
	@Test
	public void demo01() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");
		customerDao.save();
		applicationContext.close();//At this point, the CustomerDaoImpl class is created singly, and when the factory class is closed, the methods configured in the destroy-method property can be executed. 
	}
	
}

Run the demo01 unit test method above and the Eclipse console prints the following.

As I said above, to execute the method configured in the destroy-method property when a Bean is destroyed, a Bean must be created singleton by default, as shown below.

If the Bean is created in multiple-case mode instead, as shown below.

Then when the factory class is closed, the methods configured in the destroy-method property will not be executed.You can try running the demo01 unit test method and you will find that the Eclipse console prints the following.

Configuration of Bean Scope

. . .

Posted by Eggzorcist on Sat, 31 Aug 2019 18:05:56 -0700