加载应用程序属性Spring-boot获得空值

时间:2018-10-10 15:20:01

标签: spring-boot application.properties

我试图使用
从application.properties文件中读取属性 @ConfigurationProperties获取变量的空值,尝试使用@Value也有同样的问题。是否有其他方法可以将属性加载到应用程序中

        @Component
    @ConfigurationProperties(prefix = "email")
    public class EmailProperties {enter code here

        private String host;

        private String port;

        private String username ;

        private String password;

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port = port;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "EmailProperties [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password
                    + "]";
        }

属性文件

email.host=test
 email.port=25
 email.username=Test
email.password=Test

使用这些属性的方法

 @Autowired
    private EmailProperties emailProperties;

    @SuppressWarnings("unused")
    public void sendMail( String toEmail, String messageSubject, String messageBody) {

        System.out.println("host");

        Properties prop = new Properties();
        prop.put("mail.smtp.auth", false);
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.host", emailProperties.getHost());
        prop.put("mail.smtp.port", emailProperties.getPort());
        prop.put("mail.smtp.ssl.trust", emailProperties.getHost());

0 个答案:

没有答案