Added a new module (spring-core-5) for the code examples of BAEL-4764. (#10484)

This commit is contained in:
Jordan Simpson
2021-02-13 14:29:50 -06:00
committed by GitHub
parent 89eb93a46c
commit 354018732d
22 changed files with 236 additions and 2 deletions
@@ -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 {
}
@@ -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);
}
}
@@ -0,0 +1 @@
ambiguous-bean: 'B'