I've searched stackoverflow, read docs and can not seem to get my #Value("${myproperty.value}") to give me anything other than null.
I have some beans that I have defined in my spring-servlet.xml as well the properties file.
<!-- Load properties files -->
<context:property-placeholder location="classpath:MyProperties.properties" ignore-unresolvable="true" />
The properties file gets loaded without errors. In the same xml I have defined a bean.
<bean id="pushNotification" class="com.mydomian.actions.MyClass"/>
In the bean I have properties that use the #Value.
private #Value("${some.property}") String propertyValue;
My properties are always null.
You need to declare
<context:annotation-config />
to enable annotation processing.
Related
I need some help on injecting property value to a bean which is defined outside the web application.
The web application has a property file under src/main/resource.The spring application context xml has the property place holder defined as
<context:property-placeholder
location="classpath:test.properties,file:/etc/test1.properties"
ignore-resource-not-found="true"
/>
where test1.properties is another file which resides outside the application.The bean is injected with the property which is defined in the application (test.properties) ,but I want to inject the property that is defined in test1.properties (ideally the idea is to override the property values from application and read the one defined outside the application).
Thanks.
Hi use like below in applicationContext.xml
<util:properties id="property" location="classpath:test.properties"/>
In Java,
#Autowired
protected Properties property;
I guess this is what you are looking for
<context:property-placeholder location="file:c:/kp/sec.properties" order="1" ignore-resource-not-found="true" ignore-unresolvable="true" />
<context:property-placeholder location="classpath:kp-props.properties" order="2" />
If the file sec.properties exists take the value from sec.properties, if file or properties does not exist take the property from kp-props.properties file from resources directory(if the property is not found in either of place application will fail)
And say you have property my.prop and you can inject the property as follows.
#Component
public class KPProps {
#Value("${my.prop}")
private int props;
public void print(){
System.out.println(props);
}
}
I have the following class:
#Component
public class MyClass {
#Value("${main.url}") private String mainUrl;
the following XML context:
<context:annotation-config/>
<context:component-scan base-package="mypackage"/>
<context:property-placeholder file-encoding="UTF-8" location="classpath:/app.properties" ignore-unresolvable="true"/>
and prop file:
main.url=veryniceurl.com
Injection doesn't work, it is always null.
I read a lot of similar examples and I thought that everything is ok but it isn't. Can anyone tell me if I forgot about something? I'm working with Mule ESB.
#Value doesn't seem to work with Mule. Instead you need to wire it up through the Mule XML, where I assume you are loading your component as a Spring Bean:
<spring:bean id="MyClass" class="com.example.MyClass">
<spring:property name="mainUrl" value="${main.url}"/>
</spring:bean>
Give an id to your properties and use this syntax :
#Value("#{jetProperties['jetBean.name']}")
<!-- define the properties file to use -->
<util:properties id="jetProperties" location="classpath:/jet.properties" />
From http://chrislovecnm.com/2010/03/08/spring-3-java-based-configuration-with-value/
Did you add the context placeholder in the dispatcher-servlet.xml ? As per here, Spring #Value annotation in #Controller class not evaluating to value inside properties file they seem to have solved it by adding it there instead of the application context
I wish to expose a Properties Spring bean whose values have been expanded via the typical property expansion mechanism. I'm using Spring 3.1. Let me digress.
Given the following properties file:
server.host=myhost.com
service.url=http://${server.host}/some/endpoint
And this portion of Spring XML config file:
<bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:default.properties</value>
</list>
</property>
</bean>
<context:property-placeholder properties-ref="appProperties" />
I can write the following working code:
#Component
public class MyComponent {
#Autowired
#Qualifier("appProperties")
private Properties appProperties;
#Value("${service.url}")
private String serviceUrl;
// remainder omitted
}
The only problem is that if I obtain the service.url value from appProperties I get http://${server.host}/some/endpoint - ie the value is unexpanded. However, if I get the value of service.url from serviceUrl, the value has been expanded: http://myhost.com/some/endpoint.
Does anyone know of a good way to expose a Properties instance as a Spring bean whose values have been expanded?
Alternatively, if anyone can point me to a Spring bean (must be Spring 3.1) that will do the expansion for me, I'll accept this too! (Interestingly, if you manually pull the property values from the Environment or PropertySource you'll find that these too are unexpanded.)
Thanks,
Muel.
I'm pretty late to this, but it's been viewed enough times that i figured it warranted a quick response. What you're seeing is an artifact of how Spring handles property expansion. When the need for property expansion is found, only previously loaded sources are checked, not the currently loading property source. When the file loads, there are no previous sources, so ${server.host} does not expand. When you later reference ${server.url} via the #Value annotation, the property file source is loaded and can be searched as you expected. This is why the #Value annotation gets full expansion but the result queried from the property file does not.
I have a Spring application context file that imports several other resources. However some of the resources in the imported files have similar names for example include1.xml has something like
<bean id="MyBean" class="...">
...
</bean>
The same bean id is used in include2.xml. Is there a way to set a prefix to the included beans or is there a way so restrict the scope of the included resource. For example something like.
<import resource="include1.xml" prefix="foo"/>
<import resource="include2.xml" prefix="bar"/>
Now in the parent file I can refer to foo.MyBean and bar.MyBean. If no such system exists is there any way to restrict scope so there is no bean id collisions, what is the best practice here?
No, there is no way to namespace the beans based on a file(beans defined later with the same name will override the one's defined earlier) however, you have the freedom to give them your own "name" - so potentially you can name all beans in your foo file:
<bean name="foo.bean1" class=../>
<bean name="foo.bean2" class=../>
and in your bar file, thus namespacing them manually:
<bean name="bar.bean1" class=../>
<bean name="bar.bean2" class=../>
My servlet.xml file holds all my spring configuration related information like datasource bean etc.
<bean id="..." class="...">
</bean>
Now my application has other settings that I need to save in a configuration file, is it possible to create my own settings in here or is there a better way?
I want something that loads up once and is very fast to reference in my project.
I need this to store some file paths, and other database settings for things like mongodb etc.
You can use .properties file:
<context:property-placeholder location="file:///my/cfg.properties"/>
If the file contents are:
driver=com.mysql.jdbc.Driver
dbname=mysql:mydb
mysetting=42
You can reference them in Spring XML like this:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>
Reference: 4.8.2.1 Example: the PropertyPlaceholderConfigurer.
You can also inject these properties into your own classes:
#Service
public class MyService {
#Value("${mysetting}")
private int mysetting; //Spring will inject '42' on bean creation
//...
}
Of course you can also use setter-injection like in the example with DriverManagerDataSource if you prefer XML.
Also have a look at: Spring 3.1 M1: Unified Property Management.