[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;
@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.
	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;
@PropertySource(value = { "classpath:app.properties" })
public class TestMe {
	private String me;
		public String getMe() {
		return me;

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


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;

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

	private void start() {

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">


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s