SpringBoot底层注解
1. @Configuration详解
基本使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Configuration(proxyBeanMethods = false) public class MyConfig {
@Bean public User user01(){ User zhangsan = new User("zhangsan", 18); zhangsan.setPet(tomcatPet()); return zhangsan; }
@Bean("tom") public Pet tomcatPet(){ return new Pet("tomcat"); } }
|
- 最佳实战
- 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
- 配置 类组件之间有依赖关系,方法会被调用得到之前单实例组建,用Full模式(默认)
2. @Import导入组件
@Bean、@Component、@Controller、@Service、@Repository,它们是Spring的基本标签,在Spring Boot中并未改变它们原来的功能。
@Import({User.class, DBHelper.class})给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
1 2 3 4
| @Import({User.class, DBHelper.class}) @Configuration(proxyBeanMethods = false) public class MyConfig { }
|
测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
String[] beanNamesForType = run.getBeanNamesForType(User.class);
for (String s : beanNamesForType) { System.out.println(s); }
DBHelper bean1 = run.getBean(DBHelper.class); System.out.println(bean1);
|
3. @Conditional条件装配
条件装配:满足Conditional制定的条件,则进行组件注入
用@ConditionalOnMissingBean举例说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Configuration(proxyBeanMethods = false) @ConditionalOnMissingBean(name = "tom") public class MyConfig {
@Bean public User user01(){ User zhangsan = new User("zhangsan", 18); zhangsan.setPet(tomcatPet()); return zhangsan; }
@Bean("tom22") public Pet tomcatPet(){ return new Pet("tomcat"); } }
public static void main(String[] args) { ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
String[] names = run.getBeanDefinitionNames(); for (String name : names) { System.out.println(name); }
boolean tom = run.containsBean("tom"); System.out.println("容器中Tom组件:"+tom);
boolean user01 = run.containsBean("user01"); System.out.println("容器中user01组件:"+user01);
boolean tom22 = run.containsBean("tom22"); System.out.println("容器中tom22组件:"+tom22);
}
|
4. @ImportResource导入Spring配置文件
比如,公司使用bean.xml文件生成配置bean,然而你为了省事,想继续复用bean.xml,@ImportResource粉墨登场。
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8"?> <beans ...">
<bean id="haha" class="com.lun.boot.bean.User"> <property name="name" value="zhangsan"></property> <property name="age" value="18"></property> </bean>
<bean id="hehe" class="com.lun.boot.bean.Pet"> <property name="name" value="tomcat"></property> </bean> </beans>
|
使用方法:
1 2 3 4
| @ImportResource("classpath:beans.xml") public class MyConfig { ... }
|
测试:
1 2 3 4 5 6 7 8 9
| public static void main(String[] args) { ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
boolean haha = run.containsBean("haha"); boolean hehe = run.containsBean("hehe"); System.out.println("haha:"+haha); System.out.println("hehe:"+hehe); }
|
5. ConfigurationProperties配置绑定
如何使用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); } } }
|
Spring Boot一种配置配置绑定:
@ConfigurationProperties + @Component
假设有配置文件application.properties
1 2
| mycar.brand=BYD mycar.price=100000
|
只有在容器中的组件,才会拥有SpringBoot提供的强大功能
1 2 3 4 5
| @Component @ConfigurationProperties(prefix = "mycar") public class Car { ... }
|
Spring Boot另一种配置配置绑定:
@EnableConfigurationProperties + @ConfigurationProperties
- 开启Car配置绑定功能
- 把Car这个组件自动注册到容器中
1 2 3 4
| @EnableConfigurationProperties(Car.class) public class MyConfig { ... }
|
1 2 3 4
| @ConfigurationProperties(prefix = "mycar") public class Car { ... }
|