Using Application Properties with Spring 3

I’m new to Spring and it can be a lot to take in. I was confused by properties and Spring. Not Spring configuration properties, but properties that are used in the application. I just wanted global access to my application properties. I might run into issues further down, like if I want to dynamically update properties, but so far, this is what I have.

1. In my app-config.xml Spring config file:

    
    <bean id="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:application.properties" />
    </bean>

2. In the components that I wanted to use properties (some are annotated with @Service and others with @Component) I used the @Value notation. In addition, there were a few where I wanted to set a default in case the property was missing. In this situation the property is really there just in case something needs to be changed w/o redeploying.

@Component
public class RestClient {
	@Value("#{appProperties['property.webservice.rest.timeout.seconds']}")
	private Integer timeout = 360;
	
	@Value("#{appProperties['property.webservice.rest.filecontenttype']}")
	private String fileContentType = HTTP.DEFAULT_CONTENT_TYPE;

	public void doSomething() {
		//...
	}
}

The above worked to read in the property and the assignment was used if the property is missing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s