我的 spring 控制器中的 @Value 不起作用

@Value in my spring controller does not work

我的控制器有

@Value("${myProp}")
private String myProp;

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

@RequestMapping(value = "myProp", method = RequestMethod.GET)
public @ResponseBody String getMyProp(){
    return "The prop is:" + myProp;
}

我的applicationcontext.xml

<bean id="appConfigProperties" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="location" value="classpath:MyApps-local.properties" />
</bean>

我得到以下异常:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'myProp' in string value "${myProp}"

注意:我的属性文件 MyApps-local.propertiesclasspath 中并且包含 myProp=delightful

任何帮助都会很棒....

仔细阅读这篇http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

对于开发模式使用特定于配置文件的属性

在基于XML的配置中你需要使用PropertyPlaceholderConfigurer bean

<beans xmlns="http://www.springframework.org/schema/beans" . . . >
  . . .
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:database.properties" />
    </bean>
  . . .
</beans>

但您只能在 xml 配置中使用 database.properties 中的值

<beans xmlns="http://www.springframework.org/schema/beans" . . >
  . . .
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
  . . .
</beans>

如果您想将 @Value annotation for bean's fields, you need to add @Confuguration and @PropertySource 注释内的属性文件中的值用于 bean class。像这样

@Configuration
@PropertySource("classpath:database.properties")
public class AppConfig {

    @Value("${jdbc.url}")
    private String url;

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

在您的配置中,您有两个 PropertySourcesPlaceholderConfigurer。使用 Java @Configuration 在 DispatcherServlet 中定义的第一个使用标准环境来查找属性。这意味着来自系统环境和环境变量的属性以及定义的@PropertySource。

这不知道你在applicationContext.xml文件中指定的MyApps-local.properties。 xml 文件中的第二个 PropertySourcesPlaceholderConfigurer 知道 MyApps-local.properties 文件,但它仅 post 处理根应用程序上下文中的占位符

bean 工厂 post 处理器在每个应用程序上下文中都有作用域。

更改您的 Web 应用程序上下文以指定 属性 source.This 会将文件中的属性添加到您的环境中

@Configuration
@PropertySource("classpath:MyApps-local.properties")
public class WebConfig{
   @Autowired
   private Environment env;

   @RequestMapping(value = "myProp", method = RequestMethod.GET)
   public @ResponseBody String getMyProp(){
       return "The prop is:" + env.getProperty("myProp");
   }
 }

在这种情况下,您不需要 PropertySourcesPlacheholder,因为您可以从环境中查询属性。然后保持 applicationContext.xml 不变

它还将与 PropertySourcesPlaceholder @Bean 一起工作,因为它还从环境中选择属性。不过上面比下面干净

@Configuration
@PropertySource("classpath:MyApps-local.properties")
public class WebConfig{

   @Value("${myProp}")
   private String myProp;

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

   @RequestMapping(value = "myProp", method = RequestMethod.GET)
   public @ResponseBody String getMyProp(){
     return "The prop is:" + myProp;
   }
}

看看这是否有帮助

  1. 您可以继续并在 xml 配置中使用此 util 标签

<util:properties id="appConfigProperties" location="location to your properties file" /> <context:property-placeholder properties-ref="appConfigProperties" />

  1. 来自架构 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

  2. 然后在你想要从 属性 文件中获取值的地方使用它作为

@Value("${myPropl}")
私有字符串 str;

对我有用,如果卡在什么地方请告诉我:)