方案1:@ConfigurationProperties+@Component
1 defines an entity bean spring loaded profile information, other configuration information to be used is injected into the entity bean 2 . 3 ** / . 4 * values of each of the attributes configuration file is mapped to the component . 5 * @ConfigurationProperties : tell SpringBoot all the attributes and configuration files in this class for the corresponding configuration bind; 6 * prefix = "the Person": profile in which all of the following attributes one mapping 7 * 8 * this component is only a container @ConfigurationProperties assembly functions to provide the container; . 9 * 10 * / . 11 @Component 12 is @ConfigurationProperties (prefix = "Person" ) 13 is public class the Person { 14 15 Private String lastName; 16 Private Integer age; 17 private Boolean boss; 18 private Date birth; 19 20 private Map<String,Object> maps; 21 private List<Object> lists; 22 private Dog dog;
Scheme 2: @ Bean + @ ConfigurationProperties
We can also put @ConfigurationProperties also be defined directly on @bean notes, which is the entity bean class would not @Component and @ConfigurationProperties, and here is Boot's dynamic data source switching classes.
1 package com.topcheer.oss.base.datasource; 2 3 import com.alibaba.druid.pool.DruidDataSource; 4 5 import com.xiaoleilu.hutool.crypto.symmetric.SymmetricAlgorithm; 6 import com.xiaoleilu.hutool.crypto.symmetric.SymmetricCrypto; 7 import com.xiaoleilu.hutool.util.CharsetUtil; 8 import com.xiaoleilu.hutool.util.HexUtil; 9 10 import lombok.extern.slf4j.Slf4j; 11 12 @Slf4j 13 public class UmspscDataSource extends DruidDataSource { 14 15 private static final long serialVersionUID = 4766401181052251539L; 16 17 private String passwordDis; 18 19 /** 20 * 密匙 21 */ 22 private final static String Pkey ="1234565437892132"; 23 24 @Override 25 public String getPassword() { 26 if(passwordDis != null && passwordDis.length() > 0) { 27 return passwordDis; 28 } 29 String encPassword = Super .getPassword (); 30 IF ( null == encPassword) { 31 is return null ; 32 } 33 is log.info ( "Database encryption password, {" + encPassword + " } " ); 34 is the try { 35 // ciphertext decryption, decryption method may be modified 36 String Key = HexUtil.encodeHexStr (Pkey); 37 [ SymmetricCrypto AES = new newSymmetricCrypto (SymmetricAlgorithm.AES, key.getBytes ()); 38 is passwordDis = aes.decryptStr (encPassword, CharsetUtil.CHARSET_UTF_8); 39 return passwordDis; 40 } the catch (Exception E) { 41 is log.error ( "database password decryption error, { "+ encPassword +"} " ); 42 is // log.error (LogUtil.e (E)); 43 is // the throw new new Exception (" database decryption failed ", E);! 44 is return null ; 45 } 46 is } 47 48 }
1 @Bean(name = "systemDataSource") 2 @ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource.system") 3 public DataSource systemDataSource() { 4 return new UmspscDataSource(); 5 } 6 7 @Bean(name = "secondDataSource") 8 @ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource.second") 9 public DataSource secondDataSource() { 10 return new UmspscDataSource(); 11 } 12 13 @Bean(name="systemJdbcTemplate") 14 public JdbcTemplate systemJdbcTemplate( 15 @Qualifier("systemDataSource") DataSource dataSource) { 16 return new JdbcTemplate(dataSource); 17 } 18 19 @Bean(name="secondJdbcTemplate") 20 public JdbcTemplate secondJdbcTemplate( 21 @Qualifier("secondDataSource") DataSource dataSource) { 22 return new JdbcTemplate(dataSource); 23 }
方案3:@ConfigurationProperties + @EnableConfigurationProperties
And our example above, like annotation attribute, and then use the Spring @Autowire
to inject mail configuration bean:
1 package com.dxz.property; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 5 @ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") 6 public class MailProperties { 7 private String host; 8 private int port; 9 private String from; 10 private String username; 11 private String password; 12 private Smtp smtp; 13 14 // ... getters and setters 15 public String getHost() { 16 return host; 17 } 18 19 public void setHost(String host) { 20 this.host = host; 21 } 22 23 public int getPort() { 24 return port; 25 } 26 27 public void setPort(int port) { 28 this.port = port; 29 } 30 31 public String getFrom() { 32 return from; 33 } 34 35 public void setFrom(String from) { 36 this.from = from; 37 } 38 39 public String getUsername() { 40 return username; 41 } 42 43 public void setUsername(String username) { 44 this.username = username; 45 } 46 47 public String getPassword() { 48 return password; 49 } 50 51 public void setPassword(String password) { 52 this.password = password; 53 } 54 55 public Smtp getSmtp() { 56 return smtp; 57 } 58 59 public void setSmtp(Smtp smtp) { 60 this.smtp = smtp; 61 } 62 63 @Override 64 public String toString() { 65 return "MailProperties [host=" + host + ", port=" + port + ", from=" + from + ", username=" + username 66 + ", password=" + password + ", smtp=" + smtp + "]"; 67 } 68 69 public static class Smtp { 70 private boolean auth; 71 private boolean starttlsEnable; 72 73 public boolean isAuth() { 74 return auth; 75 } 76 77 public void setAuth(boolean auth) { 78 this.auth = auth; 79 } 80 81 public boolean isStarttlsEnable() { 82 return starttlsEnable; 83 } 84 85 public void setStarttlsEnable(boolean starttlsEnable) { 86 this.starttlsEnable =starttlsEnable; 87 } 88 89 } 90 }
Start class and test class:
1 package com.dxz.property; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.boot.builder.SpringApplicationBuilder; 6 import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.ResponseBody; 10 import org.springframework.web.bind.annotation.RestController; 11 12 @RestController 13 @SpringBootApplication 14 @EnableConfigurationProperties(MailProperties.class) 15 public class TestProperty1 { 16 17 @Autowired 18 private MailProperties mailProperties; 19 20 @RequestMapping(value = "/hello", method = RequestMethod.GET) 21 @ResponseBody 22 public String hello() { 23 System.out.println("mailProperties" + mailProperties); 24 return "hello world"; 25 } 26 27 public static void main(String[] args) { 28 //SpringApplication.run(TestProperty1.class, args); 29 new SpringApplicationBuilder(TestProperty1.class).web(true).run(args); 30 31 } 32 }
result:
Please note @EnableConfigurationProperties comment . The annotation is used to enable support for the configuration Bean @ConfigurationProperties comment. That is @EnableConfigurationProperties annotation tells Spring Boot support @ConfigurationProperties. If you do not see the following exception:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.dxz.property.MailProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Note: There are other ways (Spring Boot always other ways!) Let @ConfigurationProperties
beans are added - with @Configuration
or @Component
notes so that it can be found in the component scan time.
@ConfigurationProperties | @Value | |
---|---|---|
Features | Batch injection configuration file attributes | A designated |
Loosely bound (loose syntax) | stand by | not support |
Game | not support | stand by |
JSR303 data check | stand by | not support |
Complex type package | stand by | not support |
Profile yml or properties they can get to value;
If you say that we just need to get some business logic at a particular value in the configuration file, use @Value;