diff --git a/pom.xml b/pom.xml index 70efb02f4d..ccdc91ffe0 100644 --- a/pom.xml +++ b/pom.xml @@ -631,6 +631,7 @@ spring-core-2 spring-core-3 spring-core-4 + spring-core-5 spring-cucumber spring-data-rest @@ -1082,6 +1083,7 @@ spring-core-2 spring-core-3 spring-core-4 + spring-core-5 spring-cucumber spring-data-rest diff --git a/spring-core-3/README.md b/spring-core-3/README.md index b6257cb9a4..7232ad71a1 100644 --- a/spring-core-3/README.md +++ b/spring-core-3/README.md @@ -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) diff --git a/spring-core-4/README.md b/spring-core-4/README.md index 03a6747c1d..3b2c3d4764 100644 --- a/spring-core-4/README.md +++ b/spring-core-4/README.md @@ -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) diff --git a/spring-core-5/README.md b/spring-core-5/README.md new file mode 100644 index 0000000000..81669e46a7 --- /dev/null +++ b/spring-core-5/README.md @@ -0,0 +1,7 @@ +## Spring Core + +This module contains articles about core Spring functionality + +## Relevant Articles: + +- More articles: [[<-- prev]](/spring-core-4) \ No newline at end of file diff --git a/spring-core-5/pom.xml b/spring-core-5/pom.xml new file mode 100644 index 0000000000..743002c137 --- /dev/null +++ b/spring-core-5/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + spring-core-5 + spring-core-5 + + + com.baeldung + parent-spring-5 + 0.0.1-SNAPSHOT + ../parent-spring-5 + + + + + org.springframework.boot + spring-boot-starter + ${spring-boot-starter.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot-starter.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven.surefire.version} + + + + + + 5.3.3 + 2.4.2 + 2.22.1 + + + diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/AmbiguousBean.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/AmbiguousBean.java new file mode 100644 index 0000000000..70e1f0a79a --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/AmbiguousBean.java @@ -0,0 +1,4 @@ +package com.baeldung.component.inscope; + +public interface AmbiguousBean { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanExample.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanExample.java new file mode 100644 index 0000000000..d1b4cd34f1 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanExample.java @@ -0,0 +1,4 @@ +package com.baeldung.component.inscope; + +public class BeanExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanImplA.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanImplA.java new file mode 100644 index 0000000000..08e1661499 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanImplA.java @@ -0,0 +1,4 @@ +package com.baeldung.component.inscope; + +public class BeanImplA implements AmbiguousBean { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanImplB.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanImplB.java new file mode 100644 index 0000000000..8a45581532 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/BeanImplB.java @@ -0,0 +1,4 @@ +package com.baeldung.component.inscope; + +public class BeanImplB implements AmbiguousBean { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/ComponentApplication.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/ComponentApplication.java new file mode 100644 index 0000000000..604e7d6632 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/ComponentApplication.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/ComponentExample.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/ComponentExample.java new file mode 100644 index 0000000000..66d3e20d34 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/ComponentExample.java @@ -0,0 +1,7 @@ +package com.baeldung.component.inscope; + +import org.springframework.stereotype.Component; + +@Component +public class ComponentExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/ControllerExample.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/ControllerExample.java new file mode 100644 index 0000000000..773af2a113 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/ControllerExample.java @@ -0,0 +1,7 @@ +package com.baeldung.component.inscope; + +import org.springframework.stereotype.Controller; + +@Controller +public class ControllerExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/CustomComponent.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/CustomComponent.java new file mode 100644 index 0000000000..795a1b23fa --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/CustomComponent.java @@ -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 { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/CustomComponentExample.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/CustomComponentExample.java new file mode 100644 index 0000000000..8009f8ec20 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/CustomComponentExample.java @@ -0,0 +1,5 @@ +package com.baeldung.component.inscope; + +@CustomComponent +public class CustomComponentExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/RepositoryExample.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/RepositoryExample.java new file mode 100644 index 0000000000..9297992904 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/RepositoryExample.java @@ -0,0 +1,7 @@ +package com.baeldung.component.inscope; + +import org.springframework.stereotype.Repository; + +@Repository +public class RepositoryExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/inscope/ServiceExample.java b/spring-core-5/src/main/java/com/baeldung/component/inscope/ServiceExample.java new file mode 100644 index 0000000000..e2db60504a --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/inscope/ServiceExample.java @@ -0,0 +1,7 @@ +package com.baeldung.component.inscope; + +import org.springframework.stereotype.Service; + +@Service +public class ServiceExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/outsidescope/OutsideScopeBeanExample.java b/spring-core-5/src/main/java/com/baeldung/component/outsidescope/OutsideScopeBeanExample.java new file mode 100644 index 0000000000..079c012862 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/outsidescope/OutsideScopeBeanExample.java @@ -0,0 +1,4 @@ +package com.baeldung.component.outsidescope; + +public class OutsideScopeBeanExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/outsidescope/OutsideScopeExample.java b/spring-core-5/src/main/java/com/baeldung/component/outsidescope/OutsideScopeExample.java new file mode 100644 index 0000000000..5c7fff959d --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/outsidescope/OutsideScopeExample.java @@ -0,0 +1,7 @@ +package com.baeldung.component.outsidescope; + +import org.springframework.stereotype.Component; + +@Component +public class OutsideScopeExample { +} diff --git a/spring-core-5/src/main/java/com/baeldung/component/scannedscope/ScannedScopeExample.java b/spring-core-5/src/main/java/com/baeldung/component/scannedscope/ScannedScopeExample.java new file mode 100644 index 0000000000..59873634d6 --- /dev/null +++ b/spring-core-5/src/main/java/com/baeldung/component/scannedscope/ScannedScopeExample.java @@ -0,0 +1,7 @@ +package com.baeldung.component.scannedscope; + +import org.springframework.stereotype.Component; + +@Component +public class ScannedScopeExample { +} diff --git a/spring-core-5/src/main/resources/application.yml b/spring-core-5/src/main/resources/application.yml new file mode 100644 index 0000000000..5c09fdb8b0 --- /dev/null +++ b/spring-core-5/src/main/resources/application.yml @@ -0,0 +1 @@ +ambiguous-bean: 'A' \ No newline at end of file diff --git a/spring-core-5/src/test/java/com/baeldung/component/inscope/ComponentUnitTest.java b/spring-core-5/src/test/java/com/baeldung/component/inscope/ComponentUnitTest.java new file mode 100644 index 0000000000..09ce604d40 --- /dev/null +++ b/spring-core-5/src/test/java/com/baeldung/component/inscope/ComponentUnitTest.java @@ -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); + } +} \ No newline at end of file diff --git a/spring-core-5/src/test/resources/application.yml b/spring-core-5/src/test/resources/application.yml new file mode 100644 index 0000000000..da23e59c24 --- /dev/null +++ b/spring-core-5/src/test/resources/application.yml @@ -0,0 +1 @@ +ambiguous-bean: 'B' \ No newline at end of file