diff --git a/spring-autowire/pom.xml b/spring-autowire/pom.xml new file mode 100644 index 0000000000..e28efdae61 --- /dev/null +++ b/spring-autowire/pom.xml @@ -0,0 +1,63 @@ + + 4.0.0 + + com.baeldung + spring-autowire + 0.0.1-SNAPSHOT + jar + + spring-autowire + http://maven.apache.org + + + UTF-8 + 4.2.5.RELEASE + 3.5.1 + 2.19.1 + + + + + junit + junit + 4.11 + + + org.springframework + spring-core + ${org.springframework.version} + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-test + ${org.springframework.version} + test + + + + + spring-autowire + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/App.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/App.java new file mode 100644 index 0000000000..725a9b8406 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/App.java @@ -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(); + } +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/AppConfig.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/AppConfig.java new file mode 100644 index 0000000000..f948e2bf8e --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/AppConfig.java @@ -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 { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/Bar.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Bar.java new file mode 100644 index 0000000000..7aa820adef --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Bar.java @@ -0,0 +1,5 @@ +package com.baeldung.autowire.sample; + +public class Bar { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/BarFormatter.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/BarFormatter.java new file mode 100644 index 0000000000..fb704453fc --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/BarFormatter.java @@ -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"; + } + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/Foo.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Foo.java new file mode 100644 index 0000000000..b587ab38b8 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Foo.java @@ -0,0 +1,5 @@ +package com.baeldung.autowire.sample; + +public class Foo { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooDAO.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooDAO.java new file mode 100644 index 0000000000..aec26202ab --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooDAO.java @@ -0,0 +1,5 @@ +package com.baeldung.autowire.sample; + +public class FooDAO { + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooFormatter.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooFormatter.java new file mode 100644 index 0000000000..73966e9e43 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooFormatter.java @@ -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"; + } + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooService.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooService.java new file mode 100644 index 0000000000..eccea4351a --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FooService.java @@ -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(); + } + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/Formatter.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Formatter.java new file mode 100644 index 0000000000..83716d6e92 --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/Formatter.java @@ -0,0 +1,7 @@ +package com.baeldung.autowire.sample; + +public interface Formatter { + + String format(); + +} diff --git a/spring-autowire/src/main/java/com/baeldung/autowire/sample/FormatterType.java b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FormatterType.java new file mode 100644 index 0000000000..7ffc308c9a --- /dev/null +++ b/spring-autowire/src/main/java/com/baeldung/autowire/sample/FormatterType.java @@ -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(); + +} diff --git a/spring-autowire/src/test/java/com/baeldung/autowire/sample/FooServiceTest.java b/spring-autowire/src/test/java/com/baeldung/autowire/sample/FooServiceTest.java new file mode 100644 index 0000000000..4607f527d9 --- /dev/null +++ b/spring-autowire/src/test/java/com/baeldung/autowire/sample/FooServiceTest.java @@ -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()); + } +}