7.6Java EE - Bean life cycle

Bean life cycle in different scopes

        Bean life cycle refers to the process of Bean instance being created, initialized and destroyed. In the two scopes singleton and prototype of Bean, the Spring container manages the life cycle of Bean differently. In the singleton scope, the Spring container can manage the life cycle of the Bean, controlling the creation, initialization and destruction of the Bean. In the prototype scope, the Spring container is only responsible for creating Bean instances and will not manage their life cycles.

Two time nodes of the Bean life cycle

        In the life cycle of a bean, two time nodes are particularly important. These two time nodes are after the Bean instance is initialized and before the Bean instance is destroyed. Some specified operations usually need to be completed at these two time nodes. Therefore, it is often necessary to monitor these two nodes.

Ways to monitor time nodes

There are two ways to monitor two nodes, one is to use XML configuration files, and the other is to use annotations. 

        The Spring container provides @PostConstruct for monitoring Bean object initialization nodes, and @PreDestroy for monitoring Bean object destruction nodes. The following example demonstrates the use of these two annotations. 

1. Create a Student class, define the id and name fields in the class, and use @PostConstruct to specify the initialization method, and use @PreDestroy to specify the method before the Bean is destroyed.

@Component("student")
public class Student {
    @Value("1")
    private String id;
    @Value("张三")
    private String name; 	// 省略getter/setter方法,以及toString()方法
    @PostConstruct
    public void init(){System.out.println("Bean的初始化完成,调用init()方法");	}
    @PreDestroy
    public void destroy(){System.out.println("Bean销毁前调用destroy()方法");		}}

2. Create applicationStudent.xml, introduce Context constraints in this file and start the automatic scanning function of Bean.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 使用context命名空间,在配置文件中开启相应的注解处理器 -->
    <context:component-scan base-package="com.mac" />
</beans>

 3. Create a test class StudentTest, write the test code in this class, load the configuration file through the Spring container and obtain the Student instance.

public class StudentTest {
    public static void main(String[] args){
    ApplicationContext applicationContext=new 
            ClassPathXmlApplicationContext("applicationStudent.xml");
    Student student=(Student)applicationContext.getBean("student");
    System.out.println(student);
    //销毁Spring容器中的所有Bean
    AbstractApplicationContext ac=(AbstractApplicationContext)
            applicationContext;
    ac.registerShutdownHook();
}}

 4. Start the StudentTest class in IDEA, and the console will output the results.

         

Supongo que te gusta

Origin blog.csdn.net/W_Fe5/article/details/131805564
Recomendado
Clasificación