diff --git a/pom.xml b/pom.xml index 451b5b4961..aabede4f3a 100644 --- a/pom.xml +++ b/pom.xml @@ -676,6 +676,7 @@ spring-dispatcher-servlet spring-drools + spring-di spring-ehcache @@ -873,6 +874,7 @@ spring-data-rest spring-dispatcher-servlet spring-drools + spring-di spring-ehcache spring-freemarker persistence-modules/spring-hibernate-3 @@ -1345,6 +1347,7 @@ spring-data-rest-querydsl spring-dispatcher-servlet spring-drools + spring-di spring-ehcache spring-ejb @@ -1585,3 +1588,4 @@ + diff --git a/spring-di/pom.xml b/spring-di/pom.xml new file mode 100644 index 0000000000..62456ba31c --- /dev/null +++ b/spring-di/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + spring-di + 1.0-SNAPSHOT + war + + + parent-boot-2 + com.baeldung + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + + org.springframework + spring-context + + + + javax.annotation + javax.annotation-api + ${annotation-api.version} + + + + + org.springframework + spring-test + test + + + + + + + + org.springframework + spring-framework-bom + ${org.springframework.version} + pom + import + + + org.springframework + spring-core + ${org.springframework.version} + + + + + + spring-di + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-war-plugin + 3.2.2 + + false + + + + + + + + dev + + true + + + dev + + + + prod + + prod + + + + + + org.baeldung.org.baeldung.sample.App + + 5.0.6.RELEASE + 1.3.2 + + + \ No newline at end of file diff --git a/spring-di/src/main/java/org/baeldung/sample/App.java b/spring-di/src/main/java/org/baeldung/sample/App.java new file mode 100644 index 0000000000..17fc49fc8c --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/App.java @@ -0,0 +1,11 @@ +package org.baeldung.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-di/src/main/java/org/baeldung/sample/AppConfig.java b/spring-di/src/main/java/org/baeldung/sample/AppConfig.java new file mode 100644 index 0000000000..8a177d2611 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/AppConfig.java @@ -0,0 +1,10 @@ +package org.baeldung.sample; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan("org.baeldung.sample") +public class AppConfig { + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/Bar.java b/spring-di/src/main/java/org/baeldung/sample/Bar.java new file mode 100644 index 0000000000..54e8d54418 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/Bar.java @@ -0,0 +1,5 @@ +package org.baeldung.sample; + +public class Bar { + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/BarFormatter.java b/spring-di/src/main/java/org/baeldung/sample/BarFormatter.java new file mode 100644 index 0000000000..8396653970 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/BarFormatter.java @@ -0,0 +1,13 @@ +package org.baeldung.sample; + +import org.springframework.stereotype.Component; + +@FormatterType("Bar") +@Component +public class BarFormatter implements Formatter { + + public String format() { + return "bar"; + } + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/Foo.java b/spring-di/src/main/java/org/baeldung/sample/Foo.java new file mode 100644 index 0000000000..562a00121a --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/Foo.java @@ -0,0 +1,5 @@ +package org.baeldung.sample; + +public class Foo { + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/FooDAO.java b/spring-di/src/main/java/org/baeldung/sample/FooDAO.java new file mode 100644 index 0000000000..151c0c38de --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/FooDAO.java @@ -0,0 +1,5 @@ +package org.baeldung.sample; + +public class FooDAO { + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/FooFormatter.java b/spring-di/src/main/java/org/baeldung/sample/FooFormatter.java new file mode 100644 index 0000000000..68cb7f81f2 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/FooFormatter.java @@ -0,0 +1,13 @@ +package org.baeldung.sample; + +import org.springframework.stereotype.Component; + +@FormatterType("Foo") +@Component +public class FooFormatter implements Formatter { + + public String format() { + return "foo"; + } + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/FooService.java b/spring-di/src/main/java/org/baeldung/sample/FooService.java new file mode 100644 index 0000000000..711711f205 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/FooService.java @@ -0,0 +1,17 @@ +package org.baeldung.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-di/src/main/java/org/baeldung/sample/Formatter.java b/spring-di/src/main/java/org/baeldung/sample/Formatter.java new file mode 100644 index 0000000000..ab29c2b848 --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/Formatter.java @@ -0,0 +1,7 @@ +package org.baeldung.sample; + +public interface Formatter { + + String format(); + +} diff --git a/spring-di/src/main/java/org/baeldung/sample/FormatterType.java b/spring-di/src/main/java/org/baeldung/sample/FormatterType.java new file mode 100644 index 0000000000..a00a9750bf --- /dev/null +++ b/spring-di/src/main/java/org/baeldung/sample/FormatterType.java @@ -0,0 +1,17 @@ +package org.baeldung.sample; + +import org.springframework.beans.factory.annotation.Qualifier; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Qualifier +@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER }) +@Retention(RetentionPolicy.RUNTIME) +public @interface FormatterType { + + String value(); + +} diff --git a/spring-di/src/test/java/org/baeldung/sample/FooServiceIntegrationTest.java b/spring-di/src/test/java/org/baeldung/sample/FooServiceIntegrationTest.java new file mode 100644 index 0000000000..6b518395a1 --- /dev/null +++ b/spring-di/src/test/java/org/baeldung/sample/FooServiceIntegrationTest.java @@ -0,0 +1,22 @@ +package org.baeldung.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 FooServiceIntegrationTest { + + @Autowired + FooService fooService; + + @Test + public void whenFooFormatterType_thenReturnFoo() { + Assert.assertEquals("foo", fooService.doStuff()); + } +}