1.java代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource({"classpath:my.properties"})// springboot项目,my.properties放在\src\main\resources\内。
public class UserNavBarServiceImpl{
@Value("${casLogOutUrl}")//my.properties文件中的key
private String casLogOutUrl;// casLogOutUrl将获取到配置的值
public List<UserNavbar> getUserNavbar() {
List<UserNavbar> list = new ArrayList<>();
UserNavbar bar = new UserNavbar();
bar.setId("casLogOutUrl");
bar.setName("统一认证注销");
bar.setUrl(casLogOutUrl);
list.add(bar);
return list;
}
}
2.my.properties文件
casLogOutUrl = https://www.baidu.com/
3.注意
@Component
@PropertySource({"classpath:my.properties"}) :springboot项目,my.properties放在\src\main\resources\内。
@Value("${casLogOutUrl}") :my.properties文件中的key,private String casLogOutUrl 是配置的值