无法在Spring中读取属性

时间:2016-04-20 01:07:33

标签: java spring spring-mvc properties-file

我在src/main/resources中有一个属性文件。

Spring 4.0和Java 1.8项目。

mock.properties

key=test

WEB-INF / web.xml中

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

WEB-INF / MVC-调度-servlet.xml中

<context:component-scan base-package="com.x.y.z.*">
</context:component-scan>
<context:annotation-config />
<context:spring-configured />
<context:property-placeholder location="classpath:mock.properties"
    order="-1" ignore-resource-not-found="true" ignore-unresolvable="true" />
<mvc:annotation-driven />

Controller.java ...我可以在控制器中访问属性键的值。

@Controller
@RequestMapping("/xyz")
public class EC2Controller {
@Value("${key}")
String v;
}

Helper.java ...无法访问任何属性。总是得到“null”

@Component
public class Helper {
@Value("${key}")
String v;
}

我也尝试过在论坛和spring docs上提出的各种选项,但帮助者只是无法阅读prop.I试过以下

@Configuration
@PropertySource("classpath:mock.properties")
public class Helper {
@Value("${key}")
String v;

@Bean
public static PropertySourcesPlaceholderConfigurer      
propertySourcesPlaceholderConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
}

@Configuration
@PropertySource("classpath:mock.properties")
public class Helper {
@Autowired
Environment env;
String v = env.getProperty("key");
}

无效。

2 个答案:

答案 0 :(得分:0)

答案取决于您是否使用&#34; springonfig.xml&#34;你在上面展示的。根据您的代码示例,看起来您不是(除非缺少某些内容)。

你的&#34; springonfig.xml&#34;包括 context:property-placeholder ,所以它应该足够了。但是如果您没有使用它,或者想要使用@Configuration(Java配置)类(您可以在同一个应用程序中混合使用xml和Java配置),那么您需要一个PropertySourcesPlaceHolderConfigurer来解析@Value中的$ {},例如:

 @Bean
 public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
 }

请参阅https://docs.spring.io/spring/docs/4.2.4.RELEASE/javadoc-api/org/springframework/context/annotation/PropertySource.html处的文档,其中说明:

  

为了使用PropertySource中的属性解析定义或@Value注释中的$ {...}占位符,必须注册一个PropertySourcesPlaceholderConfigurer ...

请注意,文档说明在使用XML中的context-propertyholder时会自动发生这种情况 - 但是如上所述,从您的代码示例中可以看出您是否正在使用它。

答案 1 :(得分:0)

您可以尝试使用此代码段,然后使用 $ {key} 注入值:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/demo.properties</value>
        </list>
    </property>
</bean>

或者这个,并使用#{app.key} 来注入价值:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/context/spring-util-3.2.xsd" profile="dev">    

    ...

    <util:properties id="app" location="app.properties" />

    ...
</beans>    

希望能帮到你。

相关问题