- 在spring里面,设置创建bean 实例是单例还是多实例
- 在spring里面,默认情况下,bean是一个单实例对象
- 如何设置单实例还是多实例
- 在spring配置文件中设置是单实例还是多实例
- scope 属性值
- scope="prototype" 多实例
- scope="singleton" 单实例
- singleton和prototype区别
- 第一,singleton 单实例,prototype多实例
-
-
设置 scope 值是singleton时候,加载spring配置文件时候创建对象,在调用getBean 方法时候创建多实例对象
-
IOC 操作Bean管理 (Bean的生命周期)
1. 生命周期
从对象创建到对象销毁的过程
2. Bean的生命周期
- 通过构造创建 bean 实例(无参数构造)
- 为 bean 的属性设置值和对其他bean的引用(调用set方法)
- 调用bean的初始化的方法(需要进行配置)
- bean 可以使用了(对象可以取到了)
- 当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
对象
package com.study.modules.life.entity;
/**
* Order.
*
* @author lipw
* @since 2021/12/1 16:16
*/
public class Order {
private String name;
public Order() {
System.out.println("第一步,执行无参数构造函数创建bean");
}
public void setName(String name) {
this.name = name;
System.out.println("第二部,调用set方法,为bean的属性设置值和对其他bean的引用");
}
public void initMethod() {
System.out.println("第三部,执行初始化方法");
}
public void destoryMethod() {
System.out.println("第五步,销毁的方法");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="order" class="com.study.modules.life.entity.Order"
init-method="initMethod" destroy-method="destoryMethod">
<property name="name" value="订单"></property>
</bean>
</beans>
测试方法:
@Test
public void test8() {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("bean8.xml");
Order order = applicationContext.getBean("order", Order.class);
System.out.println("第四部获取bean实例对象" + order);
// 手动让bean实例销毁
applicationContext.close();
}
3. 演示添加后置处理器效果
创建类,实现接口 BeanPostProcessor,创建后置处理器。
package com.study.modules.life.entity;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* MyBeanPost.
*
* @author lipw
* @since 2021/12/1 16:46
*/
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
配置文件修改
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="order" class="com.study.modules.life.entity.Order"
init-method="initMethod" destroy-method="destoryMethod">
<property name="name" value="订单"></property>
</bean>
<bean id="myBeanPost" class="com.study.modules.life.entity.MyBeanPost">
</bean>
</beans>
运行结果: