Reputation: 1354
I am trying to read properties from "file" but not able to achive same.
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ApplicationContext.xml
<context:annotation-config />
<context:component-scan base-package="com.jodo.image.*"></context:component-scan>
<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/db.properties"/>
<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/service.properties"/>
<util:properties id="systemPropertiesHolder" location="file:/usr/local/jodo/opt/cms-image/service.properties">
</util:properties>
Class ThreadFileImageUpload
public class ThreadFileImageUpload extends Thread {
private String rawImagePath;
@Value("#{systemPropertiesHolder.rawImagePath}")
public void setRawImagePath(String property){
rawImagePath = property;
}...
Result : rawImagePath is null
Class ThreadFileImageUpload
@Configuration
@ImportResource("classpath:properties-config.xml")
public class ThreadFileImageUpload extends Thread {
private @Value("${rawImagePath}") String rawImagePath;
properties-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/service.properties"/>
</beans>
Result : rawImagePath is null
Class ThreadFileImageUpload
@Configuration
@PropertySource("file:/usr/local/jodo/opt/cms-image/service.properties")
public class ThreadFileImageUpload extends Thread {
@Autowired
Environment environment;
private String rawImagePath;
public void run() {
if(environment == null){
logger.info("environment is NULL");
}
rawImagePath = this.environment.getProperty("rawImagePath");
...
Give java.lang.NullPointerException (environment itself is null)
Class ThreadFileImageUpload
public class ThreadFileImageUpload extends Thread {
@Value("${rawImagePath}")
private String rawImagePath;
Result : rawImagePath is null
ApplicationContext.xml (now placed all file in one property-placeholder)
<context:property-placeholder location="file:/usr/local/jodo/opt/cms-image/db.properties, file:/usr/local/jodo/opt/cms-image/service.properties"/>
Class ThreadFileImageUpload
public class ThreadFileImageUpload extends Thread {
@Value("${rawImagePath}")
private String rawImagePath;
Result : rawImagePath is null
class ThreadFileImageUpload
package com.jodo.image.util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ThreadFileImageUpload extends Thread {
@Value("#{systemPropertiesHolder.rawImagePath}")
private String rawImagePath;
AppliactionContext.xml
<context:annotation-config />
<context:component-scan base-package="com.jodo.image.*"></context:component-scan>
<util:properties id="systemPropertiesHolder" location="file:/usr/local/jodo/opt/cms-image/service.properties">
</util:properties>
Still getting it null
I am sure that property file has required field
[root@localhost /]# cat /usr/local/jodo/opt/cms-image/service.properties
rawImagePath=/var/jodo-images/raw/
I am still stuck in same place.
Upvotes: 1
Views: 2875
Reputation: 4523
To make @Value work, ThreadFileImageUpload instance must be a Spring managed component. Please note that instances created using "new" are not managed by Spring. The reference manual here and here may help you to understand Spring beans and component scanning.
Upvotes: 1
Reputation: 64039
Whevever you inject properties using @Value, the class your are injecting into needs to be a Spring bean. To make that happen you need to either annotate ThreadFileImageUpload with @Component and make sure that the class in a package that is within the component scan, or you need to add that bean manually (in this case into ApplicationContext.xml)
Upvotes: 3
Reputation: 22506
I think you're missing the name(root) of the properties. See this http://forum.spring.io/forum/spring-projects/container/61645-value-and-propertyplaceholderconfigurer
If the file is service.properties
and you have
`<util:properties id="service" location="classpath:com/acme/app/service.properties"/>`
then try @Value("#{service.rawImagePath}")
Upvotes: 1