[Starter] Spring Runtime autowiring (Implicit bean discovery) – non boot

Here is a starter for a Spring 4 + Maven application that makes use of implicit bean discovery and loads some data from a properties file:

1. The bean. Make sure to:

  • create a default constructor.
  • add the @Component annotation denoting that it will be a bean
package com.vvirlan.spring.examples;
import org.springframework.stereotype.Component;

@Component
public class BlankDisc {
	private String name;
	private String title;

	public BlankDisc(){}

	public BlankDisc(String name, String title) {
		this.name = name;
		this.title = title;
	}
	
	public void play() {
		System.out.println("Name: "+name+" Title: "+title);
	}
}

2. Define the Configuration java class. Here make sure to:

  • Add the @Configuration annotation which says that this class will be used for bean configuration
  • @PropertySoource which says where to find you properties file
  • @ComponentScan saying that configuration is done by automatically scanning all the classes which have the @Component annotation
  • @Bean annotation marking this method as a bean (it will return a bean of the specified type.
package com.vvirlan.spring.examples;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:app.properties")
@ComponentScan
public class ExpressiveConfig {

	@Autowired
	Environment env;
	@Bean
	public BlankDisc disc() {
		return new BlankDisc(env.getProperty("disc.title"),
				env.getProperty("disc.artist"));
	}
}

3. app.properties with the contents:

disc.title=The Title
disc.artist=The Name Artist

4. And a JUnit test. The important parts here:

  • @RunWith(SpringJUnit4ClassRunner.class)
  • @ContextConfiguration(classes=ExpressiveConfig.class) – specifies which class is used for bean configuration
import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.vvirlan.spring.examples.BlankDisc;
import com.vvirlan.spring.examples.ExpressiveConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ExpressiveConfig.class)
public class TestDisc {
	@Autowired
	BlankDisc disc;
	
	@Test
	public void test() {
		disc.play();
	}
}

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>org.springframework.samples</groupId>
  <artifactId>RuntimeAutowiring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
		<!-- Generic properties -->
		<java.version>1.6</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

		<!-- Spring -->
		<spring-framework.version>3.2.3.RELEASE</spring-framework.version>

		<!-- Hibernate / JPA -->
		<hibernate.version>4.2.1.Final</hibernate.version>

		<!-- Logging -->
		<logback.version>1.0.13</logback.version>
		<slf4j.version>1.7.5</slf4j.version>

		<!-- Test -->
		<junit.version>4.11</junit.version>

	</properties>
	
	<dependencies>
		<!-- Spring and Transactions -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>

		<!-- Logging with SLF4J & LogBack -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
			<scope>runtime</scope>
		</dependency>

		<!-- Hibernate -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		
		<!-- Test Artifacts -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring-framework.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>	
</project>

6. Directory structure

Screenshot_2015-07-30_22-37-14

Advertisements

One thought on “[Starter] Spring Runtime autowiring (Implicit bean discovery) – non boot

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