上一级页面:ssm-spring-boot速成学习索引
前言
底层注解
08、底层注解-@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 27
|
@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"); } }
|
@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 27 28 29 30 31 32 33 34 35 36
| @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan("com.atguigu.boot") public class MainApplication {
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); }
Pet tom01 = run.getBean("tom", Pet.class); Pet tom02 = run.getBean("tom", Pet.class); System.out.println("组件:"+(tom01 == tom02));
MyConfig bean = run.getBean(MyConfig.class); System.out.println(bean);
User user = bean.user01(); User user1 = bean.user01(); System.out.println(user == user1);
User user01 = run.getBean("user01", User.class); Pet tom = run.getBean("tom", Pet.class);
System.out.println("用户的宠物:"+(user01.getPet() == tom)); } }
|
- 最佳实战
- 配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
- 配置 类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式(默认)
lite 英 [laɪt] 美 [laɪt]
adj. 低热量的,清淡的(light的一种拼写方法);类似…的劣质品
IDEA快捷键:
Alt + Ins
:生成getter,setter、构造器等代码。
Ctrl + Alt + B
:查看类的具体实现代码。
09、底层注解-@Import导入组件
@Bean、@Component、@Controller、@Service、@Repository,它们是Spring的基本标签,在Spring Boot中并未改变它们原来的功能。
@ComponentScan 在07、基础入门-SpringBoot-自动配置特性有用例。
@Import({User.class, DBHelper.class})给容器中自动创建出这两个类型的组件、默认组件的名字就是全类名
- 这两个类型分别是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 15
|
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);
|
10、底层注解-@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);
}
|
11、底层注解-@ImportResource导入Spring配置文件
比如,公司使用bean.xml文件生成配置bean,然而你为了省事,想继续复用bean.xml,@ImportResource粉墨登场。
bean.xml:
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); }
|
12、底层注解-@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 { ... }
|
自动配置
spring-boot-自动配置
参考、引用、致谢