Merge pull request #9 from eugenp/master

update with origin
This commit is contained in:
Maiklins
2019-09-09 10:57:44 +02:00
committed by GitHub
parent db85c8f275
commit 621491f8db
20076 changed files with 1628788 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
RemoteSystemsTempFiles/
.classpath
.project
.settings/
bin/
.metadata/
docs/*.autosave
.recommenders/
build/
.gradle/
.DS_Store
.idea/
+22
View File
@@ -0,0 +1,22 @@
### Relevant Articles:
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)
- [Constructor Injection in Spring with Lombok](http://www.baeldung.com/spring-injection-lombok)
- [Introduction to Springs StreamUtils](http://www.baeldung.com/spring-stream-utils)
- [XML-Based Injection in Spring](http://www.baeldung.com/spring-xml-injection)
- [A Quick Guide to the Spring @Lazy Annotation](http://www.baeldung.com/spring-lazy-annotation)
- [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)
- [BeanNameAware and BeanFactoryAware Interfaces in Spring](http://www.baeldung.com/spring-bean-name-factory-aware)
- [Spring Injecting Collections](http://www.baeldung.com/spring-injecting-collections)
- [Access a File from the Classpath in a Spring Application](http://www.baeldung.com/spring-classpath-file-access)
- [Controlling Bean Creation Order with @DependsOn Annotation](http://www.baeldung.com/spring-depends-on)
- [Spring Autowiring of Generic Types](https://www.baeldung.com/spring-autowire-generics)
- [Spring Application Context Events](https://www.baeldung.com/spring-context-events)
- [Unsatisfied Dependency in Spring](https://www.baeldung.com/spring-unsatisfied-dependency)
- [What is a Spring Bean?](https://www.baeldung.com/spring-bean)
- [Spring PostConstruct and PreDestroy Annotations](https://www.baeldung.com/spring-postconstruct-predestroy)
- [Guice vs Spring Dependency Injection](https://www.baeldung.com/guice-spring-dependency-injection)
- [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring)
+96
View File
@@ -0,0 +1,96 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<groupId>com.baeldung</groupId>
<artifactId>spring-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-core</name>
<packaging>war</packaging>
<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>
</dependencies>
<build>
<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>
<properties>
<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,20 @@
package com.baeldung;
public class Ebook {
private int bookId;
private String bookTitle;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
}
@@ -0,0 +1,5 @@
package com.baeldung;
public interface EbookRepository {
String titleById(int id);
}
@@ -0,0 +1,12 @@
package com.baeldung;
import org.springframework.beans.factory.annotation.Autowired;
public class LibraryUtils {
@Autowired
private EbookRepository eBookRepository;
public String findBook(int id) {
return eBookRepository.titleById(id);
}
}
@@ -0,0 +1,20 @@
package com.baeldung;
public class Member {
private int memberId;
private String memberName;
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
}
@@ -0,0 +1,14 @@
package com.baeldung;
import org.springframework.beans.factory.annotation.Autowired;
public class Reservation {
private Member member;
private Ebook eBook;
@Autowired
public Reservation(Member member, Ebook eBook) {
this.member = member;
this.eBook = eBook;
}
}
@@ -0,0 +1,26 @@
package com.baeldung.annotation;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class MyBean {
private final TestBean testBean;
public MyBean(TestBean testBean) {
this.testBean = testBean;
System.out.println("Hello from constructor");
}
@PostConstruct
private void postConstruct() {
System.out.println("Hello from @PostConstruct method");
}
@PreDestroy
public void preDestroy() {
System.out.println("Bean is being destroyed");
}
}
@@ -0,0 +1,8 @@
package com.baeldung.annotation;
import org.springframework.stereotype.Component;
@Component
public class TestBean {
}
@@ -0,0 +1,21 @@
package com.baeldung.applicationcontext;
public class Course {
private String name;
public Course() {
}
public Course(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,43 @@
package com.baeldung.applicationcontext;
import java.util.Locale;
public class Dialog {
private Locale locale;
private String hello;
private String thanks;
public Dialog() {
}
public Dialog(Locale locale, String hello, String thanks) {
this.locale = locale;
this.hello = hello;
this.thanks = thanks;
}
public Locale getLocale() {
return locale;
}
public void setLocale(Locale locale) {
this.locale = locale;
}
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
public String getThanks() {
return thanks;
}
public void setThanks(String thanks) {
this.thanks = thanks;
}
}
@@ -0,0 +1,35 @@
package com.baeldung.applicationcontext;
public class Student {
private int no;
private String name;
public Student() {
}
public Student(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void destroy() {
System.out.println("Student(no: " + no + ") is destroyed");
}
}
@@ -0,0 +1,39 @@
package com.baeldung.applicationcontext;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
public class Teacher {
@Autowired
private ApplicationContext context;
private List<Course> courses = new ArrayList<>();
public Teacher() {
}
@PostConstruct
public void addCourse() {
if (context.containsBean("math")) {
Course math = context.getBean("math", Course.class);
courses.add(math);
}
if (context.containsBean("physics")) {
Course physics = context.getBean("physics", Course.class);
courses.add(physics);
}
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
}
}
@@ -0,0 +1,20 @@
package com.baeldung.aware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Gebruiker on 4/24/2018.
*/
public class AwareExample {
public static void main(String[] args) {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(Config.class);
MyBeanName myBeanName = context.getBean(MyBeanName.class);
MyBeanFactory myBeanFactory = context.getBean(MyBeanFactory.class);
myBeanFactory.getMyBeanName();
}
}
@@ -0,0 +1,18 @@
package com.baeldung.aware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean(name = "myCustomBeanName")
public MyBeanName getMyBeanName() {
return new MyBeanName();
}
@Bean
public MyBeanFactory getMyBeanFactory() {
return new MyBeanFactory();
}
}
@@ -0,0 +1,24 @@
package com.baeldung.aware;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
/**
* Created by Gebruiker on 4/25/2018.
*/
public class MyBeanFactory implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void getMyBeanName() {
MyBeanName myBeanName = beanFactory.getBean(MyBeanName.class);
System.out.println(beanFactory.isSingleton("myCustomBeanName"));
}
}
@@ -0,0 +1,11 @@
package com.baeldung.aware;
import org.springframework.beans.factory.BeanNameAware;
public class MyBeanName implements BeanNameAware {
@Override
public void setBeanName(String beanName) {
System.out.println(beanName);
}
}
@@ -0,0 +1,28 @@
package com.baeldung.beanfactory;
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
@@ -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,18 @@
package com.baeldung.collection;
/**
* Created by Gebruiker on 5/22/2018.
*/
public class BaeldungBean {
private String name;
public BaeldungBean(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
@@ -0,0 +1,50 @@
package com.baeldung.collection;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.*;
@Configuration
public class CollectionConfig {
@Bean
public CollectionsBean getCollectionsBean() {
return new CollectionsBean(new HashSet<>(Arrays.asList("John", "Adam", "Harry")));
}
@Bean
public List<String> nameList(){
return Arrays.asList("John", "Adam", "Harry", null);
}
@Bean
public Map<Integer, String> nameMap(){
Map<Integer, String> nameMap = new HashMap<>();
nameMap.put(1, "John");
nameMap.put(2, "Adam");
nameMap.put(3, "Harry");
return nameMap;
}
@Bean
@Qualifier("CollectionsBean")
@Order(2)
public BaeldungBean getElement() {
return new BaeldungBean("John");
}
@Bean
@Order(3)
public BaeldungBean getAnotherElement() {
return new BaeldungBean("Adam");
}
@Bean
@Order(1)
public BaeldungBean getOneMoreElement() {
return new BaeldungBean("Harry");
}
}
@@ -0,0 +1,21 @@
package com.baeldung.collection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Gebruiker on 5/18/2018.
*/
public class CollectionInjectionDemo {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(CollectionConfig.class);
CollectionsBean collectionsBean = context.getBean(CollectionsBean.class);
collectionsBean.printNameList();
collectionsBean.printNameSet();
collectionsBean.printNameMap();
collectionsBean.printBeanList();
collectionsBean.printNameListWithDefaults();
}
}
@@ -0,0 +1,62 @@
package com.baeldung.collection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
/**
* Created by Gebruiker on 5/18/2018.
*/
public class CollectionsBean {
@Autowired
private List<String> nameList;
private Set<String> nameSet;
private Map<Integer, String> nameMap;
@Autowired(required = false)
@Qualifier("CollectionsBean")
private List<BaeldungBean> beanList = new ArrayList<>();
@Value("${names.list:}#{T(java.util.Collections).emptyList()}")
private List<String> nameListWithDefaultValue;
public CollectionsBean() {
}
public CollectionsBean(Set<String> strings) {
this.nameSet = strings;
}
@Autowired
public void setNameMap(Map<Integer, String> nameMap) {
this.nameMap = nameMap;
}
public void printNameList() {
System.out.println(nameList);
}
public void printNameSet() {
System.out.println(nameSet);
}
public void printNameMap() {
System.out.println(nameMap);
}
public void printBeanList() {
System.out.println(beanList);
}
public void printNameListWithDefaults() {
System.out.println(nameListWithDefaultValue);
}
}
@@ -0,0 +1,31 @@
package com.baeldung.config.scope;
import java.util.function.Function;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import com.baeldung.scope.prototype.PrototypeBean;
import com.baeldung.scope.singletone.SingletonFunctionBean;
@Configuration
public class AppConfigFunctionBean {
@Bean
public Function<String, PrototypeBean> beanFactory() {
return name -> prototypeBeanWithParam(name);
}
@Bean
@Scope(value = "prototype")
public PrototypeBean prototypeBeanWithParam(String name) {
return new PrototypeBean(name);
}
@Bean
public SingletonFunctionBean singletonFunctionBean() {
return new SingletonFunctionBean();
}
}
@@ -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;
}
}
@@ -0,0 +1,16 @@
package com.baeldung.configuration;
import com.baeldung.dependency.ArbitraryDependency;
import com.baeldung.dependency.YetAnotherArbitraryDependency;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationContextTestInjectName {
@Bean
public ArbitraryDependency yetAnotherFieldInjectDependency() {
ArbitraryDependency yetAnotherFieldInjectDependency = new YetAnotherArbitraryDependency();
return yetAnotherFieldInjectDependency;
}
}
@@ -0,0 +1,22 @@
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 ApplicationContextTestInjectQualifier {
@Bean
public ArbitraryDependency defaultFile() {
ArbitraryDependency defaultFile = new ArbitraryDependency();
return defaultFile;
}
@Bean
public ArbitraryDependency namedFile() {
ArbitraryDependency namedFile = new AnotherArbitraryDependency();
return namedFile;
}
}
@@ -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 ApplicationContextTestInjectType {
@Bean
public ArbitraryDependency injectDependency() {
ArbitraryDependency injectDependency = new ArbitraryDependency();
return injectDependency;
}
}
@@ -0,0 +1,16 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
@Configuration
public class ApplicationContextTestResourceNameType {
@Bean(name = "namedFile")
public File namedFile() {
File namedFile = new File("namedFile.txt");
return namedFile;
}
}
@@ -0,0 +1,22 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.File;
@Configuration
public class ApplicationContextTestResourceQualifier {
@Bean(name = "defaultFile")
public File defaultFile() {
File defaultFile = new File("defaultFile.txt");
return defaultFile;
}
@Bean(name = "namedFile")
public File namedFile() {
File namedFile = new File("namedFile.txt");
return namedFile;
}
}
@@ -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,16 @@
package com.baeldung.definition;
import com.baeldung.definition.domain.Address;
import com.baeldung.definition.domain.Company;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackageClasses = Company.class)
public class Config {
@Bean
public Address getAddress() {
return new Address("High Street", 1000);
}
}
@@ -0,0 +1,14 @@
package com.baeldung.definition.domain;
import lombok.Data;
@Data
public class Address {
private String street;
private int number;
public Address(String street, int number) {
this.street = street;
this.number = number;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.definition.domain;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@Component
public class Company {
private Address address;
public Company(Address address) {
this.address = address;
}
}
@@ -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;
}
}
@@ -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;
}
@@ -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();
}
@@ -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,24 @@
package com.baeldung.event.listener;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
public class ContextEventListener {
@Order(2)
@EventListener
public void handleContextRefreshEvent(ContextStartedEvent ctxStartEvt) {
System.out.println("Context Start Event received.");
}
@Order(1)
@EventListener(classes = { ContextStartedEvent.class, ContextStoppedEvent.class })
public void handleMultipleEvents() {
System.out.println("Multi-event listener invoked");
}
}
@@ -0,0 +1,10 @@
package com.baeldung.event.listener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.baeldung.event.listener")
public class EventConfig {
}
@@ -0,0 +1,12 @@
package com.baeldung.event.listener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringRunner {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(EventConfig.class);
ctx.start();
}
}
@@ -0,0 +1,16 @@
package com.baeldung.factorybean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FactoryBeanAppConfig {
@Bean(name = "tool")
public ToolFactory toolFactory() {
ToolFactory factory = new ToolFactory();
factory.setFactoryId(7070);
factory.setToolId(2);
return factory;
}
}
@@ -0,0 +1,39 @@
package com.baeldung.factorybean;
import org.springframework.beans.factory.config.AbstractFactoryBean;
public class NonSingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;
private int toolId;
public NonSingleToolFactory() {
setSingleton(false);
}
@Override
public Class<?> getObjectType() {
return Tool.class;
}
@Override
protected Tool createInstance() throws Exception {
return new Tool(toolId);
}
public int getFactoryId() {
return factoryId;
}
public void setFactoryId(int factoryId) {
this.factoryId = factoryId;
}
public int getToolId() {
return toolId;
}
public void setToolId(int toolId) {
this.toolId = toolId;
}
}
@@ -0,0 +1,2 @@
### Relevant Articles:
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
@@ -0,0 +1,36 @@
package com.baeldung.factorybean;
import org.springframework.beans.factory.config.AbstractFactoryBean;
//no need to set singleton property because default value is true
public class SingleToolFactory extends AbstractFactoryBean<Tool> {
private int factoryId;
private int toolId;
@Override
public Class<?> getObjectType() {
return Tool.class;
}
@Override
protected Tool createInstance() throws Exception {
return new Tool(toolId);
}
public int getFactoryId() {
return factoryId;
}
public void setFactoryId(int factoryId) {
this.factoryId = factoryId;
}
public int getToolId() {
return toolId;
}
public void setToolId(int toolId) {
this.toolId = toolId;
}
}
@@ -0,0 +1,20 @@
package com.baeldung.factorybean;
public class Tool {
private int id;
public Tool() {
}
public Tool(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
@@ -0,0 +1,40 @@
package com.baeldung.factorybean;
import org.springframework.beans.factory.FactoryBean;
public class ToolFactory implements FactoryBean<Tool> {
private int factoryId;
private int toolId;
@Override
public Tool getObject() throws Exception {
return new Tool(toolId);
}
@Override
public Class<?> getObjectType() {
return Tool.class;
}
@Override
public boolean isSingleton() {
return false;
}
public int getFactoryId() {
return factoryId;
}
public void setFactoryId(int factoryId) {
this.factoryId = factoryId;
}
public int getToolId() {
return toolId;
}
public void setToolId(int toolId) {
this.toolId = toolId;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.lazy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
@Lazy
@Configuration
@ComponentScan(basePackages = "com.baeldung.lazy")
public class AppConfig {
@Lazy
@Bean
public Region getRegion(){
return new Region();
}
@Bean
public Country getCountry(){
return new Country();
}
}

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