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.
Example
Create a simple Spring Bean that is declared using @Component. The bean reads a property file and returns the properties stored in the file.
config.properties
First we will create a config.properties in our src/test/resources folder as follows
company.name=Skills421 company.url=www.skills421.com company.contact=John Dunning
ConfigService.java
Next we will define a ConfigService bean. The role of this service is to read the config values from the config.properties 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.io.IOException; import java.util.Properties; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("config") public class ConfigService { private Properties configMap; private String configfile; public String getConfigfile() { return configfile; } @Value("config.properties") public void setConfigfile(String configfile) { this.configfile = configfile; } public Properties getConfigMap() { if(configMap==null) { configMap = new Properties(); try { configMap.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(configfile)); } catch(IOException e) { e.printStackTrace(); } } 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
TestConfig.java
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 org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.skills421.examples.spring.service.ConfigService; public class TestConfig { private static AbstractApplicationContext context; @BeforeClass public static void setupAppContext() { context = new ClassPathXmlApplicationContext("spring-config.xml"); } @AfterClass public static void closeAppContext() { if(context!=null) { context.close(); } } @After public void printBlankLine() { System.out.println(); } @Test 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)); } } }
spring-config.xml
Finally, we need to write a simple spring-config.xml to tell the Spring framework where to look for our annotated beans.
<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" 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.xsd"> <context:annotation-config /> <context:component-scan base-package="com.skills421.examples.spring.service" /> </beans>
pom.xml
For completeness – here is the pom.xml file.
<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.skills421.examples.spring</groupId> <artifactId>Spring3.2.6.Demo</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.6.RELEASE</spring.version> </properties> <dependencies> <!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> </project>
Run the Tests
Run the JUnit Tests and we see the following output:
company.name = Skills421 company.url = www.skills421.com company.contact = John Dunning