springmvc 加载.properties

来源:互联网 发布:祁东农村淘宝招募电话 编辑:程序博客网 时间:2024/05/19 17:04

这里简单最近springmvc  加载.properties的方法


1.新建test.properties

test=test

shuai=shuai



2.打开springmvc 配置文件spring-content.xml   。   如下添加此方法


  

<bean id="annotationPropertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:jdbc.properties</value><value>classpath:test.properties</value>        <value>classpath:log4j.properties</value></list></property></bean>



方法一:

在你引用的Controller方法里,引用

@Controller@RequestMapping(value = "/test")
//读取的是properties里的test        @Value("${test}")public String test; 
        @Value("${shuai}")public String shuai;  }

    这样就可以得到引用的值。


方法二:


新建文件TestA.java  

package com.web.api;import org.springframework.beans.factory.annotation.Value;
public class TestA {@Value("${shuai}")public String shuai;@Value("${test}")public String test;public String getShuai() {return shuai;}public void setShuai(String shuai) {this.shuai = shuai;}public String getTest() {return test;}public void setTest(String test) {this.test = test;}}


 指定路径com.web.api.TestA.   上面写过属性是test,这里也写test

<bean class="com.web.api.TestA" id="testA"><property name="test" value="${test}"  />                 <property name="shuai" value="${shuai}"  />        </bean>

在Controller里引用如何内容
@Controller@RequestMapping(value = "/test")//读取的是properties里的test       //@Resourceprivate TestA testA;}




然后在代码里

0 0