[BAEL-16662] Move articles out of spring-core and into spring-di and … (#7750)
* [BAEL-16662] Move articles out of spring-core and into spring-di and spring-core-2 * [BAEL-16662] Fixed required config classes * [BAEL-16662] - Removed unused dependencies from spring-core
This commit is contained in:
committed by
Josh Cummings
parent
6ee7181125
commit
8d6c2dc2ed
@@ -1,3 +1,11 @@
|
||||
### Relevant Articles
|
||||
|
||||
- [The Spring @Qualifier Annotation](https://www.baeldung.com/spring-qualifier-annotation)
|
||||
- [Constructor Dependency Injection in Spring](http://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](http://www.baeldung.com/spring-inject-prototype-bean-into-singleton)
|
||||
- [@Lookup Annotation in Spring](http://www.baeldung.com/spring-lookup)
|
||||
- [Controlling Bean Creation Order with @DependsOn Annotation](http://www.baeldung.com/spring-depends-on)
|
||||
- [Unsatisfied Dependency in Spring](https://www.baeldung.com/spring-unsatisfied-dependency)
|
||||
- [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring)
|
||||
|
||||
@@ -9,18 +9,70 @@
|
||||
<name>spring-di</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<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>
|
||||
@@ -28,12 +80,12 @@
|
||||
<version>${annotation-api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- <!– test scoped –>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.springframework</groupId>-->
|
||||
<!-- <artifactId>spring-test</artifactId>-->
|
||||
<!-- <scope>test</scope>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -97,6 +149,14 @@
|
||||
<!-- 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.dependson.file.reader;
|
||||
|
||||
import com.baeldung.dependson.shared.File;
|
||||
|
||||
public class FileReader {
|
||||
|
||||
public FileReader(File file) {
|
||||
file.setText("read");
|
||||
}
|
||||
|
||||
public void readFile() {}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.dependson.file.writer;
|
||||
|
||||
import com.baeldung.dependson.shared.File;
|
||||
|
||||
|
||||
public class FileWriter {
|
||||
|
||||
public FileWriter(File file){
|
||||
file.setText("write");
|
||||
}
|
||||
|
||||
public void writeFile(){}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.dependson.shared;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class File {
|
||||
|
||||
private String text = "";
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = this.text+text;
|
||||
}
|
||||
}
|
||||
27
spring-di/src/main/java/com/baeldung/di/spring/Account.java
Normal file
27
spring-di/src/main/java/com/baeldung/di/spring/Account.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Account {
|
||||
|
||||
private String accountNumber;
|
||||
private String type;
|
||||
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
public void setAccountNumber(String accountNumber) {
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AccountService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AccountServiceImpl implements AccountService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AudioBookService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class AudioBookServiceImpl implements AudioBookService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AuthorService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class AuthorServiceImpl implements AuthorService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface BookService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class BookServiceImpl implements BookService {
|
||||
|
||||
@Autowired(required = false)
|
||||
private AuthorService authorService;
|
||||
|
||||
}
|
||||
4
spring-di/src/main/java/com/baeldung/di/spring/Foo.java
Normal file
4
spring-di/src/main/java/com/baeldung/di/spring/Foo.java
Normal file
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class Foo {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class FooProcessor {
|
||||
|
||||
private Foo foo;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface IService {
|
||||
public String serve();
|
||||
}
|
||||
19
spring-di/src/main/java/com/baeldung/di/spring/IndexApp.java
Normal file
19
spring-di/src/main/java/com/baeldung/di/spring/IndexApp.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class IndexApp {
|
||||
|
||||
private IService service;
|
||||
|
||||
public String getServiceValue() {
|
||||
return service.serve();
|
||||
}
|
||||
|
||||
public IService getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(IService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class IndexService implements IService {
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class InstanceServiceFactory {
|
||||
public IService getService(int number) {
|
||||
switch (number) {
|
||||
case 1:
|
||||
return new MessageService("Foo");
|
||||
case 0:
|
||||
return new IndexService();
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parameter " + number);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class MessageApp {
|
||||
|
||||
private IService iService;
|
||||
|
||||
public MessageApp(IService iService) {
|
||||
this.iService = iService;
|
||||
}
|
||||
|
||||
public String getServiceValue() {
|
||||
return iService.serve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class MessageService implements IService {
|
||||
|
||||
private String message;
|
||||
|
||||
public MessageService(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serve() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SpringBeansConfig {
|
||||
|
||||
@Bean
|
||||
public AudioBookService audioBookServiceGenerator() {
|
||||
return new AudioBookServiceImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Import({ SpringBeansConfig.class })
|
||||
@ComponentScan("com.baeldung.di.spring")
|
||||
public class SpringMainConfig {
|
||||
|
||||
@Bean
|
||||
public BookService bookServiceGenerator() {
|
||||
return new BookServiceImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SpringPersonService {
|
||||
|
||||
@Autowired
|
||||
private PersonDao personDao;
|
||||
|
||||
public PersonDao getPersonDao() {
|
||||
return personDao;
|
||||
}
|
||||
|
||||
public void setPersonDao(PersonDao personDao) {
|
||||
this.personDao = personDao;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
public class StaticServiceFactory {
|
||||
public static IService getService(int number) {
|
||||
switch (number) {
|
||||
case 1:
|
||||
return new MessageService("Foo");
|
||||
case 0:
|
||||
return new IndexService();
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parameter " + number);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private AccountService accountService;
|
||||
|
||||
public AccountService getAccountService() {
|
||||
return accountService;
|
||||
}
|
||||
|
||||
public void setAccountService(AccountService accountService) {
|
||||
this.accountService = accountService;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.methodinjections;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.methodinjections")
|
||||
|
||||
public class AppConfig {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.methodinjections;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Grader {
|
||||
|
||||
public String grade(Collection<Integer> marks) {
|
||||
|
||||
boolean result = marks.stream()
|
||||
.anyMatch(mark -> mark < 45);
|
||||
if (result) {
|
||||
return "FAIL";
|
||||
}
|
||||
return "PASS";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.methodinjections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("schoolNotification")
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class SchoolNotification {
|
||||
@Autowired
|
||||
Grader grader;
|
||||
|
||||
private String name;
|
||||
private Collection<Integer> marks;
|
||||
|
||||
public SchoolNotification(String name) {
|
||||
this.name = name;
|
||||
this.marks = new ArrayList<Integer>();
|
||||
}
|
||||
|
||||
public String addMark(Integer mark) {
|
||||
this.marks.add(mark);
|
||||
return this.grader.grade(this.marks);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Collection<Integer> getMarks() {
|
||||
return marks;
|
||||
}
|
||||
|
||||
public void setMarks(Collection<Integer> marks) {
|
||||
this.marks = marks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.methodinjections;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Lookup;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("studentBean")
|
||||
public class Student {
|
||||
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* Injects a prototype bean SchoolNotification into Singleton student
|
||||
*/
|
||||
@Lookup
|
||||
public SchoolNotification getNotification(String name) {
|
||||
// spring overrides and returns a SchoolNotification instance
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.methodinjections;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Lookup;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("studentService")
|
||||
public abstract class StudentServices {
|
||||
|
||||
private Map<String, SchoolNotification> notes = new HashMap<>();
|
||||
|
||||
@Lookup
|
||||
protected abstract SchoolNotification getNotification(String name);
|
||||
|
||||
public String appendMark(String name, Integer mark) {
|
||||
SchoolNotification notification = notes.computeIfAbsent(name, exists -> getNotification(name));
|
||||
return notification.addMark(mark);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
5
spring-di/src/main/java/com/baeldung/sample/Bar.java
Normal file
5
spring-di/src/main/java/com/baeldung/sample/Bar.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.sample;
|
||||
|
||||
public class Bar {
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
5
spring-di/src/main/java/com/baeldung/sample/Foo.java
Normal file
5
spring-di/src/main/java/com/baeldung/sample/Foo.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.sample;
|
||||
|
||||
public class Foo {
|
||||
|
||||
}
|
||||
5
spring-di/src/main/java/com/baeldung/sample/FooDAO.java
Normal file
5
spring-di/src/main/java/com/baeldung/sample/FooDAO.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.sample;
|
||||
|
||||
public class FooDAO {
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
public interface Formatter {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.sample;
|
||||
package com.baeldung.sample;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.baeldung.sample;
|
||||
|
||||
public class Bar {
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.baeldung.sample;
|
||||
|
||||
public class Foo {
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.baeldung.sample;
|
||||
|
||||
public class FooDAO {
|
||||
|
||||
}
|
||||
2
spring-di/src/main/resources/application.properties
Normal file
2
spring-di/src/main/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
||||
spring.profiles.active=prod
|
||||
|
||||
20
spring-di/src/main/resources/baeldung.xml
Normal file
20
spring-di/src/main/resources/baeldung.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<bean id="toyota" class="com.baeldung.constructordi.domain.Car">
|
||||
<constructor-arg index="0" ref="engine"/>
|
||||
<constructor-arg index="1" ref="transmission"/>
|
||||
</bean>
|
||||
|
||||
<bean id="engine" class="com.baeldung.constructordi.domain.Engine">
|
||||
<constructor-arg index="0" value="v4"/>
|
||||
<constructor-arg index="1" value="2"/>
|
||||
</bean>
|
||||
|
||||
<bean id="transmission" class="com.baeldung.constructordi.domain.Transmission">
|
||||
<constructor-arg value="sliding"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
22
spring-di/src/main/resources/beaninjectiontypes-context.xml
Normal file
22
spring-di/src/main/resources/beaninjectiontypes-context.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="exampleDAO" class="com.baeldung.beaninjection.ExampleDAOBean">
|
||||
<constructor-arg index="0" type="java.lang.String"
|
||||
value="Mandatory DAO Property X" />
|
||||
</bean>
|
||||
|
||||
<bean id="anotherSampleDAO" class="com.baeldung.beaninjection.AnotherSampleDAOBean">
|
||||
<constructor-arg index="0" type="java.lang.String"
|
||||
value="Mandatory DAO Property Y" />
|
||||
</bean>
|
||||
|
||||
<bean id="exampleService" class="com.baeldung.beaninjection.ExampleServiceBean">
|
||||
<constructor-arg index="0" ref="exampleDAO" />
|
||||
<property name="propertyX" value="Some Service Property X"></property>
|
||||
<property name="anotherSampleDAO" ref="anotherSampleDAO"></property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
15
spring-di/src/main/resources/beans.xml
Normal file
15
spring-di/src/main/resources/beans.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<context:component-scan base-package="com.baeldung.methodinjections" />
|
||||
|
||||
<bean id="schoolNotification" class="com.baeldung.methodinjections.SchoolNotification" scope="prototype"/>
|
||||
|
||||
<bean id="studentServices" class="com.baeldung.methodinjections.StudentServices" scope="prototype" >
|
||||
<lookup-method name="getNotification" bean="schoolNotification"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
<context:annotation-config/>
|
||||
|
||||
<bean id="student" class="com.baeldung.applicationcontext.Student" destroy-method="destroy">
|
||||
<property name="no" value="15"/>
|
||||
<property name="name" value="Tom"/>
|
||||
</bean>
|
||||
|
||||
<bean id="math" class="com.baeldung.applicationcontext.Course">
|
||||
<property name="name" value="math"/>
|
||||
</bean>
|
||||
|
||||
<bean name="teacher" class="com.baeldung.applicationcontext.Teacher"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="messageSource"
|
||||
class="org.springframework.context.support.ResourceBundleMessageSource">
|
||||
<property name="basenames">
|
||||
<list>
|
||||
<value>dialog/dialog</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1 @@
|
||||
message.value=Hello World
|
||||
43
spring-di/src/main/resources/com.baeldung.di.spring.xml
Normal file
43
spring-di/src/main/resources/com.baeldung.di.spring.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<bean id="indexService" class="com.baeldung.di.spring.IndexService" />
|
||||
|
||||
<bean id="messageService" class="com.baeldung.di.spring.MessageService">
|
||||
<constructor-arg value="${message.value}" />
|
||||
</bean>
|
||||
|
||||
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
|
||||
<property name="location" value="com.baeldung.di.spring.properties" />
|
||||
</bean>
|
||||
|
||||
<bean id="messageServiceFromStaticFactory" class="com.baeldung.di.spring.StaticServiceFactory"
|
||||
factory-method="getService">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexServiceFactory" class="com.baeldung.di.spring.InstanceServiceFactory" />
|
||||
|
||||
<bean id="messageServiceFromInstanceFactory" class="com.baeldung.di.spring.InstanceServiceFactory"
|
||||
factory-method="getService" factory-bean="indexServiceFactory">
|
||||
<constructor-arg value="1" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
|
||||
<property name="service" ref="indexService" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexAppWithStaticFactory" class="com.baeldung.di.spring.IndexApp">
|
||||
<property name="service" ref="messageServiceFromStaticFactory" />
|
||||
</bean>
|
||||
|
||||
<bean id="indexAppWithFactoryMethod" class="com.baeldung.di.spring.IndexApp">
|
||||
<property name="service" ref="messageServiceFromInstanceFactory" />
|
||||
</bean>
|
||||
|
||||
<bean id="messageWorldApp" class="com.baeldung.di.spring.MessageApp">
|
||||
<constructor-arg ref="messageService" />
|
||||
</bean>
|
||||
</beans>
|
||||
22
spring-di/src/main/resources/constructordi.xml
Normal file
22
spring-di/src/main/resources/constructordi.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
https://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="toyota" class="com.baeldung.constructordi.domain.Car">
|
||||
<constructor-arg index="0" ref="engine" />
|
||||
<constructor-arg index="1" ref="transmission" />
|
||||
</bean>
|
||||
|
||||
<bean id="engine"
|
||||
class="com.baeldung.constructordi.domain.Engine">
|
||||
<constructor-arg index="0" value="v4" />
|
||||
<constructor-arg index="1" value="2" />
|
||||
</bean>
|
||||
|
||||
<bean id="transmission"
|
||||
class="com.baeldung.constructordi.domain.Transmission">
|
||||
<constructor-arg value="sliding" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,23 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
|
||||
|
||||
<context:annotation-config/>
|
||||
|
||||
<bean id="articleWithSetterInjectionBean"
|
||||
class="com.baeldung.dependencyinjectiontypes.ArticleWithSetterInjection">
|
||||
<constructor-arg ref="textFormatterBean" />
|
||||
</bean>
|
||||
|
||||
<bean id="articleWithConstructorInjectionBean"
|
||||
class="com.baeldung.dependencyinjectiontypes.ArticleWithConstructorInjection">
|
||||
<constructor-arg ref="textFormatterBean" />
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="textFormatterBean" class="com.baeldung.dependencyinjectiontypes.TextFormatter">
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
3
spring-di/src/main/resources/dialog/dialog_en.properties
Normal file
3
spring-di/src/main/resources/dialog/dialog_en.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
hello=hello
|
||||
you=you
|
||||
thanks=thank {0}
|
||||
@@ -0,0 +1,3 @@
|
||||
hello=\u4f60\u597d
|
||||
you=\u4f60
|
||||
thanks=\u8c22\u8c22{0}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="singleTool" class="com.baeldung.factorybean.SingleToolFactory">
|
||||
<property name="factoryId" value="3001"/>
|
||||
<property name="toolId" value="1"/>
|
||||
</bean>
|
||||
|
||||
<bean id="nonSingleTool" class="com.baeldung.factorybean.NonSingleToolFactory">
|
||||
<property name="factoryId" value="3002"/>
|
||||
<property name="toolId" value="2"/>
|
||||
</bean>
|
||||
</beans>
|
||||
10
spring-di/src/main/resources/factorybean-spring-ctx.xml
Normal file
10
spring-di/src/main/resources/factorybean-spring-ctx.xml
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="tool" class="com.baeldung.factorybean.ToolFactory">
|
||||
<property name="factoryId" value="9090"/>
|
||||
<property name="toolId" value="1"/>
|
||||
</bean>
|
||||
</beans>
|
||||
15
spring-di/src/main/resources/injectiontypes.xml
Normal file
15
spring-di/src/main/resources/injectiontypes.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean name="ftpReader" class="com.baeldung.beaninjection.FtpReader">
|
||||
<constructor-arg value="localhost" />
|
||||
<constructor-arg value="21" />
|
||||
</bean>
|
||||
|
||||
<bean name="readerService" class="com.baeldung.beaninjection.ReaderService">
|
||||
<property name="ftpReader" ref="ftpReader" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
13
spring-di/src/main/resources/logback.xml
Normal file
13
spring-di/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
24
spring-di/src/main/resources/setterdi.xml
Normal file
24
spring-di/src/main/resources/setterdi.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
|
||||
|
||||
<bean id="toyota" class="com.baeldung.setterdi.domain.Car">
|
||||
<property name="engine" ref="engine" />
|
||||
<property name="transmission" ref="transmission" />
|
||||
<property name="trailer" ref="trailer" />
|
||||
</bean>
|
||||
|
||||
<bean id="engine" class="com.baeldung.setterdi.domain.Engine">
|
||||
<property name="type" value="v4" />
|
||||
<property name="volume" value="2" />
|
||||
</bean>
|
||||
|
||||
<bean id="transmission" class="com.baeldung.setterdi.domain.Transmission">
|
||||
<property name="type" value="sliding" />
|
||||
</bean>
|
||||
|
||||
<bean id="trailer" class="com.baeldung.setterdi.domain.Trailer">
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredType;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
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;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestAutowiredType.class)
|
||||
public class FieldAutowiredIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency fieldDependency;
|
||||
|
||||
@Test
|
||||
public void givenAutowired_WhenSetOnField_ThenDependencyResolved() {
|
||||
assertNotNull(fieldDependency);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredName;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
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;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestAutowiredName.class)
|
||||
public class FieldAutowiredNameIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ArbitraryDependency autowiredFieldDependency;
|
||||
|
||||
@Test
|
||||
public void givenAutowiredAnnotation_WhenOnField_ThenDependencyValid() {
|
||||
assertNotNull(autowiredFieldDependency);
|
||||
assertEquals("Arbitrary Dependency", autowiredFieldDependency.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import com.baeldung.configuration.ApplicationContextTestAutowiredQualifier;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(
|
||||
loader = AnnotationConfigContextLoader.class,
|
||||
classes = ApplicationContextTestAutowiredQualifier.class)
|
||||
public class FieldQualifierAutowiredIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("autowiredFieldDependency")
|
||||
private ArbitraryDependency fieldDependency1;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("anotherAutowiredFieldDependency")
|
||||
private ArbitraryDependency fieldDependency2;
|
||||
|
||||
@Test
|
||||
public void givenAutowiredQualifier_WhenOnField_ThenDep1Valid() {
|
||||
assertNotNull(fieldDependency1);
|
||||
assertEquals("Arbitrary Dependency", fieldDependency1.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAutowiredQualifier_WhenOnField_ThenDep2Valid() {
|
||||
assertNotNull(fieldDependency2);
|
||||
assertEquals("Another Arbitrary Dependency", fieldDependency2.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { TestConfig.class })
|
||||
public class CircularDependencyIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Bean
|
||||
public CircularDependencyA getCircularDependencyA() {
|
||||
return new CircularDependencyA();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CircularDependencyB getCircularDependencyB() {
|
||||
return new CircularDependencyB();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCircularDependency_whenSetterInjection_thenItWorks() {
|
||||
final CircularDependencyA circA = context.getBean(CircularDependencyA.class);
|
||||
|
||||
Assert.assertEquals("Hi!", circA.getCircB().getMessage());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.circulardependency;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = { "com.baeldung.circulardependency" })
|
||||
public class TestConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.baeldung.dependency"})
|
||||
public class ApplicationContextTestAutowiredName {
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.AnotherArbitraryDependency;
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestAutowiredQualifier {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency autowiredFieldDependency() {
|
||||
ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency();
|
||||
|
||||
return autowiredFieldDependency;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency anotherAutowiredFieldDependency() {
|
||||
ArbitraryDependency anotherAutowiredFieldDependency = new AnotherArbitraryDependency();
|
||||
|
||||
return anotherAutowiredFieldDependency;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.configuration;
|
||||
|
||||
import com.baeldung.dependency.ArbitraryDependency;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationContextTestAutowiredType {
|
||||
|
||||
@Bean
|
||||
public ArbitraryDependency autowiredFieldDependency() {
|
||||
ArbitraryDependency autowiredFieldDependency = new ArbitraryDependency();
|
||||
return autowiredFieldDependency;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user