学习SpringBoot容器功能及注解原理
作者:LL.LEBRON 发布时间:2023-11-24 22:06:17
1.组件添加
1.1@Configuration
@Configuration:告诉SpringBoot这是一个配置类
配置类里面使用@Bean
标注在方法上给容器注册组件,默认也是单实例的
配置类本身也是组件
proxyBeanMethods
:代理bean的方法
Full(
proxyBeanMethods = true
):保证每个@Bean方法被调用多少次返回的组件都是单实例的Lite(
proxyBeanMethods = false
):每个@Bean方法被调用多少次返回的组件都是新创建的组件依赖必须使用Full模式默认。其他默认是否Lite模式
最佳实战:
1.配置类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
2.配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
代码实战演示:
@Configuration(proxyBeanMethods = false)//告诉SpringBoot这是一个配置类=配置文件
public class MyConfig {
@Bean//给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01() {
User zhangsan = new User("zhangsan", 18);
return zhangsan;
}
@Bean("tom")//也可以自己设置id代替方法名作为id
public Pet tomcatPet() {
return new Pet("tomcat");
}
}
@SpringBootApplication
public class Boot01HelloworldApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);//com.atguigu.boot.config.MyConfig@d67d8
//如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
//保持组件单实例
User user = bean.user01();
User user1 = bean.user01();
//(proxyBeanMethods = true)返回true
//(proxyBeanMethods = false)返回false
System.out.println(user == user1);
}
}
如果有组件依赖:
@Configuration(proxyBeanMethods = true)//告诉SpringBoot这是一个配置类=配置文件
public class MyConfig {
@Bean//给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01() {
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean("tom")//也可以自己设置id代替方法名作为id
public Pet tomcatPet() {
return new Pet("tomcat");
}
}
@SpringBootApplication
public class Boot01HelloworldApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
MyConfig bean = run.getBean(MyConfig.class);
System.out.println(bean);
User user01 = run.getBean("user01", User.class);
Pet tom = run.getBean("tom", Pet.class);
//(proxyBeanMethods = true)返回(用户的宠物:true)
//(proxyBeanMethods = false)返回(用户的宠物:false)
System.out.println("用户的宠物:"+(user01.getPet() == tom));
}
}
1.2@Import
@Import:给容器中导入组件
代码演示:
//给容器中自动无参构造创建出这两个类型的组件、默认组件的名字就是全类名
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
}
@SpringBootApplication
public class Boot01HelloworldApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
//获取组件
String[] beanNamesForType = run.getBeanNamesForType(User.class);
for (String s : beanNamesForType) {
System.out.println(s);
}
DBHelper bean = run.getBean(DBHelper.class);
System.out.println(bean);
}
}
//输出:
com.atguigu.boot.bean.User
ch.qos.logback.core.db.DBHelper@16ef799
1.3@Conditional
@Conditional:条件装配,满足Conditional
指定的条件,则进行组件注入
有一系列派生注解:
2.原生配置文件引入
2.1@ImportResource
原生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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="haha" class="com.atguigu.boot.bean.User">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
</bean>
<bean id="hehe" class="com.atguigu.boot.bean.Pet">
<property name="name" value="tomcat"></property>
</bean>
</beans>
自定义配置类:
@Configuration(proxyBeanMethods = true)//告诉SpringBoot这是一个配置类=配置文件
@ImportResource("classpath:beans.xml")
public class MyConfig {
}
测试:
@SpringBootApplication
public class Boot01HelloworldApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Boot01HelloworldApplication.class, args);
boolean haha = run.containsBean("haha");
boolean hehe = run.containsBean("hehe");
System.out.println(haha);//true
System.out.println(hehe);//true
}
}
3.配置绑定
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用;
原生方法(配置文件复杂就显得麻烦):
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean。
}
}
}
3.1@ConfigurationProperties
配置文件:
mycar.brand=BYD
mycar.price=100000
创建一个car类:
//只有在容器中的组件,才会拥有SpringBoot提供的强大功能
@Component
@ConfigurationProperties(prefix = "mycar")
//Lombok注解简化开发
@Data
@NoArgsConstructor
@ToString
@AllArgsConstructor
public class Car {
private String brand;
private Integer price;
}
测试方法:
@RestController
public class HelloController {
@Autowired
Car car;
@RequestMapping("/car")
public Car car(){
return car;
}
}
测试结果:
3.2@EnableConfigurationProperties + @ConfigurationProperties
@EnableConfigurationProperties
必须在配置类里写:
@Configuration(proxyBeanMethods = true)//告诉SpringBoot这是一个配置类=配置文件
@EnableConfigurationProperties(Car.class)
//1.开启Car属性配置绑定功能
//2.把Car这个组件自动注册到容器中
public class MyConfig {
}
该写法就不用在写@Component
@ConfigurationProperties(prefix = "mycar")
@Data
@NoArgsConstructor
@ToString
@AllArgsConstructor
public class Car {
private String brand;
private Integer price;
}
来源:https://blog.csdn.net/qq_45966440/article/details/120412878


