My Constants File:
@Component public class Constants { public static String ROOT_DOMAIN; @Value("${setting.rootDomain}") private void setRootDomain(String rootDomain) { ROOT_DOMAIN = rootDomain; } ... }
My settings.properties:
setting.rootDomain=example.com
My Referring Class and Method:
@Component public class MyServiceClass { public void doSomething() { System.out.println("Root is:" + Constants.ROOT_DOMAIN); } }
But Root Domain is coming as null
[update] My 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>
And the ApplicationContextProvider class is as follows:
@Component public class ApplicationContextProvider implements ApplicationContextAware { private static ApplicationContext context; public static ApplicationContext getApplicationContext() { return context; } @Override public void setApplicationContext(ApplicationContext ctx) { context = ctx; } }
So yes the files are in classpath
Advertisement
Answer
@Value() gets called on the initialization of the bean , the bean gets initialized on need not on the startup so you will not have the value unless the bean is initialized