[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:
Sam Millington 2019-09-16 11:00:12 +01:00 committed by Josh Cummings
parent 6ee7181125
commit 8d6c2dc2ed
139 changed files with 787 additions and 320 deletions

View File

@ -1,3 +1,6 @@
## Relevant Articles:
- [Understanding getBean() in Spring](https://www.baeldung.com/spring-getbean)
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
- [Spring Injecting Collections](http://www.baeldung.com/spring-injecting-collections)

View File

@ -14,6 +14,11 @@
</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>

View File

@ -1,22 +1,12 @@
### 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)

View File

@ -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)

View File

@ -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>
<!-- &lt;!&ndash; test scoped &ndash;&gt;-->
<!-- <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>

View File

@ -1,31 +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);
}
}
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);
}
}

View File

@ -1,21 +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);
}
}
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);
}
}

View File

@ -1,27 +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;
}
}
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;
}
}

View File

@ -1,5 +1,5 @@
package com.baeldung.di.spring;
public interface AccountService {
}
package com.baeldung.di.spring;
public interface AccountService {
}

View File

@ -1,8 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}

View File

@ -1,5 +1,5 @@
package com.baeldung.di.spring;
public interface BookService {
}
package com.baeldung.di.spring;
public interface BookService {
}

View File

@ -1,10 +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;
}
package com.baeldung.di.spring;
import org.springframework.beans.factory.annotation.Autowired;
public class BookServiceImpl implements BookService {
@Autowired(required = false)
private AuthorService authorService;
}

View File

@ -1,4 +1,4 @@
package com.baeldung.di.spring;
public class Foo {
}
package com.baeldung.di.spring;
public class Foo {
}

View File

@ -1,6 +1,6 @@
package com.baeldung.di.spring;
public class FooProcessor {
private Foo foo;
package com.baeldung.di.spring;
public class FooProcessor {
private Foo foo;
}

View File

@ -1,5 +1,5 @@
package com.baeldung.di.spring;
public interface IService {
public String serve();
}
package com.baeldung.di.spring;
public interface IService {
public String serve();
}

View File

@ -1,19 +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;
}
}
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;
}
}

View File

@ -1,10 +1,10 @@
package com.baeldung.di.spring;
public class IndexService implements IService {
@Override
public String serve() {
return "Hello World";
}
}
package com.baeldung.di.spring;
public class IndexService implements IService {
@Override
public String serve() {
return "Hello World";
}
}

View File

@ -1,14 +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);
}
}
}
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);
}
}
}

View File

@ -1,14 +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();
}
}
package com.baeldung.di.spring;
public class MessageApp {
private IService iService;
public MessageApp(IService iService) {
this.iService = iService;
}
public String getServiceValue() {
return iService.serve();
}
}

View File

@ -1,16 +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;
}
}
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;
}
}

View File

@ -1,18 +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();
}
}
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();
}
}

View File

@ -1,14 +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);
}
}
}
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);
}
}
}

View File

@ -1,20 +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;
}
}
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;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

View File

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

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
import org.springframework.stereotype.Component;

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
import org.springframework.stereotype.Component;

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
public interface Formatter {

View File

@ -1,4 +1,4 @@
package org.baeldung.sample;
package com.baeldung.sample;
import org.springframework.beans.factory.annotation.Qualifier;

View File

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

View File

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

View File

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

View File

@ -0,0 +1,2 @@
spring.profiles.active=prod

View 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>

View 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>

View 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>

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