建设工程自学网站,网站开发知识培训,品牌规划,广州建设工程交易中心增城电话前言 在之前的学习中我们知道#xff0c;容器是一个空间的概念#xff0c;一般理解为可盛放物体的地方。在Spring容器通常理解为BeanFactory或者ApplicationContext。我们知道spring的IOC容器能够帮我们创建对象#xff0c;对象交给spring管理之后我们就不用手动去new对象。…前言 在之前的学习中我们知道容器是一个空间的概念一般理解为可盛放物体的地方。在Spring容器通常理解为BeanFactory或者ApplicationContext。我们知道spring的IOC容器能够帮我们创建对象对象交给spring管理之后我们就不用手动去new对象。 那么Spring是如何管理Bean的呢 文章目录 前言一、概念二、创建Bean对象的三种方式2.1、使用默认构造函数创建方式2.1.1、定义Bean2.1.2、主配置文件中配置bean2.1.3、测试Bean2.1.4、注意点 2.2、使用工厂中的实例方法创建方式2.2.1、定义工厂2.2.2、定义Bean2.2.3、主配置文件中配置Bean2.2.4、测试 2.3、使用工厂中的静态方法创建方式2.3.1、定义工厂2.3.2、定义Bean2.3.3、主配置文件中配置Bean2.3.4、测试 三、Bean对象的作用域3.1、说明3.2、作用域类型3.3、注意细节3.4、如何修改Bean的作用域3.5、测试3.5.1、测试singleton单例3.5.2、测试prototype多例 四、Bean对象的生命周期4.1、单例对象4.1.1、说明4.1.2、测试4.1.2.1、定义Bean4.1.2.2、主配置文件中配置Bean4.1.2.3、测试4.1.2.4、测试结果 4.2、多例对象4.2.1、说明4.2.2、测试4.2.2.1、定义Bean4.2.2.2、主配置文件中配置Bean4.2.2.3、测试14.2.2.4、测试2 五、总结 一、概念
简而言之Spring bean是Spring框架在运行时管理的对象。Spring bean是任何Spring应用程序的基本构建块。你编写的大多数应用程序逻辑代码都将放在Spring bean中。
Spring bean的管理包括
创建一个对象提供依赖项例如其他bean配置属性拦截对象方法调用以提供额外的框架功能销毁一个对象
Spring bean是框架的基本概念。作为Spring的用户你应该对这个核心抽象有深刻的理解。 二、创建Bean对象的三种方式
2.1、使用默认构造函数创建方式
2.1.1、定义Bean
public class UserServiceImpl {}2.1.2、主配置文件中配置bean
!-- 方式一使用默认构造函数方式创建Bean --
beansbean iduserService classcn.bdqn.UserServiceImpl/bean
/beans2.1.3、测试Bean
Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);// 2、从容器中根据id获取对象(bean)UserServiceImpl userService (UserServiceImpl) ac.getBean(userService);// 3、打印beanSystem.out.println(userService);
}2.1.4、注意点
此种方式采用的就是通过默认构造函数的方式创建Bean假设我们给UserServiceImpl添加了一个带参的构造方法则运行会报错原因在于当我们为某个类自定义构造方法的时候Java编译器便不会为该类提供默认的不带参数的构造方法了。
2.2、使用工厂中的实例方法创建方式
2.2.1、定义工厂
// UserService的工厂作用是创建UserServiceBean对象
public class UserServiceImplFactory {public UserServiceImpl createUserService(){return new UserServiceImpl();}
}2.2.2、定义Bean
public class UserServiceImpl {}2.2.3、主配置文件中配置Bean
beans!-- 方式二使用工厂中提供的实例方法创建Bean --!-- 第一步把该工厂定义出来 --bean iduserServiceFactory classcn.bdqn.UserServiceImplFactory/!-- 第二步定义Bean(通过userServiceFactory中提供的实例方法)--bean iduserService factory-beanuserServiceFactory factory-methodcreateUserService/
/beans2.2.4、测试
Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);// 2、从容器中根据id获取对象(bean)UserServiceImpl userService (UserServiceImpl) ac.getBean(userService);// 3、打印beanSystem.out.println(userService);
}2.3、使用工厂中的静态方法创建方式
2.3.1、定义工厂
// UserService的工厂作用是创建UserServiceBean对象
public class UserServiceImplFactory {public static UserServiceImpl createUserService(){return new UserServiceImpl();}
}2.3.2、定义Bean
public class UserServiceImpl {}2.3.3、主配置文件中配置Bean
beans!-- 方式三使用工厂中提供的静态方法创建Bean --!-- 定义Bean(通过工厂类的静态方法创建) --bean iduserService classcn.bdqn.UserServiceImplFactory factory-methodcreateUserService/bean
/beans2.3.4、测试
Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);// 2、从容器中根据id获取对象(bean)UserServiceImpl userService (UserServiceImpl) ac.getBean(userService);// 3、打印beanSystem.out.println(userService);
}三、Bean对象的作用域
3.1、说明
Spring对Bean的默认的作用域(作用范围)是singleton【单例】
3.2、作用域类型 singleton单例的默认值只会new一次。 prototype多例的用到一次就会new一次。 request作用于web应用的请求范围Spring创建这个类之后将这个类存到request范围内。 session应用于web项目的会话范围Spring创建这个类之后将这个类存到session范围内。 global-session作用于集群环境的会话范围全局会话范围当不是集群环境时它就是session。
3.3、注意细节
实际开发中用得最多的就是singleton和prototype在整合struts2的时候使用prototype在整合SpringMVC的时候使用singleton。
3.4、如何修改Bean的作用域
bean标签的scope属性作用指定bean的作用范围。
3.5、测试
3.5.1、测试singleton单例
public class UserServiceImpl {}beansbean iduserService classcn.bdqn.UserServiceImpl /
/beansTest
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);// 2、从容器中根据id获取对象(bean)UserServiceImpl userService1 (UserServiceImpl) ac.getBean(userService);UserServiceImpl userService2 (UserServiceImpl) ac.getBean(userService);// 3、打印beanSystem.out.println(userService1 userService2); // true
}3.5.2、测试prototype多例
public class UserServiceImpl {}bean iduserService classcn.bdqn.UserServiceImpl scopeprototype/Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);// 2、从容器中根据id获取对象(bean)UserServiceImpl userService1 (UserServiceImpl) ac.getBean(userService);UserServiceImpl userService2 (UserServiceImpl) ac.getBean(userService);// 3、打印beanSystem.out.println(userService1 userService2); // false
}四、Bean对象的生命周期
4.1、单例对象
4.1.1、说明
出生当容器创建时对象出生 活着只要容器还在对象一直活着 死亡容器销毁对象消亡
4.1.2、测试
4.1.2.1、定义Bean
public class UserServiceImpl {public UserServiceImpl(){System.out.println(对象的构造方法执行了);}public void init(){System.out.println(对象初始化了);}public void destroy(){System.out.println(对象销毁了); }
}4.1.2.2、主配置文件中配置Bean
beansbean iduserService classcn.bdqn.UserServiceImplscopesingleton init-methodinit destroy-methoddestroy/
/beans4.1.2.3、测试
Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ClassPathXmlApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);ac.close();
}
// 结果对于单例对象来说只要容器创建了那么对象就创建了。类似于立即加载。4.1.2.4、测试结果
对象的构造方法执行了 对象初始化了 对象销毁了
总结单例对象的生命周期和容器相同
4.2、多例对象
4.2.1、说明
出生当我们使用对象时spring框架为我们创建 活着对象只要是在使用过程中就一直活着。 死亡当对象长时间不用且没有别的对象引用时由Java的垃圾回收器回收
4.2.2、测试
4.2.2.1、定义Bean
public class UserServiceImpl {public UserServiceImpl(){System.out.println(对象的构造方法执行了);}public void init(){System.out.println(对象初始化了);}public void destroy(){System.out.println(对象销毁了); }
}4.2.2.2、主配置文件中配置Bean
beansbean iduserService classcn.bdqn.UserServiceImplscopeprototype init-methodinit destroy-methoddestroy/
/beans4.2.2.3、测试1
Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ClassPathXmlApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);ac.close();
}
// 结果什么都不输出说明容器启动的时候对于多例对象来说并不会创建4.2.2.4、测试2
Test
public void testUserService() throws Exception{// 1、读取主配置文件信息获取核心容器对象ClassPathXmlApplicationContext ac new ClassPathXmlApplicationContext(beans.xml);UserServiceImpl userService (UserServiceImpl) ac.getBean(userService);System.out.println(userService);ac.close();
}
/**结果对象的构造方法执行了对象初始化了说明对于多例对象来说只有等到真正使用到该对象的时候才会创建。类似于懒加载。
**/ 对于多例的BeanSpring框架是不负责管理的 五、总结 以上就是本篇文章的全部内容了如果对你有帮助的话可以点个免费的关注如果能在下方三连一下就更好啦你的支持就是我更新的动力