Added a new module (spring-core-5) for the code examples of BAEL-4764. (#10484)
This commit is contained in:
parent
89eb93a46c
commit
354018732d
2
pom.xml
2
pom.xml
@ -631,6 +631,7 @@
|
||||
<module>spring-core-2</module>
|
||||
<module>spring-core-3</module>
|
||||
<module>spring-core-4</module>
|
||||
<module>spring-core-5</module>
|
||||
<module>spring-cucumber</module>
|
||||
|
||||
<module>spring-data-rest</module>
|
||||
@ -1082,6 +1083,7 @@
|
||||
<module>spring-core-2</module>
|
||||
<module>spring-core-3</module>
|
||||
<module>spring-core-4</module>
|
||||
<module>spring-core-5</module>
|
||||
<module>spring-cucumber</module>
|
||||
|
||||
<module>spring-data-rest</module>
|
||||
|
@ -11,4 +11,4 @@ This module contains articles about core Spring functionality
|
||||
- [Difference Between BeanFactory and ApplicationContext](https://www.baeldung.com/spring-beanfactory-vs-applicationcontext)
|
||||
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
|
||||
- [Custom Scope in Spring](http://www.baeldung.com/spring-custom-scope)
|
||||
- More articles: [[<-- prev]](/spring-core-2)
|
||||
- More articles: [[<-- prev]](/spring-core-2) [[next -->]](/spring-core-4)
|
||||
|
@ -11,4 +11,4 @@ This module contains articles about core Spring functionality
|
||||
- [Using @Autowired in Abstract Classes](https://www.baeldung.com/spring-autowired-abstract-class)
|
||||
- [Constructor Injection in Spring with Lombok](https://www.baeldung.com/spring-injection-lombok)
|
||||
- [The Spring ApplicationContext](https://www.baeldung.com/spring-application-context)
|
||||
- More articles: [[<-- prev]](/spring-core-3)
|
||||
- More articles: [[<-- prev]](/spring-core-3) [[next -->]](/spring-core-5)
|
||||
|
7
spring-core-5/README.md
Normal file
7
spring-core-5/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
## Spring Core
|
||||
|
||||
This module contains articles about core Spring functionality
|
||||
|
||||
## Relevant Articles:
|
||||
|
||||
- More articles: [[<-- prev]](/spring-core-4)
|
46
spring-core-5/pom.xml
Normal file
46
spring-core-5/pom.xml
Normal file
@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-core-5</artifactId>
|
||||
<name>spring-core-5</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot-starter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven.surefire.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring.version>5.3.3</spring.version>
|
||||
<spring-boot-starter.version>2.4.2</spring-boot-starter.version>
|
||||
<maven.surefire.version>2.22.1</maven.surefire.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
public interface AmbiguousBean {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
public class BeanExample {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
public class BeanImplA implements AmbiguousBean {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
public class BeanImplB implements AmbiguousBean {
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import com.baeldung.component.outsidescope.OutsideScopeBeanExample;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan({"com.baeldung.component.inscope", "com.baeldung.component.scannedscope"})
|
||||
@Configuration
|
||||
public class ComponentApplication {
|
||||
|
||||
@Value( "${ambiguous-bean}" )
|
||||
public String ambiguousBean;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run( ComponentApplication.class, args );
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BeanExample beanExample() {
|
||||
return new BeanExample();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OutsideScopeBeanExample outsideScopeBeanExample() {
|
||||
return new OutsideScopeBeanExample();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AmbiguousBean ambiguousBean() {
|
||||
if (ambiguousBean.equals("A")) {
|
||||
return new BeanImplA();
|
||||
}
|
||||
else {
|
||||
return new BeanImplB();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ComponentExample {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@Controller
|
||||
public class ControllerExample {
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Component
|
||||
public @interface CustomComponent {
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
@CustomComponent
|
||||
public class CustomComponentExample {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class RepositoryExample {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ServiceExample {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.component.outsidescope;
|
||||
|
||||
public class OutsideScopeBeanExample {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.component.outsidescope;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class OutsideScopeExample {
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.component.scannedscope;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ScannedScopeExample {
|
||||
}
|
1
spring-core-5/src/main/resources/application.yml
Normal file
1
spring-core-5/src/main/resources/application.yml
Normal file
@ -0,0 +1 @@
|
||||
ambiguous-bean: 'A'
|
@ -0,0 +1,54 @@
|
||||
package com.baeldung.component.inscope;
|
||||
|
||||
import com.baeldung.component.outsidescope.OutsideScopeBeanExample;
|
||||
import com.baeldung.component.outsidescope.OutsideScopeExample;
|
||||
import com.baeldung.component.scannedscope.ScannedScopeExample;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest
|
||||
@ExtendWith(SpringExtension.class)
|
||||
public class ComponentUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Test
|
||||
public void givenInScopeComponents_whenSearchingInApplicationContext_thenFindThem() {
|
||||
assertNotNull(applicationContext.getBean(ControllerExample.class));
|
||||
assertNotNull(applicationContext.getBean(ServiceExample.class));
|
||||
assertNotNull(applicationContext.getBean(RepositoryExample.class));
|
||||
assertNotNull(applicationContext.getBean(ComponentExample.class));
|
||||
assertNotNull(applicationContext.getBean(CustomComponentExample.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOutsideScopeComponent_whenSearchingInApplicationContext_thenFail() {
|
||||
assertThrows(NoSuchBeanDefinitionException.class, () -> applicationContext.getBean(OutsideScopeExample.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenScannedScopeComponent_whenSearchingInApplicationContext_thenFindIt() {
|
||||
assertNotNull(applicationContext.getBean(ScannedScopeExample.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBeanComponents_whenSearchingInApplicationContext_thenFindThem() {
|
||||
assertNotNull(applicationContext.getBean(BeanExample.class));
|
||||
assertNotNull(applicationContext.getBean(OutsideScopeBeanExample.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAmbiguousBeanSetToB_whenSearchingInApplicationContext_thenFindImplB() {
|
||||
AmbiguousBean ambiguousBean = applicationContext.getBean(AmbiguousBean.class);
|
||||
assertNotNull(ambiguousBean);
|
||||
assertTrue(ambiguousBean instanceof BeanImplB);
|
||||
}
|
||||
}
|
1
spring-core-5/src/test/resources/application.yml
Normal file
1
spring-core-5/src/test/resources/application.yml
Normal file
@ -0,0 +1 @@
|
||||
ambiguous-bean: 'B'
|
Loading…
x
Reference in New Issue
Block a user