Spring boot使用配置文件
Spring boot使用配置文件
yan5845hao 发表于7个月前
Spring boot使用配置文件
  • 发表于 7个月前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

第一种使用@PropertySource注解

package com.uwo.spring.boot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by yanhao on 2017/5/22.
 */
@Controller
@PropertySource(value = "classpath:/application.properties")
public class AuthController {

    @Value("${uwo.password}")
    private String password;

    @RequestMapping("/")
    @ResponseBody
    public String auth(){
        return "hello, " + password;
    }
}

第二种使用@ConfigurationProperties与@EnableConfigurationProperties注解实现

package com.uwo.spring.boot.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * Created by yanhao on 2017/5/22.
 */
@ConfigurationProperties(prefix = "uwo")
public class UwoProperties {

    private String username;

    private String password;

    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;
    }
}

package com.uwo.spring.boot.controller;
import com.uwo.spring.boot.properties.UwoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by yanhao on 2017/5/22.
 */
@Controller
@EnableConfigurationProperties({UwoProperties.class})
public class AuthController {
    @Autowired
    private UwoProperties uwoProperties;
    @RequestMapping("/")
    @ResponseBody
    public String auth(){
        return "hello, " + uwoProperties.getUsername();
    }
}

第三种使用Environment与@Autowired注解实现

package com.uwo.spring.boot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by yanhao on 2017/5/22.
 */
@Controller
public class AuthController {
    @Autowired
    private Environment env;
    @RequestMapping("/")
    @ResponseBody
    public String auth(){
        return "hello, " + env.getProperty("uwo.username");
    }
}
共有 人打赏支持
粉丝 7
博文 102
码字总数 20648
×
yan5845hao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: