SpringBoot注⼊配置⽂件的3种⽅法
⽅案1:@ConfigurationProperties+@Component
1定义spring的⼀个实体bean装载配置⽂件信息,其它要使⽤配置信息是注⼊该实体bean
2
3/**
4 * 将配置⽂件中配置的每⼀个属性的值,映射到这个组件中
5 * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置⽂件中相关的配置进⾏绑定;
6 * prefix = "person":配置⽂件中哪个下⾯的所有属性进⾏⼀⼀映射
7 *
8 * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
9 *
10*/
11@Component
12 @ConfigurationProperties(prefix = "person")
13public class Person {
14
15private String lastName;
16private Integer age;
17private Boolean boss;
18private Date birth;
19
20private Map<String,Object> maps;
21private List<Object> lists;
22private Dog dog;
⽅案2:@Bean+@ConfigurationProperties
我们还可以把@ConfigurationProperties还可以直接定义在@bean的注解上,这是bean实体类就不⽤@Component和@ConfigurationProperties了,这边是Boot的动态数据源切换的类。
pcheer.oss.base.datasource;
2
3import com.alibaba.druid.pool.DruidDataSource;
4
5import com.pto.symmetric.SymmetricAlgorithm;
6import com.pto.symmetric.SymmetricCrypto;
7import com.xiaoleilu.hutool.util.CharsetUtil;
8import com.xiaoleilu.hutool.util.HexUtil;
9
slf4j.Slf4j;
11
12@Slf4j
13public class UmspscDataSource extends DruidDataSource {
14
15private static final long serialVersionUID = 4766401181052251539L;
16
17private String passwordDis;
18
19/**
20 * 密匙
21*/
22private final static String Pkey ="1234565437892132";
23
24 @Override
25public String getPassword() {
26if(passwordDis != null && passwordDis.length() > 0) {
27return passwordDis;
28 }
29 String encPassword = Password();
30if(null == encPassword) {
31return null;
32 }
33 log.info("数据库密码加解密,{" + encPassword + "}");
34try {
35// 密⽂解密,解密⽅法可以修改
36 String key = deHexStr(Pkey);
37 SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, Bytes());
38 passwordDis = aes.decryptStr(encPassword, CharsetUtil.CHARSET_UTF_8);
39return passwordDis;
40 } catch (Exception e) {
41 ("数据库密码解密出错,{"+encPassword + "}");
42//(LogUtil.e(e));
43//throw new Exception("数据库密码解密失败!", e);
44return null;
45 }
46 }
47
48 }
1 @Bean(name = "systemDataSource")
2 @ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource.system")
3public DataSource systemDataSource() {
4return new UmspscDataSource();
5 }
6
7 @Bean(name = "secondDataSource")
8 @ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource.second")
9public DataSource secondDataSource() {
10return new UmspscDataSource();
11 }
12
13 @Bean(name="systemJdbcTemplate")
14public JdbcTemplate systemJdbcTemplate(
15 @Qualifier("systemDataSource") DataSource dataSource) {
16return new JdbcTemplate(dataSource);
17 }
18
19 @Bean(name="secondJdbcTemplate")
20public JdbcTemplate secondJdbcTemplate(
21 @Qualifier("secondDataSource") DataSource dataSource) {
22return new JdbcTemplate(dataSource);
23 }
⽅案3:@ConfigurationProperties + @EnableConfigurationProperties
我们和上⾯例⼦⼀样注解属性,然后⽤ Spring的@Autowire来注⼊ mail configuration bean:
1package com.dxz.property;
2
3import org.t.properties.ConfigurationProperties;
4
5 @ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") 6public class MailProperties {
7private String host;
8private int port;
9private String from;
10private String username;
11private String password;
12private Smtp smtp;
13
14// ... getters and setters
15public String getHost() {
16return host;
17 }
18
spring怎么读取properties19public void setHost(String host) {
20this.host = host;
21 }
22
23public int getPort() {
24return port;
25 }
26
27public void setPort(int port) {
28this.port = port;
29 }
30
31public String getFrom() {
32return from;
33 }
34
35public void setFrom(String from) {
36this.from = from;
37 }
38
39public String getUsername() {
40return username;
41 }
42
43public void setUsername(String username) {
44this.username = username;
45 }
46
47public String getPassword() {
48return password;
49 }
50
51public void setPassword(String password) {
52this.password = password;
53 }
54
55public Smtp getSmtp() {
56return smtp;
57 }
58
59public void setSmtp(Smtp smtp) {
60this.smtp = smtp;
61 }
62
63 @Override
64public String toString() {
65return "MailProperties [host=" + host + ", port=" + port + ", from=" + from + ", username=" + username
66 + ", password=" + password + ", smtp=" + smtp + "]";
67 }
68
69public static class Smtp {
70private boolean auth;
71private boolean starttlsEnable;
72
73public boolean isAuth() {
74return auth;
75 }
76
77public void setAuth(boolean auth) {
78this.auth = auth;
79 }
80
81public boolean isStarttlsEnable() {
82return starttlsEnable;
83 }
84
85public void setStarttlsEnable(boolean starttlsEnable) {
86this.starttlsEnable = starttlsEnable;
87 }
88
89 }
90 }
启动类及测试类:
1package com.dxz.property;
2
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.boot.autoconfigure.SpringBootApplication;
5import org.springframework.boot.builder.SpringApplicationBuilder;
6import org.t.properties.EnableConfigurationProperties;
7import org.springframework.web.bind.annotation.RequestMapping;
8import org.springframework.web.bind.annotation.RequestMethod;
9import org.springframework.web.bind.annotation.ResponseBody;
10import org.springframework.web.bind.annotation.RestController;
11
12@RestController
13@SpringBootApplication
14 @EnableConfigurationProperties(MailProperties.class)
15public class TestProperty1 {
16
17 @Autowired
18private MailProperties mailProperties;
19
20 @RequestMapping(value = "/hello", method = RequestMethod.GET)
21 @ResponseBody
22public String hello() {
23 System.out.println("mailProperties" + mailProperties);
24return "hello world";
25 }
26
27public static void main(String[] args) {
28//SpringApplication.run(TestProperty1.class, args);
29new SpringApplicationBuilder(TestProperty1.class).web(true).run(args);
30
31 }
32 }
结果:
请注意@EnableConfigurationProperties注解。该注解是⽤来开启对@ConfigurationProperties注解配置Bean的⽀持。也就是@EnableConfigurationProperties注解告诉Spring Boot 能⽀持@ConfigurationProperties。如果不指定会看到如下异常:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dxz.property.MailProperties] foun d for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springfram ework.beans.factory.annotation.Autowired(required=true)}
注意: 还有其他办法 (Spring Boot 总是有其他办法!) 让@ConfigurationProperties beans 被添加 – ⽤@Configuration或者 @Component注解, 这样就可以在 component scan时候被发现了。
@ConfigurationProperties@Value
功能批量注⼊配置⽂件中的属性⼀个个指定
松散绑定(松散语法)⽀持不⽀持
SpEL不⽀持⽀持
JSR303数据校验⽀持不⽀持
复杂类型封装⽀持不⽀持
配置⽂件yml还是properties他们都能获取到值;
如果说,我们只是在某个业务逻辑中需要获取⼀下配置⽂件中的某项值,使⽤@Value;
如果说,我们专门编写了⼀个javaBean来和配置⽂件进⾏映射,我们就直接使⽤@ConfigurationProperties;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论