spring boot 使用@ConfigurationProperties
有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我们就可以使用 @ConfigurationProperties,它可以把同类的配置信息自动封装成实体类
首先在配置文件里面,这些信息是这样子滴
connection.username=admin
connection.password=kyjufskifas2jsfs
connection.remoteAddress=192.168.1.1
这时候我们可以定义一个实体类在装载配置文件信息
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings {</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String username; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String remoteAddress; </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String password ; </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getUsername() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> username; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUsername(String username) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.username =<span style="color: rgba(0, 0, 0, 1)"> username; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getRemoteAddress() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> remoteAddress; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setRemoteAddress(String remoteAddress) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.remoteAddress =<span style="color: rgba(0, 0, 0, 1)"> remoteAddress; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPassword() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> password; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPassword(String password) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.password =<span style="color: rgba(0, 0, 0, 1)"> password; }
}
我们还可以把 @ConfigurationProperties 还可以直接定义在 @bean 的注解上,这是 bean 实体类就不用 @Component 和 @ConfigurationProperties 了
@SpringBootApplication public class DemoApplication{@Bean @ConfigurationProperties(prefix = "connection") public ConnectionSettings connectionSettings(){ return new ConnectionSettings();</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">...</span>
} </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { SpringApplication.run(DemoApplication.</span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">, args); }
}
然后我们需要使用的时候就直接这样子注入
@RestController @RequestMapping("/task") public class TaskController {@Autowired ConnectionSettings conn;
@RequestMapping(value = {"/",""})
public String hellTask(){
String userName = conn.getUsername();
return "hello task !!";
}}
如果发现 @ConfigurationPropertie 不生效,有可能是项目的目录结构问题,
你可以通过 @EnableConfigurationProperties(ConnectionSettings.class) 来明确指定需要用哪个实体类来装载配置信息
@Configuration @EnableConfigurationProperties(ConnectionSettings.class) public class MailConfiguration { @Autowired private MailProperties mailProperties;@Bean </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> JavaMailSender javaMailSender() { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> omitted for readability</span>
}
}