【问题标题】:Unable to read properties in Spring无法在 Spring 中读取属性
【发布时间】:2016-08-12 10:34:27
【问题描述】:

我在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-dispatcher-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 文档中建议的各种选项,但助手无法阅读道具。我也尝试了以下操作

@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");
}

没有任何作用。

【问题讨论】:

  • 澄清一下:您的@Autowire 注释实际上是@Autowired(带有一个d),对吗?
  • 我有一个示例工作代码@github.com/shaimakh/so36732090
  • 嗨@Sanj..是的,我知道它在理想情况下应该如何工作。但我无法弄清楚我的应用程序出了什么问题。
  • 有趣。似乎您拥有所需的所有组件。呃,让我想起了我挣扎了很长时间以及试图让它发挥作用的糟糕回忆。
  • @WillMcavoy - 我从你的帖子中获取了所有信息,它正在工作。您可以在 github 上发布示例吗?

标签: java spring spring-mvc properties-file


【解决方案1】:

答案取决于您是否使用上面显示的 springonfig.xml mvc-dispatcher-servlet.xml。根据您的代码示例,您似乎不是(除非缺少某些内容)。

您的 mvc-dispatcher-servlet.xml 包含 context:property-placeholder,所以应该足够了。但是如果你不使用它,或者想使用@Configuration(Java config)类来代替(你可以在同一个应用程序中混合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:property-placeholder 时会自动发生这种情况 - 但如前所述,从您的代码示例中不清楚您是否正在使用它。

【讨论】:

  • 我认为PropertySourcesPlaceholderConfigurer&lt;context:property-placeholder... 相同,不同的是Java config 和xml
  • 正确 - 虽然从代码示例中不清楚是否在配置中使用了它(请参阅上面的注释)。
  • 我的错。我现在已经正确更新了问题。这是我拥有的唯一上下文文件,我相信它已经开始使用了。我的 REST 调用被正确映射到我的 @Controller 控制器也能够阅读属性。问题在于我的应用程序中的助手或任何其他类。
  • 我曾按照 Woodchuck 的建议尝试使用 PropertySourcesPlaceholderConfigurer。我之前忘记将其粘贴到我的问题中。它对我不起作用
【解决方案2】:

你可以试试这个代码 sn-p,并使用 ${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>    

希望能帮到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2019-03-15
    • 2020-02-14
    • 2020-07-22
    • 2015-12-23
    相关资源
    最近更新 更多