SpringBoot入门系列~SpringBoot属性配置2

1、上一章节通过@Value("${customMessager}")获取配置文件的值,但是如果一个配置文件有多个值,在赋值的代码中会显得特别臃肿,现在通过Spring中bean的模式管理可以实现

2、以发邮件为例

  首先新建一个mail.properties属性配置文件,配置属性的信息

#邮件基本配置信息
mail.host=135.140.39.188
mail.port=8808
mail.smtp=false
mail.from=smtp.126.com
mail.username=wnst1990@126.com    
mail.password=*********

  新建Mail的实体Bean

复制代码
package com.sun.spring.boot.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 邮件基本配置信息配置信息
* @ClassName: MySqlCfg  
* @author sunt  
* @date 2017年11月7日 
* @version V1.0
 */
@Component    //@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
@ConfigurationProperties(prefix = "mail") 
@PropertySource("classpath:/mail.properties") //指定配置文件的位置
public class MailCfgBean {

    private String host;
    private String port;
    private String smtp;
    private String from;
    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 getSmtp() {
        return smtp;
    }
    public void setSmtp(String smtp) {
        this.smtp = smtp;
    }
    public String getFrom() {
        return from;
    }
    public void setFrom(String from) {
        this.from = from;
    }
    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;
    }
    
    
}
复制代码

新建Controller将Bean对象注入,获取属性配置的文件的值通过对象获取

复制代码
package com.sun.spring.boot.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sun.spring.boot.properties.MailCfgBean;

/**
 * mail控制器
* @ClassName: MailController  
* @author sunt  
* @date 2017年11月7日 
* @version V1.0
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    //注入mail配置实体Bean
    @Resource
    private MailCfgBean mailCfgBean;
    
    @RequestMapping("/showMailInfo")
    public String showMailInfo() {
        return "host=" + mailCfgBean.getHost() + ",port=" + mailCfgBean.getPort() + 
                "<br />,smpt=" + mailCfgBean.getSmtp() + ",from:" + mailCfgBean.getFrom() + 
                "<br />,username=" + mailCfgBean.getUsername() + ",password=" + mailCfgBean.getPassword();
    }
}
复制代码

浏览器访问:http://127.0.0.1:8088/SpringBootChapter1/mail/showMailInfo

项目源码地址:https://gitee.com/SunnySVN/SprinBoot.git

posted @   sunny1009  阅读(225)  评论(0)    收藏  举报

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示