一、创建bean的三种方式
<!--第一种方式 使用构造函数创建 -->
使用bean 标签 配上 id class属性且没有其他属性时 采用的就是默认构造函数创建bean对象
此时类中如果没有默认构造函数则无法创建
<bean id="accountService" class="com.rpf.service.impl.AccountServiceImpl"></bean>
service实现类
public class AccountServiceImpl implements AccountService {
public void saveAccount(){
System.out.println("service 中的save执行了");
}
}
测试
//1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountService as = (IAccountService)ac.getBean("accountService");
as.saveAccount();
}

第二种方式:如果某个类在别人写的jar包中 无法通过修改源代码 使用默认构造函数时
<!--bean.xml 第二种方式 使用某个类中的方法创建对象 并存入spring容器-->
<bean id="InstanceFactory" class="com.rpf.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="InstanceFactory" factory-method="getAccountService"></bean>
创建一个工厂类模拟jar包中的代码
/**
* 模拟一个工厂类 该类可能是存在jar包中 无法修改源码来提供默认构造函数
* 这是一个新建的类
*/
public class InstanceFactory {
public AccountService getAccountService(){
return new AccountServiceImpl();
}
}
测试
//1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountService as = (AccountService)ac.getBean("accountService");
as.saveAccount();
}

第三种使用工厂中的静态方法
工厂中静态方法类
public class StaticFactory {
public static AccountService getAccountService(){
return new AccountServiceImpl();
}
}
bean.xml配置
<!--第三种方式 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象并存入spring容器) -->
<bean id="accountService" class="com.rpf.factory.StaticFactory" factory-method="getAccountService"></bean>
测试
//1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountService as = (AccountService)ac.getBean("accountService");
as.saveAccount();
}
二、bean的作用范围
bean的scope属性
作用 指定bean的作用范围
取值:singleton:单例(默认值) 每次获取的都是同一个对象 只会创建一个对象
prototype:多例的 每次从容器get的时候都会产生一个新对象
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时 就是session
<bean id="accountService" class="com.rpf.service.impl.AccountServiceImpl" scope="prototype"></bean>
测试多例模式 (原型模式)
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountService as1 = (AccountService)ac.getBean("accountService");
AccountService as2 = (AccountService)ac.getBean("accountService");
System.out.println(as1==as2);

三、bean的生命周期
在service实现类中加入两个测试方法
public void init(){
System.out.println("对象初始化了");
}
public void destory(){
System.out.println("对象销毁了");
}
bean中的配置
<!--bean对象的生命周期
单例对象
出生:当容器创建时对象出生
活着:只要容器还在对象一直活着
死亡:容器销毁对象消亡
总结:单例对象的生命周期和容器相同
多例对象
出生:当我们使用对象是spring为我们创建
活着:对象只要是在使用过程中就一直活着
死亡:当对象长时间不用,且没有别的对象引用时,又java垃圾回收机制回收 -->
<bean id="accountService" class="com.rpf.service.impl.AccountServiceImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>
测试单例生命周期 和容器相同
public static void main(String[] args) {
//1.获取核心容器对象
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountService as1 = (AccountService)ac.getBean("accountService");
//手动关闭容器
ac.close();
}
}

本文详细介绍了Spring Boot中bean的三种创建方式:构造函数、工厂方法和静态工厂,以及bean的作用范围(singleton、prototype)。实例演示了如何配置不同作用域,并探讨了单例和多例对象的生命周期。
1785