猜你喜欢
- 本文实例讲述了C#简单获取全屏中鼠标焦点位置坐标的方法。分享给大家供大家参考,具体如下:using System;using System.
- 一、什么是内存泄漏内存泄漏是指你向系统申请分配内存进行使用(new/malloc),然后系统在堆内存中给这个对象申请一块内存空间,但当我们使
- (一)什么是微服务网关后端写完所有的微服务之后,最终是要交给前端去调用。我们都知道每个微服务都有各自的端口号,如果前端直接通过IP加端口的方
- 本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下前言一、游戏规则介绍扫雷是一个十分经典的游戏,一张棋盘中有很多个不
- org.slf4j.Logger中info()方法如果info()方法参数为以下类型 public void in
- 本文实例讲述了C#使用doggleReport生成pdf报表的方法。分享给大家供大家参考,具体如下:1. 安装nuget-install p
- 本篇介绍我们如何利用selenium 来操作各种页面元素阅读目录链接(link)输入框 textbox按钮(Button)下拉选择框(Sel
- Android和iOS开发都支持C++开发,可以一套代码多平台使用。同时C++难以反编译的特性也可以为Android开发带来代码的保密,另一
- 本章节内容很丰富,主要有基本的表单操作,数据的格式化,数据的校验,以及提示信息的国际化等实用技能。首先看效果图项目结构图接下来用代码重点学习
- 本文实例讲述了Java基于Swing实现的打猎射击游戏代码。分享给大家供大家参考。具体实现代码如下:package Game;import
- 本文实例为大家分享了SpringBoot集成kaptcha验证码的具体代码,供大家参考,具体内容如下1.kaptcha相关介绍Kaptcha
- 之前给大家在博文中讲过如何通过eclipse快速搭建SSM开发环境,但相对而言还是有些麻烦的,今天玄武老师给大家介绍下如何使用Intelli
- 一. 概念简介在开始学习今天的知识之前,有必要先给大家讲解一下与今天内容相关的一些概念,否则可能会让一些小白产生迷惑。1. 日期和时间的区别
- 通过一个变量控制线程中断代码:package com.itsoku.chat05;import java.util.concurrent.T
- using System;using System.Collections.Generic;using System.ComponentMo
- 如下所示:import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.
- 这是个我在C#调用批处理文件时遇到的问题。首先我通过Process.Start方法调用一个批处理文件,那个批处理文件里面则调用了一大堆程序。
- 摘要:Java8通过Function获取字段名,解决硬编码,效果类似于mybatis-plus的LambdaQueryWrapper。本文总
- 数据表格能够清晰的呈现数据信息,但是我们对于一些繁杂多变的数据想要很直观的看到数据变化走势或者数据的占比时,数据图表会更具代表性,并且在呈现
- 先看下面的这组字符,如果输出来,它是无法靠右对齐: Source Codestring[] s1 = { "300",