静态变量由@Value

时间:2017-04-10 09:06:51

标签: java spring

我的常量文件:

 @Component
    public class Constants {
        public static String ROOT_DOMAIN;
        @Value("${setting.rootDomain}")
        private void setRootDomain(String rootDomain) {
            ROOT_DOMAIN = rootDomain;
        }
    ...
    }

我的settings.properties:

setting.rootDomain=example.com

我的推荐课程和方法:

@Component
public class MyServiceClass
{
        public void doSomething()
        {
           System.out.println("Root is:" + Constants.ROOT_DOMAIN);
        }
}

但Root Domain即将为空

[更新] 我的spring-master.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:/META-INF/spring/local/settings.properties</value>
                <value>classpath*:/META-INF/spring/${DEPLOY_ENVIRONMENT}/settings.properties</value>                        
            </list>
        </property>

        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="searchSystemEnvironment" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    </bean>
    <context:component-scan base-package="com.myapp.app">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>  
    </context:component-scan>
    <context:component-scan base-package="com.myapp.app.dao.api" />

    <!-- Application Context Provider -->
    <bean id="appContextProvider" class="com.myapp.app.ApplicationContextProvider"/>

</beans>

ApplicationContextProvider类如下:

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

     private static ApplicationContext context;

     public static ApplicationContext getApplicationContext() {
         return context;
     }

     @Override
     public void setApplicationContext(ApplicationContext ctx) {
         context = ctx;
     }
}

所以是文件在classpath中

2 个答案:

答案 0 :(得分:3)

@Value()在bean的初始化时被调用,bean在初始化时不需要在初始化时被初始化,因此除非初始化bean,否则你将没有值

答案 1 :(得分:0)

我通过将 @DependsOn 注释添加到我的 annatated @Bean 中解决了这个问题,如此处所述Order of Configuration in SpringBoot