[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:
parent
6ee7181125
commit
8d6c2dc2ed
|
@ -1,3 +1,6 @@
|
||||||
## Relevant Articles:
|
## Relevant Articles:
|
||||||
|
|
||||||
- [Understanding getBean() in Spring](https://www.baeldung.com/spring-getbean)
|
- [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)
|
||||||
|
|
|
@ -14,6 +14,11 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-beans</artifactId>
|
<artifactId>spring-beans</artifactId>
|
||||||
|
|
|
@ -1,22 +1,12 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
- [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)
|
- [Constructor Injection in Spring with Lombok](http://www.baeldung.com/spring-injection-lombok)
|
||||||
- [Introduction to Spring’s StreamUtils](http://www.baeldung.com/spring-stream-utils)
|
- [Introduction to Spring’s StreamUtils](http://www.baeldung.com/spring-stream-utils)
|
||||||
- [XML-Based Injection in Spring](http://www.baeldung.com/spring-xml-injection)
|
- [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)
|
- [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)
|
- [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)
|
- [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)
|
- [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)
|
- [What is a Spring Bean?](https://www.baeldung.com/spring-bean)
|
||||||
- [Spring PostConstruct and PreDestroy Annotations](https://www.baeldung.com/spring-postconstruct-predestroy)
|
- [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)
|
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
### Relevant Articles
|
### Relevant Articles
|
||||||
|
|
||||||
- [The Spring @Qualifier Annotation](https://www.baeldung.com/spring-qualifier-annotation)
|
- [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>
|
<name>spring-di</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-spring-5</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
<relativePath>../parent-spring-5</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<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>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>javax.annotation</groupId>
|
<groupId>javax.annotation</groupId>
|
||||||
|
@ -28,12 +80,12 @@
|
||||||
<version>${annotation-api.version}</version>
|
<version>${annotation-api.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- test scoped -->
|
<!-- <!– test scoped –>-->
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>org.springframework</groupId>
|
<!-- <groupId>org.springframework</groupId>-->
|
||||||
<artifactId>spring-test</artifactId>
|
<!-- <artifactId>spring-test</artifactId>-->
|
||||||
<scope>test</scope>
|
<!-- <scope>test</scope>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -97,6 +149,14 @@
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<org.springframework.version>5.0.6.RELEASE</org.springframework.version>
|
<org.springframework.version>5.0.6.RELEASE</org.springframework.version>
|
||||||
<annotation-api.version>1.3.2</annotation-api.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>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,31 +1,31 @@
|
||||||
package com.baeldung.constructordi;
|
package com.baeldung.constructordi;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
import com.baeldung.constructordi.domain.Car;
|
import com.baeldung.constructordi.domain.Car;
|
||||||
|
|
||||||
public class SpringRunner {
|
public class SpringRunner {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Car toyota = getCarFromXml();
|
Car toyota = getCarFromXml();
|
||||||
|
|
||||||
System.out.println(toyota);
|
System.out.println(toyota);
|
||||||
|
|
||||||
toyota = getCarFromJavaConfig();
|
toyota = getCarFromJavaConfig();
|
||||||
|
|
||||||
System.out.println(toyota);
|
System.out.println(toyota);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Car getCarFromJavaConfig() {
|
private static Car getCarFromJavaConfig() {
|
||||||
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||||
|
|
||||||
return context.getBean(Car.class);
|
return context.getBean(Car.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Car getCarFromXml() {
|
private static Car getCarFromXml() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("constructordi.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("constructordi.xml");
|
||||||
|
|
||||||
return context.getBean(Car.class);
|
return context.getBean(Car.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,21 +1,21 @@
|
||||||
package com.baeldung.constructordi.domain;
|
package com.baeldung.constructordi.domain;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class Car {
|
public class Car {
|
||||||
private Engine engine;
|
private Engine engine;
|
||||||
private Transmission transmission;
|
private Transmission transmission;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public Car(Engine engine, Transmission transmission) {
|
public Car(Engine engine, Transmission transmission) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.transmission = transmission;
|
this.transmission = transmission;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Engine: %s Transmission: %s", engine, transmission);
|
return String.format("Engine: %s Transmission: %s", engine, transmission);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,27 +1,27 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class Account {
|
public class Account {
|
||||||
|
|
||||||
private String accountNumber;
|
private String accountNumber;
|
||||||
private String type;
|
private String type;
|
||||||
|
|
||||||
public String getAccountNumber() {
|
public String getAccountNumber() {
|
||||||
return accountNumber;
|
return accountNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountNumber(String accountNumber) {
|
public void setAccountNumber(String accountNumber) {
|
||||||
this.accountNumber = accountNumber;
|
this.accountNumber = accountNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getType() {
|
public String getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(String type) {
|
public void setType(String type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public interface AccountService {
|
public interface AccountService {
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class AccountServiceImpl implements AccountService {
|
public class AccountServiceImpl implements AccountService {
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public interface BookService {
|
public interface BookService {
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
public class BookServiceImpl implements BookService {
|
public class BookServiceImpl implements BookService {
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private AuthorService authorService;
|
private AuthorService authorService;
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class Foo {
|
public class Foo {
|
||||||
}
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class FooProcessor {
|
public class FooProcessor {
|
||||||
|
|
||||||
private Foo foo;
|
private Foo foo;
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public interface IService {
|
public interface IService {
|
||||||
public String serve();
|
public String serve();
|
||||||
}
|
}
|
|
@ -1,19 +1,19 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class IndexApp {
|
public class IndexApp {
|
||||||
|
|
||||||
private IService service;
|
private IService service;
|
||||||
|
|
||||||
public String getServiceValue() {
|
public String getServiceValue() {
|
||||||
return service.serve();
|
return service.serve();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IService getService() {
|
public IService getService() {
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setService(IService service) {
|
public void setService(IService service) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class IndexService implements IService {
|
public class IndexService implements IService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String serve() {
|
public String serve() {
|
||||||
return "Hello World";
|
return "Hello World";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class InstanceServiceFactory {
|
public class InstanceServiceFactory {
|
||||||
public IService getService(int number) {
|
public IService getService(int number) {
|
||||||
switch (number) {
|
switch (number) {
|
||||||
case 1:
|
case 1:
|
||||||
return new MessageService("Foo");
|
return new MessageService("Foo");
|
||||||
case 0:
|
case 0:
|
||||||
return new IndexService();
|
return new IndexService();
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown parameter " + number);
|
throw new IllegalArgumentException("Unknown parameter " + number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class MessageApp {
|
public class MessageApp {
|
||||||
|
|
||||||
private IService iService;
|
private IService iService;
|
||||||
|
|
||||||
public MessageApp(IService iService) {
|
public MessageApp(IService iService) {
|
||||||
this.iService = iService;
|
this.iService = iService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getServiceValue() {
|
public String getServiceValue() {
|
||||||
return iService.serve();
|
return iService.serve();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,16 +1,16 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class MessageService implements IService {
|
public class MessageService implements IService {
|
||||||
|
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
public MessageService(String message) {
|
public MessageService(String message) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String serve() {
|
public String serve() {
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,18 +1,18 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({ SpringBeansConfig.class })
|
@Import({ SpringBeansConfig.class })
|
||||||
@ComponentScan("com.baeldung.di.spring")
|
@ComponentScan("com.baeldung.di.spring")
|
||||||
public class SpringMainConfig {
|
public class SpringMainConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public BookService bookServiceGenerator() {
|
public BookService bookServiceGenerator() {
|
||||||
return new BookServiceImpl();
|
return new BookServiceImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
public class StaticServiceFactory {
|
public class StaticServiceFactory {
|
||||||
public static IService getService(int number) {
|
public static IService getService(int number) {
|
||||||
switch (number) {
|
switch (number) {
|
||||||
case 1:
|
case 1:
|
||||||
return new MessageService("Foo");
|
return new MessageService("Foo");
|
||||||
case 0:
|
case 0:
|
||||||
return new IndexService();
|
return new IndexService();
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unknown parameter " + number);
|
throw new IllegalArgumentException("Unknown parameter " + number);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,20 +1,20 @@
|
||||||
package com.baeldung.di.spring;
|
package com.baeldung.di.spring;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AccountService accountService;
|
private AccountService accountService;
|
||||||
|
|
||||||
public AccountService getAccountService() {
|
public AccountService getAccountService() {
|
||||||
return accountService;
|
return accountService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAccountService(AccountService accountService) {
|
public void setAccountService(AccountService accountService) {
|
||||||
this.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;
|
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.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
|
@ -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;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.sample;
|
||||||
|
|
||||||
|
public class Foo {
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.sample;
|
package com.baeldung.sample;
|
||||||
|
|
||||||
public interface Formatter {
|
public interface Formatter {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package org.baeldung.sample;
|
package com.baeldung.sample;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
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 {
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
spring.profiles.active=prod
|
||||||
|
|
|
@ -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>
|
|
@ -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>
|
|
@ -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
Loading…
Reference in New Issue