从java类中的spring文件访问属性

从java类中的spring文件访问属性,java,spring,Java,Spring,我一直在假设,现在发现这是不正确的。我在spring上下文中有以下配置属性声明: <bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />

我一直在假设,现在发现这是不正确的。我在spring上下文中有以下配置属性声明:

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchContextAttributes" value="true" />
        <property name="contextOverride" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/app.properties</value>
            </list>
        </property>
    </bean>
 <context:property-placeholder location="classpath:your.properties" ignore-unresolvable="true"/>
当然,到处都有空指针异常。现在我要问的是如何从应用程序(应用程序的Java类部分)访问应用程序属性

下面有没有比这更好的方法(我不是说这不好)

应用程序上下文中:

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchContextAttributes" value="true" />
        <property name="contextOverride" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/app.properties</value>
            </list>
        </property>
    </bean>
 <context:property-placeholder location="classpath:your.properties" ignore-unresolvable="true"/>
其中,your.properties包含以下内容:

cities = my test string 
应用程序内上下文:

<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="searchContextAttributes" value="true" />
        <property name="contextOverride" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/app.properties</value>
            </list>
        </property>
    </bean>
 <context:property-placeholder location="classpath:your.properties" ignore-unresolvable="true"/>
其中,your.properties包含以下内容:

cities = my test string 

Spring
属性不会覆盖
系统
属性。这是另一种方式。您应该从
Spring
获取所有属性,而不是从
System.getProperties()
获取<代码>系统属性将覆盖同名的
Spring
属性。您正在设置的
SYSTEM\u PROPERTIES\u MODE\u OVERRIDE
表示,当您从
Spring
获取属性值时,
SYSTEM
属性将获胜

您希望将该值设置为
系统属性\u模式\u回退
。这是默认值,因此实际上不需要设置它

如果您考虑到这一点,@NimChimpsky为您提供了访问属性值的正确方法:

@Value("${nameFromPropertyFileOrSystemProperty}")
private String someThingFromProperty;

Spring
属性不会覆盖
系统
属性。这是另一种方式。您应该从
Spring
获取所有属性,而不是从
System.getProperties()
获取<代码>系统属性将覆盖同名的
Spring
属性。您正在设置的
SYSTEM\u PROPERTIES\u MODE\u OVERRIDE
表示,当您从
Spring
获取属性值时,
SYSTEM
属性将获胜

您希望将该值设置为
系统属性\u模式\u回退
。这是默认值,因此实际上不需要设置它

如果您考虑到这一点,@NimChimpsky为您提供了访问属性值的正确方法:

@Value("${nameFromPropertyFileOrSystemProperty}")
private String someThingFromProperty;

为什么不在需要的地方注入属性或使用utils呢?(我不知道为什么你会认为它们会覆盖系统属性——它们是完全不相关的。)为什么不在需要的地方注入属性或使用utils呢?(我不知道为什么你会认为它们会覆盖系统属性——它们完全不相关。)