(4)Spring中Bean的五大作用域及其生命周期 【详解】
实例化Bean的四种方式
applicationContext.xml
<?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">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="userDao3" class="cn.itcast.spring.UserDaoImpl"/>
<bean id="userDao4" class="cn.itcast.spring.UserDaoImpl"/>
<bean id="userService" class="cn.itcast.spring.UserServiceImpl">
</bean>
<bean id="bean1" class="cn.itcast.spring.bean.Bean1"/>
<bean id = "bean2" class="cn.itcast.spring.bean.Bean2Factory" factory-method="getBean2"/>
<bean id="bean3Factory" class="cn.itcast.spring.bean.Bean3Factory"/>
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"/>
<bean id="bean4" class="cn.itcast.spring.bean.Bean4Factory"/>
<bean id="singletonBean" class="cn.itcast.spring.SingletonBean"/>
<bean id="prototypeBean" class="cn.itcast.spring.PrototypeBean" scope="prototype"/>
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
四种方式: 第一种:最常用 第二、第三种:一些框架初始化的时候用的多。 第四种:spring底层实现的比较多
SpringTest.java
package cn.itcast.spring;
import cn.itcast.spring.bean.Bean1;
import cn.itcast.spring.bean.Bean2;
import cn.itcast.spring.bean.Bean3;
import cn.itcast.spring.bean.Bean4;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean1 bean1 = (Bean1) ac.getBean("bean1");
System.out.println("bean1 = " + bean1);
ApplicationContext ac2 = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean2 bean2 = (Bean2) ac2.getBean("bean2");
System.out.println("bean2 = " + bean2);
ApplicationContext ac3 = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean3 bean3=(Bean3) ac3.getBean("bean3");
System.out.println("bean3 = " + bean3);
ApplicationContext ac4 = new ClassPathXmlApplicationContext("applicationContext.xml");
Bean4 bean4=(Bean4) ac4.getBean("bean4");
System.out.println("bean4 = " + bean4);
}
}
Bean的作用域
项目开发中通常会使用:singleton 单例、 prototype多例 Singleton: 在一个spring容器中,对象只有一个实例。(默认值) Prototype: 在一个spring容器中,存在多个实例,每次getBean 返回一个新的实例。
【注意】 单例是默认值,如果需要单例对象,则不需要配置scope。
SpringBeanTest.java
package cn.itcast.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringBeanTest {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
SingletonBean singletonBean1 = (SingletonBean) applicationContext.getBean("singletonBean");
SingletonBean singletonBean2 = (SingletonBean) applicationContext.getBean("singletonBean");
System.out.println("singletonBean1 = " + singletonBean1);
System.out.println("singletonBean2 = " + singletonBean2);
PrototypeBean prototypeBean1 = (PrototypeBean) applicationContext.getBean("prototypeBean");
PrototypeBean prototypeBean2 = (PrototypeBean) applicationContext.getBean("prototypeBean");
System.out.println("prototypeBean1 = " + prototypeBean1);
System.out.println("prototypeBean2 = " + prototypeBean2);
}
}
Bean的生命周期
通过spring工厂,可以控制bean的生命周期。 在xml配置Bean的初始化和销毁方法
通过 init-method属性 指定实例化后的调用方法 通过 destroy-method属性 指定销毁对象前的方法
LifeCycleBean.java
package cn.itcast.spring;
public class LifeCycleBean {
public LifeCycleBean() {
System.out.println("LifeCycleBean构造器调用了");
}
public void init(){
System.out.println("LifeCycleBean-init初始化时调用");
}
public void destroy(){
System.out.println("LifeCycleBean-destroy销毁时调用");
}
}
applicationContext.xml
<bean id="lifeCycleBean" class="cn.itcast.spring.LifeCycleBean" init-method="init" destroy-method="destroy" />
SpringBeanLife.java
package cn.itcast.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringBeanLife {
@Test
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
LifeCycleBean lifeCycleBean=(LifeCycleBean) applicationContext.getBean("lifeCycleBean");
System.out.println("lifeCycleBean = " + lifeCycleBean);
((ClassPathXmlApplicationContext) applicationContext).close();
}
}
|