1.自定义配置文件site.properties
#网络基本配置信息
weirongqian.site.name=\u201c\u9b4f\u6995\u6c58\u000d\u000a\u201d
weirongqian.site.url="https://www.weirongqian.com"
2.从自定义配置文件中加载变量
package com.wei.base.controller.admin.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 网站基础信息配置类
*/
@Component
@PropertySource(value = "classpath:site.properties")
public class SiteConfig {
@Value("${weirongqian.site.name}")
private String siteName;
@Value("${weirongqian.site.url}")
private String siteUrl;
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getSiteUrl() {
return siteUrl;
}
public void setSiteUrl(String siteUrl) {
this.siteUrl = siteUrl;
}
}
package com.wei.base.controller.admin;
import com.wei.base.controller.admin.config.SiteConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/system")
public class SystemController {
// @Value("${spring.datasource.url}")
@Autowired
private SiteConfig siteConfig;
private Logger logger = LoggerFactory.getLogger(SystemController.class);
@RequestMapping("/index")
public String index(String name, Model model){
model.addAttribute("siteUrl",siteConfig.getSiteUrl());
logger.warn("siteName="+siteConfig.getSiteName());
logger.error("siteUrl="+siteConfig.getSiteUrl());
return "index";
}
}
3.网页显示效果