SpringBoot容器功能及注解超详解🎉🎉🎉
1.组件添加
1.1@Configuration
@Configuration:告诉SpringBoot这是一个配置类
-
配置类里面使用@Bean 标注在方法上给容器注册组件,默认也是单实例的 -
配置类本身也是组件 -
proxyBeanMethods :代理bean的方法
- Full(
proxyBeanMethods = true ):保证每个@Bean方法被调用多少次返回的组件都是单实例的 - Lite(
proxyBeanMethods = false ):每个@Bean方法被调用多少次返回的组件都是新创建的 - 组件依赖必须使用Full模式默认。其他默认是否Lite模式
最佳实战:
- 配置类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
- 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
代码实战演示:
@Configuration(proxyBeanMethods = false)
public class MyConfig {
@Bean
public User user01() {
User zhangsan = new User("zhangsan", 18);
return zhangsan;
}
@Bean("tom")
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 user = bean.user01();
User user1 = bean.user01();
System.out.println(user == user1);
}
}
如果有组件依赖:
@Configuration(proxyBeanMethods = true)
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");
}
}
@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);
System.out.println("用户的宠物:"+(user01.getPet() == tom));
}
}
1.2@Import
@Import:给容器中导入组件
代码演示:
@Import({User.class, DBHelper.class})
@Configuration(proxyBeanMethods = false)
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)
@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);
System.out.println(hehe);
}
}
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);
}
}
}
3.1@ConfigurationProperties
配置文件:
mycar.brand=BYD
mycar.price=100000
创建一个car类:
@Component
@ConfigurationProperties(prefix = "mycar")
@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)
@EnableConfigurationProperties(Car.class)
public class MyConfig {
}
该写法就不用在写@Component
@ConfigurationProperties(prefix = "mycar")
@Data
@NoArgsConstructor
@ToString
@AllArgsConstructor
public class Car {
private String brand;
private Integer price;
}
最后喜欢的小伙伴别忘了一键三连哦🎈🎈🎈
|