annotated Java Bean to read

This example is a little contrived in order to keep it simple. So far we have only covered Spring beans that are defined using the spring-config.xml.

In this example we will use annotations in the bean class declaration to identify it as a Spring bean and then tell Spring where to look for these beans.

This works well for singletons, but becomes a little more complicated for prototype (many) beans.


Create a simple Spring Bean that is declared using @Component. The bean reads a property file and returns the properties stored in the file.

First we will create a in our src/test/resources folder as follows Dunning

Next we will define a ConfigService bean. The role of this service is to read the config values from the and return then upon request.

There are Spring beans available to read from a Properties file – which we will cover later. For now, we will write some simple Java code in our bean to read from the properties file.

package com.skills421.examples.spring.service;

import java.util.Properties;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

public class ConfigService
private Properties configMap;
private String configfile;

public String getConfigfile()
return configfile;

public void setConfigfile(String configfile)
this.configfile = configfile;

public Properties getConfigMap()
configMap = new Properties();
catch(IOException e)

return configMap;

public String getProperty(String name)
return (String) this.getConfigMap().get(name);

Note the following:

  • the bean is declared using the @Component annotation – this means we do not need to declare it in the spring-config.xml
  • the bean id is provided inside the @Component annotation – “config”
  • the configMap is loaded lazily in the getter
  • the configFile itself is set using the @Value annotation

Now let’s write a JUnit test suite to read the config parameters. We simply print them out instead of checking their values which we would normally do in JUnit.

package com.skills421.example.spring;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.skills421.examples.spring.service.ConfigService;

public class TestConfig
private static AbstractApplicationContext context;

public static void setupAppContext()
context = new ClassPathXmlApplicationContext("spring-config.xml");

public static void closeAppContext()

public void printBlankLine()

public void testAllPropertiesFromConfigService()
ConfigService configService = context.getBean("config",ConfigService.class);

for(Object propKey : configService.getConfigMap().keySet())
String propName = (String) propKey;

System.out.println(propName+" = "+configService.getProperty(propName));



Finally, we need to write a simple spring-config.xml to tell the Spring framework where to look for our annotated beans.

<beans xmlns=""

<context:annotation-config />
<context:component-scan base-package="com.skills421.examples.spring.service" />



For completeness – here is the pom.xml file.

<project xmlns="" xmlns:xsi=""



<!-- Spring 3 dependencies -->


<!-- JUnit -->


Run the Tests

Run the JUnit Tests and we see the following output: = Skills421
company.url = = John Dunning

Leave a Reply

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

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

Up ↑

%d bloggers like this: