1.组件添加
1.1@Configuration
@Configuration:告诉SpringBoot这是一个配置类
配置类里面使用@Bean
标注在方法上给容器注册组件,默认也是单实例的
配置类本身也是组件
proxyBeanMethods
:代理bean的方法
-
Full(
proxyBeanMethods = true
):保证每个@Bean方法被调用多少次返回的组件都是单实例的 -
Lite(
proxyBeanMethods = false
):每个@Bean方法被调用多少次返回的组件都是新创建的 - 组件依赖必须使用Full模式默认。其他默认是否Lite模式
最佳实战:
1.配置类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
2.配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
代码实战演示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@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); } } |
如果有组件依赖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@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" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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:给容器中导入组件
代码演示:
1
2
3
4
5
|
//给容器中自动无参构造创建出这两个类型的组件、默认组件的名字就是全类名 @Import ({User. class , DBHelper. class }) @Configuration (proxyBeanMethods = false ) //告诉SpringBoot这是一个配置类 == 配置文件 public class MyConfig { } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@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文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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 <a href=" https://www.springframework.org/schema/context/spring-context.xsd">" rel="external nofollow">https://www.springframework.org/schema/context/spring-context.xsd"> </ a > < 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 > |
自定义配置类:
1
2
3
4
|
@Configuration (proxyBeanMethods = true ) //告诉SpringBoot这是一个配置类=配置文件 @ImportResource ( "classpath:beans.xml" ) public class MyConfig { } |
测试:
1
2
3
4
5
6
7
8
9
10
11
12
|
@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中,以供随时使用;
原生方法(配置文件复杂就显得麻烦):
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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类:
1
2
3
4
5
6
7
8
9
10
11
12
|
//只有在容器中的组件,才会拥有SpringBoot提供的强大功能 @Component @ConfigurationProperties (prefix = "mycar" ) //Lombok注解简化开发 @Data @NoArgsConstructor @ToString @AllArgsConstructor public class Car { private String brand; private Integer price; } |
测试方法:
1
2
3
4
5
6
7
8
9
|
@RestController public class HelloController { @Autowired Car car; @RequestMapping ( "/car" ) public Car car(){ return car; } } |
测试结果:
3.2@EnableConfigurationProperties + @ConfigurationProperties
@EnableConfigurationProperties
必须在配置类里写:
1
2
3
4
5
6
|
@Configuration (proxyBeanMethods = true ) //告诉SpringBoot这是一个配置类=配置文件 @EnableConfigurationProperties (Car. class ) //1.开启Car属性配置绑定功能 //2.把Car这个组件自动注册到容器中 public class MyConfig { } |
该写法就不用在写@Component
1
2
3
4
5
6
7
8
9
|
@ConfigurationProperties (prefix = "mycar" ) @Data @NoArgsConstructor @ToString @AllArgsConstructor public class Car { private String brand; private Integer price; } |
以上就是学习SpringBoot容器功能及注解原理的详细内容,更多关于SpringBoot容器功能及注解的资料请关注服务器之家其它相关文章!
原文链接:https://blog.csdn.net/qq_45966440/article/details/120412878