- spring管理JavaBean的过程
- spring的JavaBean管理中单例模式及原型模式
1.spring管理JavaBean的过程
面试题:spring容器管理JavaBean的初始化过程(spring中bean的生命周期)
1.xml/annotation/confguation 配置JavaBean
2.BeanDefinitionReader解析配置的Javabean得到BeanDefinition,最终得到List<BeanDefinition>集合
3.触发BeanFactoryPostProcessor,javabean初始化之前执行自己的业务
4.spring中beanFactory,会通过List<BeanDefinition>集合遍历初始化所有的JavaBean对象
5.如果自己的JavaBean需要调动spring上下文中的资源,那么需要实现*Aware感知接口
6.如果自己的JavaBean已经初始化好了,还需要扩展功能,那么需要借助BeanPostProcessor来实现
package com.zlj.beanlife; import org.springframework.beans.factory.config.BeanDefinition; import java.util.ArrayList; import java.util.List; /** * @author zlj * @create 2023-08-18 13:59 */ public class Demo1 { public static void main(String[] args) throws ClassNotFoundException { List<BeanDefinition> beans=new ArrayList<>(); for (BeanDefinition bean : beans){ String beanClassName = bean.getBeanClassName(); Class.forName(beanClassName); } } } class User{ } class Person{ private int pid; private String pname; private String sex; private User user; public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Person(int pid, String pname, String sex) { this.init(); this.pid = pid; this.pname = pname; this.sex = sex; } public void init() { } }
package com.zlj.beanlife; /** * @author zlj * @create 2023-08-18 14:24 */ public class MyBaseDao extends BaseDao{ public MyBaseDao(){ super(); System.out.println("初始化。。。"); } }
2.spring的JavaBean管理中单例模式及原型模式
面试题:spring中JavaBean是单例的还是多例的。
1.默认是单例的,但是可以配置多例
2.单例的优点,节约内存,弊端,有变量污染
多列的优点:无遍历污染,缺点:极其消耗内存
3.单例:JavaBean是跟着spring上下文初始化的:容器生对象生,容器死对象死
多例:JavaBean是使用的时候才会创建,销毁跟着jvm走
4.以上观点并绝对的(第三点)
package com.zlj.beanlife; import java.util.List; public class ParamAction { private int age; private String name; private List<String> hobby; private int num = 1; // private UserBiz userBiz = new UserBizImpl1(); public ParamAction() { super(); } public ParamAction(int age, String name, List<String> hobby) { super(); this.age = age; this.name = name; this.hobby = hobby; } public void execute() { // userBiz.upload(); // userBiz = new UserBizImpl2(); System.out.println("this.num=" + this.num++); System.out.println(this.name); System.out.println(this.age); System.out.println(this.hobby); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" default-autowire="byType" 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的bean的生命周期--> <!-- <bean class="com.zlj.beanlife.ParamAction" id="paramAction"></bean>--> <bean id="paramAction" class="com.zlj.beanlife.ParamAction" scope="prototype"> <constructor-arg name="name" value="三丰"></constructor-arg> <constructor-arg name="age" value="21"></constructor-arg> <constructor-arg name="hobby"> <list> <value>抽烟</value> <value>烫头</value> <value>大保健</value> </list> </constructor-arg> </bean> <bean id="instanceFactory" class="com.zlj.beanlife.InstanceFactory" scope="prototype" init-method="init" destroy-method="destroy"></bean> </beans>
package com.zlj.beanlife; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /* * spring bean的生命週期 * spring bean的單例多例 */ public class Demo2 { // 体现单例与多例的区别 @Test public void test1() { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml"); // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml"); ParamAction p1 = (ParamAction) applicationContext.getBean("paramAction"); ParamAction p2 = (ParamAction) applicationContext.getBean("paramAction"); // System.out.println(p1==p2); p1.execute(); p2.execute(); // 单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁; applicationContext.close(); } // 体现单例与多例的初始化的时间点 instanceFactory @Test public void test2() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml"); InstanceFactory instanceFactory = (InstanceFactory) applicationContext.getBean("instanceFactory"); } // BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式 // 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化 @Test public void test3() { // ClassPathXmlApplicationContext applicationContext = new // ClassPathXmlApplicationContext("/spring-context.xml"); Resource resource = new ClassPathResource("/spring-context.xml"); BeanFactory beanFactory = new XmlBeanFactory(resource); // InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory"); } }