basics of spring autowire - first cut

This commit is contained in:
sivabalachandran 2016-05-15 17:47:30 -04:00
parent 3896065b80
commit 75d0eb499f
12 changed files with 188 additions and 0 deletions

63
spring-autowire/pom.xml Normal file
View File

@ -0,0 +1,63 @@
<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.baeldung</groupId>
<artifactId>spring-autowire</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-autowire</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-autowire</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.autowire.sample;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
FooService fooService = ctx.getBean(FooService.class);
fooService.doStuff();
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.autowire.sample;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.autowire.sample")
public class AppConfig {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.autowire.sample;
public class Bar {
}

View File

@ -0,0 +1,13 @@
package com.baeldung.autowire.sample;
import org.springframework.stereotype.Component;
@FormatterType("Bar")
@Component
public class BarFormatter implements Formatter {
public String format() {
return "bar";
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.autowire.sample;
public class Foo {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.autowire.sample;
public class FooDAO {
}

View File

@ -0,0 +1,13 @@
package com.baeldung.autowire.sample;
import org.springframework.stereotype.Component;
@FormatterType("Foo")
@Component
public class FooFormatter implements Formatter {
public String format() {
return "foo";
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.autowire.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FooService {
@Autowired
@FormatterType("Foo")
private Formatter formatter;
public String doStuff(){
return formatter.format();
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.autowire.sample;
public interface Formatter {
String format();
}

View File

@ -0,0 +1,17 @@
package com.baeldung.autowire.sample;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
@Qualifier
@Target({ElementType.FIELD, ElementType.METHOD,ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface FormatterType {
String value();
}

View File

@ -0,0 +1,22 @@
package com.baeldung.autowire.sample;
import 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 org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class, loader=AnnotationConfigContextLoader.class)
public class FooServiceTest {
@Autowired
FooService fooService;
@Test
public void whenFooFormatterType_thenReturnFoo(){
Assert.assertEquals("foo", fooService.doStuff());
}
}