Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
+15
View File
@@ -0,0 +1,15 @@
## Spring Dependency Injection
This module contains articles about dependency injection with Spring
### Relevant Articles
- [The Spring @Qualifier Annotation](https://www.baeldung.com/spring-qualifier-annotation)
- [Constructor Dependency Injection in Spring](https://www.baeldung.com/constructor-injection-in-spring)
- [Spring Autowiring of Generic Types](https://www.baeldung.com/spring-autowire-generics)
- [Guice vs Spring – Dependency Injection](https://www.baeldung.com/guice-spring-dependency-injection)
- [Injecting Prototype Beans into a Singleton Instance in Spring](https://www.baeldung.com/spring-inject-prototype-bean-into-singleton)
- [@Lookup Annotation in Spring](https://www.baeldung.com/spring-lookup)
- [Controlling Bean Creation Order with @DependsOn Annotation](https://www.baeldung.com/spring-depends-on)
- [Unsatisfied Dependency in Spring](https://www.baeldung.com/spring-unsatisfied-dependency)
- [Circular Dependencies in Spring](https://www.baeldung.com/circular-dependencies-in-spring)
+162
View File
@@ -0,0 +1,162 @@
<?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>
<name>spring-di</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-5</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>${javax.inject.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${mockito.spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<!-- 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>
<!-- &lt;!&ndash; test scoped &ndash;&gt;-->
<!-- <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>${maven-war-plugin.version}</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>
<mockito.spring.boot.version>1.4.4.RELEASE</mockito.spring.boot.version>
<javax.inject.version>1</javax.inject.version>
<guava.version>20.0</guava.version>
<commons.io.version>2.5</commons.io.version>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
<mockito.version>1.10.19</mockito.version>
<assertj.version>3.12.2</assertj.version>
</properties>
</project>
@@ -0,0 +1,29 @@
package com.baeldung.circulardependency;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class CircularDependencyA implements ApplicationContextAware, InitializingBean {
private CircularDependencyB circB;
private ApplicationContext context;
public CircularDependencyB getCircB() {
return circB;
}
@Override
public void afterPropertiesSet() throws Exception {
circB = context.getBean(CircularDependencyB.class);
}
@Override
public void setApplicationContext(final ApplicationContext ctx) throws BeansException {
context = ctx;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.circulardependency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CircularDependencyB {
private CircularDependencyA circA;
private String message = "Hi!";
@Autowired
public void setCircA(final CircularDependencyA circA) {
this.circA = circA;
}
public String getMessage() {
return message;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.constructordi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.baeldung.constructordi.domain.Engine;
import com.baeldung.constructordi.domain.Transmission;
@Configuration
@ComponentScan("com.baeldung.constructordi")
public class Config {
@Bean
public Engine engine() {
return new Engine("v8", 5);
}
@Bean
public Transmission transmission() {
return new Transmission("sliding");
}
}
@@ -0,0 +1,31 @@
package com.baeldung.constructordi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baeldung.constructordi.domain.Car;
public class SpringRunner {
public static void main(String[] args) {
Car toyota = getCarFromXml();
System.out.println(toyota);
toyota = getCarFromJavaConfig();
System.out.println(toyota);
}
private static Car getCarFromJavaConfig() {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
return context.getBean(Car.class);
}
private static Car getCarFromXml() {
ApplicationContext context = new ClassPathXmlApplicationContext("constructordi.xml");
return context.getBean(Car.class);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.constructordi.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Car {
private Engine engine;
private Transmission transmission;
@Autowired
public Car(Engine engine, Transmission transmission) {
this.engine = engine;
this.transmission = transmission;
}
@Override
public String toString() {
return String.format("Engine: %s Transmission: %s", engine, transmission);
}
}
@@ -0,0 +1,16 @@
package com.baeldung.constructordi.domain;
public class Engine {
private String type;
private int volume;
public Engine(String type, int volume) {
this.type = type;
this.volume = volume;
}
@Override
public String toString() {
return String.format("%s %d", type, volume);
}
}
@@ -0,0 +1,14 @@
package com.baeldung.constructordi.domain;
public class Transmission {
private String type;
public Transmission(String type) {
this.type = type;
}
@Override
public String toString() {
return String.format("%s", type);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.dependency;
import org.springframework.stereotype.Component;
@Component
public class AnotherArbitraryDependency extends ArbitraryDependency {
private final String label = "Another Arbitrary Dependency";
public String toString() {
return label;
}
}
@@ -0,0 +1,13 @@
package com.baeldung.dependency;
import org.springframework.stereotype.Component;
@Component(value = "autowiredFieldDependency")
public class ArbitraryDependency {
private final String label = "Arbitrary Dependency";
public String toString() {
return label;
}
}
@@ -0,0 +1,13 @@
package com.baeldung.dependency;
import org.springframework.stereotype.Component;
@Component
public class YetAnotherArbitraryDependency extends ArbitraryDependency {
private final String label = "Yet Another Arbitrary Dependency";
public String toString() {
return label;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.dependency.exception.app;
import com.baeldung.dependency.exception.repository.InventoryRepository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class PurchaseDeptService {
private InventoryRepository repository;
public PurchaseDeptService(@Qualifier("dresses") InventoryRepository repository) {
this.repository = repository;
}
}
@@ -0,0 +1,13 @@
package com.baeldung.dependency.exception.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.baeldung.dependency.exception")
public class SpringDependenciesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDependenciesExampleApplication.class, args);
}
}
@@ -0,0 +1,10 @@
package com.baeldung.dependency.exception.repository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
@Qualifier("dresses")
@Repository
public class DressRepository implements InventoryRepository {
}
@@ -0,0 +1,7 @@
package com.baeldung.dependency.exception.repository;
import org.springframework.stereotype.Repository;
@Repository
public interface InventoryRepository {
}
@@ -0,0 +1,9 @@
package com.baeldung.dependency.exception.repository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
@Qualifier("shoes")
@Repository
public class ShoeRepository implements InventoryRepository {
}
@@ -0,0 +1,16 @@
package com.baeldung.dependencyinjectiontypes;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ArticleFormatter {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("dependencyinjectiontypes-context.xml");
ArticleWithSetterInjection article = (ArticleWithSetterInjection) context.getBean("articleWithSetterInjectionBean");
String formattedArticle = article.format("This is a text !");
System.out.print(formattedArticle);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.dependencyinjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
public class ArticleWithConstructorInjection {
private TextFormatter formatter;
@Autowired
public ArticleWithConstructorInjection(TextFormatter formatter) {
this.formatter = formatter;
}
public String format(String text) {
return formatter.format(text);
}
}
@@ -0,0 +1,21 @@
package com.baeldung.dependencyinjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
public class ArticleWithSetterInjection {
private TextFormatter formatter;
public ArticleWithSetterInjection(TextFormatter formatter) {
this.formatter = formatter;
}
@Autowired
public void setTextFormatter(TextFormatter formatter) {
this.formatter = formatter;
}
public String format(String text) {
return formatter.format(text);
}
}
@@ -0,0 +1,8 @@
package com.baeldung.dependencyinjectiontypes;
public class TextFormatter {
public String format(String text) {
return text.toUpperCase();
}
}
@@ -0,0 +1,15 @@
package com.baeldung.dependencyinjectiontypes.annotation;
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;
@Target({ElementType.FIELD, ElementType.METHOD,
ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface CarQualifier {
}
@@ -0,0 +1,39 @@
package com.baeldung.dependencyinjectiontypes.app;
import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier;
import com.baeldung.dependencyinjectiontypes.model.Car;
import com.baeldung.dependencyinjectiontypes.model.CarHandler;
import com.baeldung.dependencyinjectiontypes.model.Motorcycle;
import com.baeldung.dependencyinjectiontypes.model.Vehicle;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.baeldung.dependencyinjectiontypes.model")
public class CustomConfiguration {
@Bean
@CarQualifier
public Car getMercedes() {
return new Car("E280", "Mercedes", "Diesel");
}
public static void main(String[] args) throws NoSuchFieldException {
ConfigurableApplicationContext context = SpringApplication.run(CustomConfiguration.class, args);
CarHandler carHandler = context.getBean(CarHandler.class);
carHandler.getVehicles().forEach(System.out::println);
}
@Bean
@CarQualifier
public Car getBmw() {
return new Car("M5", "BMW", "Petrol");
}
@Bean
public Motorcycle getSuzuki() {
return new Motorcycle("Yamaguchi", "Suzuki", true);
}
}
@@ -0,0 +1,25 @@
package com.baeldung.dependencyinjectiontypes.model;
public class Car extends Vehicle {
private String engineType;
public Car(String name, String manufacturer, String engineType) {
super(name, manufacturer);
this.engineType = engineType;
}
public String getEngineType() {
return engineType;
}
public void setEngineType(String engineType) {
this.engineType = engineType;
}
@Override
public String toString() {
return "Car{" +
"engineType='" + engineType + '\'' +
'}';
}
}
@@ -0,0 +1,26 @@
package com.baeldung.dependencyinjectiontypes.model;
import com.baeldung.dependencyinjectiontypes.annotation.CarQualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ResolvableType;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class CarHandler {
@Autowired
@CarQualifier
private List<Vehicle> vehicles;
public List<Vehicle> getVehicles() throws NoSuchFieldException {
ResolvableType vehiclesType = ResolvableType.forField(getClass().getDeclaredField("vehicles"));
System.out.println(vehiclesType);
ResolvableType type = vehiclesType.getGeneric();
System.out.println(type);
Class<?> aClass = type.resolve();
System.out.println(aClass);
return this.vehicles;
}
}
@@ -0,0 +1,25 @@
package com.baeldung.dependencyinjectiontypes.model;
public class Motorcycle extends Vehicle {
private boolean twoWheeler;
public Motorcycle(String name, String manufacturer, boolean twoWheeler) {
super(name, manufacturer);
this.twoWheeler = true;
}
public boolean isTwoWheeler() {
return twoWheeler;
}
public void setTwoWheeler(boolean twoWheeler) {
this.twoWheeler = twoWheeler;
}
@Override
public String toString() {
return "Motorcycle{" +
"twoWheeler=" + twoWheeler +
'}';
}
}
@@ -0,0 +1,28 @@
package com.baeldung.dependencyinjectiontypes.model;
public abstract class Vehicle {
private String name;
private String manufacturer;
public Vehicle(String name, String manufacturer) {
this.name = name;
this.manufacturer = manufacturer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getManufacturer() {
return manufacturer;
}
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.dependson;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.baeldung.dependson.config.Config;
import com.baeldung.dependson.file.processor.FileProcessor;
public class DriverApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
ctx.getBean(FileProcessor.class);
ctx.close();
}
}
@@ -0,0 +1,38 @@
package com.baeldung.dependson.config;
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.DependsOn;
import org.springframework.context.annotation.Lazy;
import com.baeldung.dependson.file.processor.FileProcessor;
import com.baeldung.dependson.file.reader.FileReader;
import com.baeldung.dependson.file.writer.FileWriter;
import com.baeldung.dependson.shared.File;
@Configuration
@ComponentScan("com.baeldung.dependson")
public class Config {
@Autowired
File file;
@Bean("fileProcessor")
@DependsOn({"fileReader","fileWriter"})
@Lazy
public FileProcessor fileProcessor(){
return new FileProcessor(file);
}
@Bean("fileReader")
public FileReader fileReader(){
return new FileReader(file);
}
@Bean("fileWriter")
public FileWriter fileWriter(){
return new FileWriter(file);
}
}
@@ -0,0 +1,19 @@
package com.baeldung.dependson.file.processor;
import org.springframework.beans.factory.annotation.Autowired;
import com.baeldung.dependson.file.reader.FileReader;
import com.baeldung.dependson.file.writer.FileWriter;
import com.baeldung.dependson.shared.File;
public class FileProcessor {
File file;
public FileProcessor(File file){
this.file = file;
if(file.getText().contains("write") && file.getText().contains("read")){
file.setText("processed");
}
}
}

Some files were not shown because too many files have changed in this diff Show More