有三种,都没测试
1 中#################
在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件
中的文件,进行键值对的注入,例子如下:
1 首先在applicationContext.xml中加入:
<beans xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
</beans>
的命名空间,然后
2
<util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" />
3 创建test.properties
abc=123
4
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/admin/images")
@Controller
public class ImageAdminController {
private String imageDir;
@Value("#{settings['test.abc']}")
public void setImageDir(String val) {
this.imageDir = val;
}
}
这样就将test.abc的值注入了imageDir中了
2 中##########################
<context:property-placeholder location="classpath*:sysconfig.properties"/>;
3 中 ###############直接用类
public class abcextends PropertyPlaceholderConfigurer {
private static HashMap<String, String> ctxPropertiesMap;
@Override
protected void processProperties(
ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
super.processProperties(beanFactory, props);
ctxPropertiesMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static String getProperty(String name) {
return ctxPropertiesMap.get(name);
}
public static boolean getPropertyMapKey(String key) {
if (ctxPropertiesMap.containsKey(key)) {
return true;
}
return false;
}
public static void writePropertyByKey(String key, String value) {
ctxPropertiesMap.put(key, value);
}
}
<bean class="sdewwwww">
<property name="locations">
<list>
<value>classpath:FrontService.properties</value>
<value>classpath:ResponseCode.properties</value>
</list>
</property>
</bean>