1、Spring 的对象创建(Bean)
-
必备jar包的导入
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies>
-
bean.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"> <!-- 把对象的创建交给spring管理 --> <bean id="userService" class="com.mypro.service.impl.UserServiceImpl"> </bean> <bean id="userDao" class="com.mypro.dao.impl.UserDaoImpl"> </bean> </beans>
-
bean对象的读取方式(常用的三个实现类读取)
-
ClassPathXmlApplicationContext: 它可以加载类路径下的配置文件,要求配置文件必须在类路径下
-
FileSystemXmlApplicationContext: 它可以加载磁盘下任意路径的配置文件(必须有访问权限)
-
AnnotationConfigApplicationContext: 它用于读取注解创建的容器
-
注意事项:核心容器的两个接口问题
-
ApplicationContext: 使用方式单例模式创建对象
-
它在构建核心容器时,创建对象采取的策略是立即加载,也就是说,只要一读取完配置文件, 马上就创建配置文件中配置的对象
-
-
BeanFactory: 使用方式多例模式创建对象
-
它在构建核心容器时,创建对象采取的策略时延迟加载,也就是说,什么时候根据id获取对象, 什么时候才是真正的创建对象
-
-
public class Client { public static void main(String[] args) { // 1、获取核心容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); ApplicationContext ac = new FileSystemXmlApplicationContext("C:\user\bean.xml"); // 2、根据id获取bean对象的两种方式 UserService userService = (UserService)ac.getBean("userService"); UserDao userDao = ac.getBean("userDao", UserDao.class); userService.saveUser(); } }
-
-
bean.xml配置文件的细节
-
创建bean的三种方式
-
第一种方式:使用默认构造函数创建。在spring的配置文件中使用bean标签,配置以id和class属性之后,且没有其他属性和标签;采用的就是默认构造函数创建bean对象,若类中没有默认构造函数,则创建失败
<?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"> <!-- 把对象的创建交给spring管理 --> <bean id="userService" class="com.mypro.service.impl.UserServiceImpl"> </bean> <bean id="userDao" class="com.mypro.dao.impl.UserDaoImpl"> </bean> </beans>
-
第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器中)。前提还需要有类似于工厂的类
package com.mypro.factory; import com.mypro.service.UserService; import com.mypro.service.impl.UserServiceImpl; import com.mypro.service.UserDao; import com.mypro.service.impl.UserDaoImpl; /** * 模拟一个工厂类(该类可能时存在于jar包中的类,我们无法通过修改源码的方式来提供默认构造函数) */ public class InstanceFactory { public UserService getUserService(){ return new UserServiceImpl(); } public UserDao getUserDao(){ return new UserDaoImpl(); } /** * 静态方法 */ public static UserService getUserServiceByStatic(){ return new UserServiceImpl(); } public static UseDao getUserDaoByStatic(){ return new UserDaoImpl(); } }
<?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"> <!-- 把对象的创建交给spring管理 --> <bean id="instanceFactory" class="com.mypro.factory.InstanceFactory"></bean> <bean id="userService" factory-bean="instanceFactory" factory-method="getUserService"></bean> <bean id="userService" factory-bean="instanceFactory" factory-method="getUserDao"></bean> </bean> </beans>
-
第三种方式:使用普通工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器中)
<?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"> <!-- 把对象的创建交给spring管理 --> <bean id="userService" class="com.mypro.factory.InstanceFactory" factory-method="getUserServiceByStatic"></bean> <bean id="userDao" class="com.mypro.factory.InstanceFactory" factory-method="getUserDaoByStatic"></bean> </beans>
-
-
bean的作用范围
-
bean标签的属性scope的取值如下:
-
singleton:单例的(默认值)
-
prototype:多例的
-
request:作用于web应用的请求范围
-
session:作用于web应用的会话范围
-
global-session:作用于集群环境的会话范围,若不是集群环境,就等同于session
-
-
-
bean的生命周期
-
单例对象
-
容器创建时出生,一直到容器销毁时,对象才消失
-
-
多例对象
-
使用对象时spring框架帮忙创建,对象长时间不使用,Java垃圾回收器回收
-
-
-