学习之spring属性文件注入
2016-03-17 16:26
465 查看
package com.my.proper; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("configInfo") public class ConfigInfo { @Value("${pagesize}") private String pagesize; @Value("${aaa}") private String aaa; public String getAaa() { return aaa; } public void setAaa(String aaa) { this.aaa = aaa; } public String getPagesize() { return pagesize; } public void setPagesize(String pagesize) { this.pagesize = pagesize; } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <context:component-scan base-package="com.my.proper" /> <context:component-scan base-package="com.my.action" /> <!-- spring的属性加载器,加载properties文件中的属性 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:messages.properties</value> <value>classpath:messages2.properties</value> </list> </property> <property name="fileEncoding" value="utf-8" /> </bean> </beans>
public void propertyTest() { ConfigInfo config = (ConfigInfo) cx.getBean("configInfo"); System.out.println(config.getPagesize()); System.out.println(config.getAaa()); }
相关文章推荐
- Java流操作之转换流
- SpringMVC的拦截器Interceptor
- SpringMVC中的DispatcherServlet详解
- 【编程语言】Struts2 中 iterator 标签的用法
- 从java多线程内存模型说到ConcurrentHashMap
- Java JUC之Atomic系列12大类实例讲解和原理分解
- Java Collection
- Struts 上传和下载功能
- Java对象结构及大小计算
- Struts2 Convention 系列 1
- Java的类加载机制
- Maven包引用出现重复包,去重复包3种方法(javassist)
- Eclipse 启动不了 一闪而过
- 学习之spring自带缓存
- 将Eclipse中的项目,添加到GitHub上
- java获取文件md5码
- Java对象分配简要流程
- java反射的一些调试
- MyEclipse 每次保存都要building workspace的解决方法
- 【转】Java并发编程:深入剖析ThreadLocal