SpringBoot——Bean管理
文章目录
- Bean管理
- 一、获取Bean
- 二、 Bean的作用域
- 2.1 注意事项
- 三、第三方Bean
Bean管理
一、获取Bean
默认情况下,Spring项目启动时,会把Bean都创建好放在IOC容器中,如果主要获取这些Bean,可以通过如下方式:
- 根据name获取bean: Object getBean(String name)
- 根据类型获取bean: <T> T getBean(Class <T> requiredType)
- 根据name获取bean(带类型转换): <T> T getBean(String name,Class <T> requiredType)
// IOC容器对象@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testBean(){// TODO 根据bean名称获取 若没指定bean名称,默认类名首字母小写DeptController deptControllerBean1 =(DeptController) applicationContext.getBean("deptController");System.out.println(deptControllerBean1); //com.zhangjingqi.controller.DeptController@249b54af// TODO 根据bean的类型获取DeptController deptControllerBean2 = applicationContext.getBean(DeptController.class);System.out.println(deptControllerBean2);//com.zhangjingqi.controller.DeptController@249b54af// TODO 根据bean的名称 及 类型获取DeptController deptControllerBean3 = applicationContext.getBean("deptController",DeptController.class);System.out.println(deptControllerBean3);//com.zhangjingqi.controller.DeptController@249b54af}
上述所说的【Spring项目启动时,会把其中的bean创建好】还会受到作用域及延迟初始化影响,这里主要针对于默认的单例非延迟加载的bean而言。
二、 Bean的作用域
Spring支持五中作用域,后三种在Web环境下才生效:
作用域 | 说明 |
---|---|
singleton | 容器内同名称的bean只有一个实例(单例)(默认) |
prototype | 每次使用该bean时会创建新的实例(非单例) |
request | 每个请求范围内会创建新的实例(Web环境中,了解) |
session | 每个会话范围内会创建新的实例(Web环境中,了解) |
application | 每个应用范围内会创建新的实例(Web环境中,了解) |
singleton模式下bean对象情况,并且在容器启动的时候创建好的
@Test
public void testScope(){for (int i=0;i<10;i++){DeptController deptControllerBean2 = applicationContext.getBean(DeptController.class);System.out.println(deptControllerBean2);}
}
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
com.zhangjingqi.controller.DeptController@586728e8
我们也可以在第一次使用的时候实例化 @Lazy
代表延迟初始化,直到第一次使用的时候
@Lazy //
@RestController
@Slf4j
public class DeptController {...}
通过@Scope注解来进行配置作用域
@Scope("prototype")
@RestController
@Slf4j
public class DeptController {}
com.zhangjingqi.controller.DeptController@7f6b57f2
com.zhangjingqi.controller.DeptController@144ee8a7
com.zhangjingqi.controller.DeptController@52b32b70
com.zhangjingqi.controller.DeptController@18c820d2
com.zhangjingqi.controller.DeptController@3d3930fe
com.zhangjingqi.controller.DeptController@5e51ec2e
com.zhangjingqi.controller.DeptController@15f2a43f
com.zhangjingqi.controller.DeptController@4c65d8e3
com.zhangjingqi.controller.DeptController@382faf51
com.zhangjingqi.controller.DeptController@69ce14e6
2.1 注意事项
- 默认singleton的bean,在容器启动时被创建,可以使用@Lazy注解来延迟初始化(延迟到第一次使用时)
- prototype,每一次使用该bean的时候都会创建一个新的实例
- 实际开发当中,绝大部分的bean是单例的,也就是说绝大部分的bean不需要scope属性的
三、第三方Bean
项目开发中,自己开发的类使用@Component以及其三个衍生注解@Controller、@Service、@Repository注入即可
但是还有一种情况是第三方提供的,比如依赖
比如:dom4j就是第三方组织提供的。 dom4j中的SAXReader类就是第三方编写的。
如果我想将SAXReader对象存入到容器,需要在SAXReader类添加@Component注解,但是这是第三方bean,我们是无法修改的是不能添加注解的。
<dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.3</version></dependency>
如果要管理的bean对象来自于第三方(不是自定义的),是无法用@Component及衍生注解生命bean的,此时需要用到@Bean注解
启动类也是配置类,我们完全可以在这里进行注入
//Filter是javaweb三大组件之一,不是Spring提供的,如果想要使用三大组件,需要添加这个注解
@ServletComponentScan
@SpringBootApplication
public class SpringbootWebApplication {public static void main(String[] args) {SpringApplication.run(SpringbootWebApplication.class, args);}//声明第三方bean@Bean //将当前方法的返回值对象交给IOC容器管理, 成为IOC容器beanpublic SAXReader saxReader(){return new SAXReader();}
}
测试是否可以
创建xml文件,解析下面内容
<?xml version="1.0" encoding="UTF-8"?>
<emp><name>Tom</name><age>18</age>
</emp>
挺完美的
@Autowired
private SAXReader saxReader;
@Test
public void testBean2() throws DocumentException {Document document = saxReader.read(this.getClass().getClassLoader().getResource("1.xml"));Element rootElement = document.getRootElement();String name = rootElement.element("name").getText();String age = rootElement.element("age").getText();System.out.println(name + " : " + age); // Tom : 18
}
但是在Spring项目中,我们一般会保证启动类的纯粹性,让启动类仅仅是启动类,我们把其他的配置单独列出来
@Configuration //配置类 (在配置类当中对第三方bean进行集中的配置管理)
public class CommonConfig {//声明第三方bean@Bean //将当前方法的返回值对象交给IOC容器管理, 成为IOC容器bean
//通过@Bean注解的name/value属性指定bean名称, 如果未指定, 默认是方法名public SAXReader reader(DeptService deptService) {System.out.println(deptService);return new SAXReader();}
}
的返回值对象交给IOC容器管理, 成为IOC容器bean
//通过@Bean注解的name/value属性指定bean名称, 如果未指定, 默认是方法名public SAXReader reader(DeptService deptService) {System.out.println(deptService);return new SAXReader();}
}
SpringBoot——Bean管理相关推荐
- IOC操作Bean管理注解方式(完全注解开发)
IOC操作Bean管理注解方式(完全注解开发) (1)创建配置类,替代xml配置文件 需要让Spring 把一个普通的类认为是配置类 结构图: SpringConfig类代码如下: package c ...
- SpringBoot缓存管理(二) 整合Redis缓存实现
SpringBoot支持的缓存组件 觅波小说网 https://www.3812.info 在SpringBoot中,数据的缓存管理存储依赖于Spring框架中cache相关的org.springfr ...
- Spring——Bean管理-xml方式进行属性注入
目录 一.xml方式创建对象 二.xml方式注入属性 第①种方式注入:set方法注入 第②种方式注入:有参构造函数注入 constructor-arg:通过构造函数注入 用name标签属性: 不按照顺 ...
- springboot缓存管理器(CacheManager)
springboot缓存管理器(CacheManager)讲解--超详细!!! 一.引入 随着java的慢慢学习,缓存的使用也越来越多.我们使用缓存大多数是通过api的方式来操作,厉害的人也可以自己 ...
- 【一步一步学习spring】spring bean管理(上)
1. spring 工厂类 我们前边的demo中用到的spring 工厂类是ClassPathXmlApplicationContext,从上图可以看到他还有一个兄弟类FileSystemApplic ...
- spring项目属性注入和bean管理xml 注入一般属性和集合属性
IOC 介绍: 在Spring的应用中,Spring IoC容器可以创建.装配和配置应用组件对象,这里的组件对象称为Bean. Bean的实例化 在面向对象编程中,想使用某个对象时,需要事先实例化该对 ...
- SpringBoot —— Bean的注入方式
文章目录 1.组件注解 2.@Component + @Bean 3.@Import(PlaceHolderClass)快速导入一个组件 4.使用Spring提供的FactoryBean注入 1.组件 ...
- IOC操作Bean管理注解方式(注入属性@Autowired和Qualifier)
目录 IOC操作Bean管理注解方式(注入属性@Autowired.@Qualifier和@Resource) 1.基于注解方式实现 属性注入 (1)@Autowired:根据属性类型进行自动装配 第 ...
- IOC操作Bean管理注解方式(组件扫描配置)
IOC操作Bean管理注解方式(组件扫描配置) 开启组件扫描的 细节配置 约定那些类可以扫描,哪些类不可以扫描 bean1.xml配置如下: <?xml version="1.0&qu ...
最新文章
- 域名解析对网站SEO优化有何影响?
- 如何让VB6代码编辑器垂直滚动条随鼠标滚轮滚动
- 超重磅 | 神策数据“两云一平台”上线,拥抱数字化经营
- python代数式的表达方式_关于python字典类型最疯狂的表达方式
- python import如何使用_python之import引用
- (9)Verilog inout使用方法(FPGA不积跬步101)
- 数据结构上机实践第八周项目9-广义表算法库及应用
- 数据源:SHP数据下载平台
- EPUB格式怎么转成MOBI格式
- 2019通信工程师最新的职业发展方向
- excel输入公式显示公式_显示Excel公式而不是结果
- Windows系统蓝屏代码分析
- Activity工作流(2)-入门安装运行第一个例子
- python pil grab screen
- OpenCV打开摄像头并显示图像(C++、Python)
- BOSS直聘直直4周岁生日会圆满举办
- CTF学习笔记:misc
- PYQT5安装时,labelImg执行pyrcc5 -o libs/resources.py resources.qrc 报错:File does not exist ‘resources.qrc‘
- 【密码学】DES加解密原理及其Java实现算法
- python缺少位置参数_缺少1个必需的位置参数
热门文章
- python中去掉字符串中的\xa0、\t、\n
- 早起的十个好处,以及如何做到早起
- [python] 基于matplotlib实现圆环图的绘制
- spring cloud gateway监听nacos服务上下线,刷新路由,防止404
- java-----权限修饰符,重写,super和final关键字,多态,转型和抽象类
- Razor、ASPX 语法分析
- 2022内蒙古最新水利水电施工安全员模拟考试试题及答案
- 8小时8节零基础MySQL快速人门实验上机课
- 电脑 PC 端如何打开企业微信后台
- pubg为什么服务器一直在维护中,pubg服务器维护中