Merge pull request #7294 from juanvaccari/BAEL-2928

BAEL-2928 - Create new spring-di project with examples for Qualifier …
This commit is contained in:
Erik Pragt 2019-08-06 07:19:36 +10:00 committed by GitHub
commit a56d3b1e58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 230 additions and 0 deletions

View File

@ -676,6 +676,7 @@
<!-- <module>spring-data-rest-querydsl</module> --> <!-- BAEL-14304 -->
<module>spring-dispatcher-servlet</module>
<module>spring-drools</module>
<module>spring-di</module>
<module>spring-ehcache</module>
<!-- <module>spring-ejb</module> BAEL-14304 -->
@ -873,6 +874,7 @@
<module>spring-data-rest</module>
<module>spring-dispatcher-servlet</module>
<module>spring-drools</module>
<module>spring-di</module>
<module>spring-ehcache</module>
<module>spring-freemarker</module>
<module>persistence-modules/spring-hibernate-3</module>
@ -1345,6 +1347,7 @@
<module>spring-data-rest-querydsl</module>
<module>spring-dispatcher-servlet</module>
<module>spring-drools</module>
<module>spring-di</module>
<module>spring-ehcache</module>
<module>spring-ejb</module>
@ -1585,3 +1588,4 @@
</properties>
</project>

101
spring-di/pom.xml Normal file
View File

@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>spring-di</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${annotation-api.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${org.springframework.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>spring-di</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
<properties>
<start-class>org.baeldung.org.baeldung.sample.App</start-class>
<!-- Spring -->
<org.springframework.version>5.0.6.RELEASE</org.springframework.version>
<annotation-api.version>1.3.2</annotation-api.version>
</properties>
</project>

View File

@ -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();
}
}

View File

@ -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 {
}

View File

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

View File

@ -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";
}
}

View File

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

View File

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

View File

@ -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";
}
}

View File

@ -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();
}
}

View File

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

View File

@ -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();
}

View File

@ -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());
}
}