_008_SpringBoot_自定義配置

VeryHotLight發表於2018-07-31

 ------------------------------------------------------- 

 

spring.profiles.active=dev

boot.name=反反覆覆
boot.location=頂頂頂頂付

spring.http.encoding.charset=utf-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true

 -------------------------------------------------------

 

 

@Controller
public class ConfigurationController {

    @Value("${boot.name}")
    private String name;

    @Value("${boot.location}")
    private String location;

    @RequestMapping("/boot/config")
    public @ResponseBody String confif(){
        return name+"------"+location;
    }

}

 

 

 ------------------------------------------------------- 

@Component
@ConfigurationProperties(prefix = "boot")
public class Configuration {

    private String name;
    private String location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
    
}

 ​​​​​​​-------------------------------------------------------

@Controller
public class ConfigurationController {

    @Autowired
    public Configuration configuration;

    @Value("${boot.name}")
    private String name;

    @Value("${boot.location}")
    private String location;

    @RequestMapping("/boot/config")
    public @ResponseBody String config(){
        return name+"------"+location;
    }

    @RequestMapping("/boot/config_1")
    public @ResponseBody String config_1(){
        String lo = configuration.getLocation();
        String name = configuration.getName();
        return  lo+"-helllo---"+name;
    }

}
 ​​​​​​

-------------------------------------------------------

 

 

 

相關文章