⼗、spring框架中对property属性⽂件的读取的5种⽅式
1、⽅式⼀:通过spring框架 PropertyPlaceholderConfigurer ⼯具实现
<bean id="propertyConfig" class="org.springframework.fig.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<value>classpath:conf/jdbc.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${tion.driver}"/>
<property name="url" value="${tion.url}"/>
<property name="username" value="${tion.username}"/>
<property name="password" value="${tion.password}"/>
</bean>
<!-- DAL客户端接⼝实现->
<bean id="dalClient" class="org.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
View Code
2、⽅式⼆:简化配置
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:jee="/schema/jee" xmlns:aop="/schema/aop"
xmlns:context="/schema/context" xmlns:tx="/schema/tx"
xsi:schemaLocation="/schema/beans
g/schema/beans/spring-beans.xsd
/schema/jee /schema/jee/spring-jee.xsd
/schema/context /schema/context/spring-context.xsd
/schema/aop /schema/aop/spring-aop.xsd
/schema/tx /schema/tx/spring-tx.xsd
">
<context:property-placeholder location="classpath:conf/jdbc.properties" ignore-unresolvable="true"/>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${tion.driver}"/>
<property name="url" value="${tion.url}"/>
<property name="username" value="${tion.username}"/>
<property name="password" value="${tion.password}"/>
</bean>
<!-- DAL客户端接⼝实现-->
<bean id="dalClient" class="org.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--备注:如果${} 这种写法⽆法读取到,或者编译出错,则增加ignore-unresolvable="true"的属性信息,并添加上⽂的命名空间信息-->
jdbc.properties⽂件:
mocha教程
View Code
上述配置理解:
1)ignore-unresolvable属性的⽰意:
<xsd:documentation><![CDATA[
Specifies if failure to find the property value to replace a key should be ignored.
Default is "false", meaning that this placeholder configurer will raise an exception
if it cannot resolve a key. Set to "true" to allow the configurer to pass on the key
to any others in the context that have not yet visited the key in question.
]]>
翻译后:指定是否应忽略未能到⽤于替换键的属性值。默认值为“false”,表⽰如果它⽆法解析密钥,此占位符配置程序将引发异常。设置为“true”以允许配置程序传递密钥对于上下⽂中尚未访问相关密钥的任何其他⽤户。
2) 为简化 PropertyPlaceholderConfigurer 的使⽤,Spring提供了<context:property-placeholder location="classpath:jdbc.properties" />元素,启⽤它后,开发者便不⽤配置PropertyPlaceholderConfigurer对象了。PropertyPlaceholderConfigurer内置的功能⾮常丰富,如果它未到${xxx}中定义的xxx键,它还会去JVM系统属性(Property())和环境变量(v())中寻。其通过启⽤systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这⼀⾏为。context:property-placeholder⼤⼤的⽅便了我们数据库的配置。这样就可以为spring配置的bean的属性设置值了。
备注:spring容器中最多只能定义⼀个 context:property-placeholder,否则会报错:Could not resolve
placeholder XXX,但如果想引⼊多个属性⽂件怎么办那,可以使⽤通配符:<context:property-placeholder location="classpath*:conf*.properties"/>
3、⽅式三:通过对spring PropertyPlaceholderConfigurer bean⼯⼚后置处理器的实现,在java程序中进⾏属性⽂件的读取
<bean id="propertyConfigurer" class="com.weblearn.utils.PropertyConfigurer">
<property name="locations">
<list>
<value>classpath:conf/web-sys-relation.properties</value>
<value>classpath:conf/jdbc.properties</value>
<value>classpath:conf/main-setting-web.properties</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"/>
</bean>
<!-- DAL客户端接⼝实现-->
<bean id="dataSource" class="org.apachemons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${tion.driver}"/>
<property name="url" value="${tion.url}"/>
<property name="username" value="${tion.username}"/>
<property name="password" value="${tion.password}"/>
</bean>
<bean id="dalClient" class="org.JdbcTemplate">properties文件用什么打开
<property name="dataSource" ref="dataSource"/>
</bean>
View Code
public class PropertyConfigurer extends PropertyPlaceholderConfigurer {
/*
PropertyPlaceholderConfigurer 是个bean⼯⼚后置处理器的实现,也就是 BeanFactoryPostProcessor 接⼝的⼀个实现。
在Spring中,使⽤PropertyPlaceholderConfigurer可以在XML配置⽂件中加⼊外部属性⽂件,当然也可以指定外部⽂件的编码。PropertyPlaceholderConfigurer可以将上下⽂
(配置⽂件)中的属性值放在另⼀个单独的标准java Properties⽂件中去。在XML⽂件中⽤${key}替换指定的properties⽂件中的值。这样的话,只需要对properties⽂件进
⾏修改,⽽不⽤对xml配置⽂件进⾏修改。
引⼊外部⽂件后,就可以在xml中⽤${key}替换指定的properties⽂件中的值,通常项⽬中都会将jdbc的配置放在properties⽂件中。
在启动容器时,初始化bean时,${key}就会替换成properties⽂件中的值。
*/
//存取properties配置⽂件key-value结果
private Properties props;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
throws BeansException {
super.processProperties(beanFactoryToProcess, props);
this.props = props;
}
public String getProperty(String key) {
return Property(key);
}
public String getProperty(String key, String defaultValue) {
return Property(key, defaultValue);
}
public Object setProperty(String key, String value) {
return this.props.setProperty(key, value);
}
}
@Controller
类风湿和风湿一样吗@RequestMapping("/")
public class TestController {
@Autowired
PropertyConfigurer propertyConfigurer;
@RequestMapping("/index.do")
public String getIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
Map map = new HashMap<String, Object>();
map.put("orderNo", "111");
String address1 = Property("static.picture.address1");
String resRoot = Property("resRoot");
String envName = Property("tion.username");
String keyzjbceshi = Property("keyceshi");
map.put("address1", address1);
map.put("resRoot", resRoot);
map.put("envName", envName);
如何配置jdk18环境变量map.put("keyzjbceshi", keyzjbceshi);
model.addAllAttributes(map);
return "index/index.ftl";
}
}
View Code
4、⽅式四:通过ClassPathResource类进⾏属性⽂件的读取使⽤
在线文档public class ReadPropertiesUtils1 {
private static final Logger LOGGER = Logger(ReadPropertiesUtils1.class);
private static Properties props = new Properties();
static {
ClassPathResource cpr = new ClassPathResource("conf/ref-system-relation.properties");// 会重新加载spring框架try {
清除定时器props.InputStream());
} catch (IOException exception) {
<("ReadPropertiesUtils1 IOException", exception);
}
}
private ReadPropertiesUtils1() {
}
public static String getValue(String key) {
return (String) (key);
}
public static void main(String[] args) {
System.out.println("static.picture.address1>>>"+ Value("static.picture.address1"));
System.out.println("static.picture.address2>>>"+ Value("static.picture.address2"));
}
}
View Code
5、⽅式五:通过ContextClassLoader进⾏属性⽂件的读取使⽤
public class ReadPropertiesUtils2 {
private static final Logger LOGGER = Logger(ReadPropertiesUtils2.class);
public static String getValue(String key) {
Properties properties = new Properties();
try {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("conf/ref-system-relation.properties"); properties.load(inputStream);
} catch (FileNotFoundException e) {
<("conf/web-sys-relation.properties⽂件没有到异常", e);
} catch (IOException e) {
<("IOException", e);
}
Property(key);
}
public static void main(String[] args) {
System.out.println("static.picture.address1>>>" + Value("static.picture.address1"));
System.out.println("static.picture.address2>>>" + Value("static.picture.address2"));
}
}
View Code
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论