一.通过context : property-placeholder加载配置文件jdbc.properties的内容
ignore-unresolvable='true'/
以上结构与以下结构等效,是以下结构的简化。
classpath:jdbc.properties
二.使用util:properties标签暴露properties文件的内容
注:在此行的配置中,必须在spring-dao.xml文件的开头声明以下部分:
xmlns 3360 xsi=' http://www.w3.org/2001/XML架构-instance '
xmlns 3360 context=' http://www.spring framework.org/schema/context '
xmlns 3360 util=' http://www.spring framework.org/schema/util '
xsi :方案位置=' http://www.spring framework.org/schema/beans
33558 www.spring framework.org/schema/beans/spring-beans-3.2.xsd
33558 www.spring framework.org/schema/context
33558 www.spring framework.org/schema/context/spring-context-3.2.xsd
33558 www.spring framework.org/schema/util
33558 www.spring framework.org/schema/util/spring-util.xsd '
三.通过PropertyPlaceholderConfigurer在加载上下文时将properties暴露给自定义子类的属性,以供程序使用
classpath:jdbc.properties
自定义类PropertyConfigurer的声明为
//*
* desc :属性文件读取类
*/
publicclasspropertyconfigurerextendspropertyplaceholderconfigurer {
私有属性属性; 访问属性配置文件密钥-值的结果
@Override
protectedvoidprocessproperties (configurablelistablebeanfactorybeanfactorytoprocess,属性程序) )。
throws BeansException {
super.process properties (beanfactorytoprocess,props );
this.props=props;
}
公共字符串属性(字符串密钥) {
return this.props.getproperty (key;
}
公共字符串生成属性(string key,String defaultValue ) {
return this.props.getproperty (key,defaultValue );
}
publicobjectsetproperty (string key,String value ) {
return this.props.set property (key,value );
}
}
使用方法:在使用的类中使用@Autowired注释注入即可。
四.定制工具类PropertyUtil,在该类的静态静态代码块中读取properties文件的内容并将其保存在静态属性中,以便在其他程序中使用
//*
* desc :属性文件获取工具类
*/
公共类属性实用工具{
privatestaticfinalloggerlogger=logger factory.getlogger (property util.class;
私有状态属性属性;
静态{
loadProps (;
}
synchronizedstaticprivatevoidloadprops (
logger.info (开始加载“属性”文件的内容
.......");props = new Properties();
InputStream in = null;
try {
in = PropertyUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
props.load(in);
} catch (FileNotFoundException e) {
logger.error("jdbc.properties文件未找到");
} catch (IOException e) {
logger.error("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
logger.error("jdbc.properties文件流关闭出现异常");
}
}
logger.info("加载properties文件内容完成...........");
logger.info("properties文件内容:" + props);
}
public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}
说明:这样的话,在该类被加载的时候,它就会自动读取指定位置的配置文件内容并保存到静态属性中,高效且方便,一次加载,可多次使用。
五、使用注解的方式注入,主要用在java代码中使用注解注入properties文件中相应的value值
classpath:jdbc.properties
六、@Value(常用)
application.properties配置文件
string.port=1111
integer.port=1111
db.link.url=jdbc:mysql://localhost:3306/test
db.link.driver=com.mysql.jdbc.Driver
db.link.username=root
db.link.password=root
类文件:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyConf {
@Value("${string.port}") private int intPort;
@Value("${string.port}") private String stringPort;
@Value("${db.link.url}") private String dbUrl;
@Value("${db.link.driver}") private String dbDriver;
@Value("${db.link.username}")private String dbUsername;
@Value("${db.link.password}")private String dbPassword;
public void show(){
System.out.println("======================================");
System.out.println("intPort : " + (intPort + 1111));
System.out.println("stringPort : " + (stringPort + 1111));
System.out.println("string : " + dbUrl);
System.out.println("string : " + dbDriver);
System.out.println("string : " + dbUsername);
System.out.println("string : " + dbPassword);
System.out.println("======================================");
}
}
类名上指定配置文件@PropertySource可以声明多个,或者使用@PropertySources(@PropertySource(“xxx”),@PropertySource(“xxx”))。
在bean中使用@value注解获取配置文件的值
@Value("${key}")
private Boolean timerEnabled;
即使给变量赋了初值也会以配置文件的值为准。
七、import org.springframework.core.env.Environment;
如何引用这个类:
可以通过 @Autowired注入Environment
@Autowired
private Environment environment;
可以通过实现 EnvironmentAware 然后实现接口中的方法
@Setter
private Environment environment;
常用功能
获取属性配制文件中的值:environment.getProperty("rabbitmq.address")
获取是否使用profile的
public boolean isDev(){
boolean devFlag = environment.acceptsProfiles("dev");
return devFlag;
}
八、@ConfigurationProperties(常用)
通过@ConfigurationProperties读取配置信息并与 bean 绑定,可以像使用普通的 Spring bean 一样,将其注入到类中使用。
@Component
@ConfigurationProperties(prefix = "library")
class LibraryProperties {
@NotEmpty
private String location;
private List books;
@Setter
@Getter
@ToString
static class Book {
String name;
String description;
}
//省略getter/setter
......
}
九、PropertySource(不常用)
@PropertySource读取指定 properties 文件
@Component
@PropertySource("classpath:website.properties")
class WebSite {
@Value("${url}")
private String url;
省略getter/setter
......
}