package com.bojia.fund.proconfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value={"classpath:application.properties","classpath:message.properties"},encoding="UTF-8")//设置编码格式
public class MessageProperty {
@Value("${msg_nopay}")
private String msg_nopay;
public String getMsg_nopay() {
return msg_nopay;
}
}
文件目录结构为src/main/re/application.properties,
src/main/resources/message.properties.
我这里有两个配置文件,如果要引用MessageProperty 这个类的话在server层或者controller层加上如下代码:
@Controller
public class TestController {
@Autowired
MessageProperty property;
@RequestMapping(value = "/index.do",produces="text/plain;charset=UTF-8",method = RequestMethod.GET)
public String index() {
//你的代码
return null;
}
}
我这里是controller,server层也是一样,就可以使用了,注意:
1.要用到@value这个注解的话,不要new,否则取不到值,我这里用的是@Autowired
MessageProperty property;还有application.properties在resources目录下,如果出现扫描不到xxx.properties等自定义配置文件的话可能是你的
@PropertySource(value={"classpath:application.properties","classpath:message.properties"},encoding="UTF-8")配置错误,注意不要写成
@PropertySource(value={"classpath:resources/application.properties","classpath:resources/message.properties"},encoding="UTF-8"),否认你会启动报错的,因为SpringBoot会依次默认从 src/main/resources/等路径下查找文件,你就不要多此一举在前面加一个"resources/"了
2.如果出现配置文件中文乱码就在注解@PropertySource里面加上encoding="UTF-8",可以解决配置文件中文乱码问题!
3.多个配置文件的话@PropertySource要用value={"classpath:application.properties","classpath:message.properties"}来指定
4.@value得注解在@Component,@Server,@Controller下都可以拿到值