容器功能
- 1.组件添加
- 1. @Configuration
- 2. 4大组件注解
- 3. @Conditional
- 2.引入原生配置文件
- 3.配置绑定
- 3.1 @ConfigurationProperties注解
1.组件添加
1. @Configuration
在SpringBoot中,标志着这个注解,会认为这个类为配置类,可以定义自己想要的容器
- Full模式(proxyBeanMethods = true):保证每个@Bean组件返回都是单实例的,外部无论对配置类中的这个组件注册方法调用多少次获取的都是之前注册容器中的单实例对象
- Lite模式(proxyBeanMethods = false):每个@Bean组件被调用多少次就创建多少个
2. 4大组件注解
名称 | @Bean | @Component | @Controller | @Service | @Repository | @Import |
描述 | 标注实例是一个Spring容器 注解 | 标注是个容器(一般加入配置)注解 | 在Controller层标注由mvc管理注解 | 标注是服务层容器注解 | 标注是持久化层容器注解 | 快速给容器中导入一个组件 |
3. @Conditional
条件装配:满足Conditional指定的条件,则进行组件注入
在SpringBoot底层中,很多自动注入的原理都是依靠这个@ConditionalOnMissingBean注解,进行是否需要自动装配的原则,在类上或者方法上标注这个注解,决定是否需要需要对此类进行生效
测试:
@Configuration
@ConditionalOnMissingBean(name = "tom") //容器中没有tom组件的时候此类生效
public class MyConfig {
@Bean
public User user01(){
System.out.println("创建成功");
return new User("hahah");
}
3.1 User实体类
public class User {
private String name;
}
3.2 Controller测试
@RestController
public class UserController {
@Autowired
User users;
@GetMapping("/hello")
private String sayHello(){
return users.toString();
}
3.3 结果打印:
2021-04-15 14:27:05.743 INFO 7044 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-04-15 14:27:05.744 INFO 7044 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-15 14:27:05.921 INFO 7044 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-04-15 14:27:05.921 INFO 7044 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2661 ms
创建成功
2021-04-15 14:27:06.399 INFO 7044 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-15 14:27:06.878 INFO 7044 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-04-15 14:27:06.894 INFO 7044 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 5.227 seconds (JVM running for 7.603)
2021-04-15 14:27:11.367 INFO 7044 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-15 14:27:11.368 INFO 7044 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-04-15 14:27:11.369 INFO 7044 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2.引入原生配置文件
@ImportResource(“classpath:x-x-x.xml”)注解引入
2.1 存在两个实体类:
public class User {
private String name;
}
public class DesUser {
User user;
}
2.2 创建配置文件:beans.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="users" class="com.example.demo.domain.User">
<property name="name" value="小明"></property>
</bean>
<bean id="desUser" class="com.example.demo.domain.DesUser">
<property name="user" ref="users"/>
</bean>
</beans>
2.3 标志注解:主启动类上标注引入 文件
@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
2.4 自动注入Controller测试
@Autowired
DesUser desUser;
@GetMapping("/hello")
private String sayHello(){
return "用户描述:"+desUser.toString();
}
2.5 结果:
3.配置绑定
3.1 @ConfigurationProperties注解
只有在容器中的组件,才会拥有SpringBoot提供的强大功能,所以@ConfigurationProperties+@Component一齐使用
1. 配置测试:
@Component
@ConfigurationProperties(prefix = "mycolor")
public class Color {
private String red;
private String blue;
private String pink;
}
2. 在配置类上标注@EnableConfigurationProperties(Color.class)注解
补充:有两个功能:1. 绑定Color配置 2. 将color注入到容器中
3. 打印查看容器中是否存在Color容器
4. 在配置文件中添加如下配置:
mycolor.red=红色
mycolor.blue=蓝色
mycolor.pink=粉色
5. Controller测试
@RestController
public class UserController {
@Autowired
Color color;
@GetMapping("/hello")
private String sayHello(){
return color.toString();
}
}
6. 结果如下:
如果有同学遇到乱码问题,参考:页面乱码问题完美解决