[Spring] Simple Example with @Value and annotation driven configuration (no xml)

This is an example of a simple maven Spring application run as a standalone application which demonstrates the use of properties files and annotation based configuration.

1. The Configuration Bean. Here are 2 important things:

  1. You need to provide basePackages to the package where your applicaiton resides. In particular, where you MainClass resides.
  2. You need to define the bean of type PropertySourcesPlaceholderConfigurer as shown below.
package com.vvirlan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@ComponentScan(basePackages = "com.vvirlan")
public class AppConfig {

	/*
	 * PropertySourcesPlaceHolderConfigurer Bean only required for @Value("{}")
	 * annotations. Remove this bean if you are not using @Value annotations for
	 * injecting properties.
	 */
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
}

2. The Class which contains the properties you want to load with @Value. Here the important thing is to provide the path to the properties file (for maven it must be under src/main/resources).

package com.vvirlan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = { "classpath:app.properties" })
public class TestMe {
	@Value("${me}")
	private String me;
		public String getMe() {
		return me;
	}
}

3. app.properties file itself (put under src/main/resources):

me=Vladimir

4. The MainClass. Here the important thing is to instantiate your MainClass in Spring fashion. If you do MainClass cl = new MainClass(); it just won’t work (the autowiring I mean). Don’t forget to close the context when you’re done.

package com.vvirlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MainClass {
	@Autowired
	private TestMe me;
	public static void main(String[] args) {
                AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		MainClass cl = context.getBean(MainClass.class);
		cl.start();
		context.close();
	}

	private void start() {
		System.out.println(me.getMe());
	}
}

5. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.vvirlan</groupId>
  <artifactId>springexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>

  <dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-core</artifactId>
  	<version>4.1.5.RELEASE</version>
  </dependency>
  <dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-beans</artifactId>
  	<version>4.1.5.RELEASE</version>
  </dependency>
  <dependency>
  	<groupId>org.springframework</groupId>
  	<artifactId>spring-context</artifactId>
  	<version>4.1.5.RELEASE</version>
  </dependency>
  </dependencies>
</project>
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