Spring Import Annotation - initial commit (#9424)
Co-authored-by: Gilvan Ornelas Fernandes Filho <gilvan.fernandes@bairesdev.com>
This commit is contained in:
parent
024874d9c8
commit
aa5551bd9b
@ -0,0 +1,9 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Import({ MammalConfiguration.class, BirdConfig.class })
|
||||
class AnimalConfiguration {
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class AnimalScanConfiguration {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
class Bird {
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
class BirdConfig {
|
||||
|
||||
@Bean
|
||||
Bird bird() {
|
||||
return new Bird();
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value = "bug")
|
||||
class Bug {
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Import(Bug.class)
|
||||
class BugConfig {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
class Cat {
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
class CatConfig {
|
||||
|
||||
@Bean
|
||||
Cat cat() {
|
||||
return new Cat();
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
class Dog {
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
class DogConfig {
|
||||
|
||||
@Bean
|
||||
Dog dog() {
|
||||
return new Dog();
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Configuration
|
||||
@Import({ DogConfig.class, CatConfig.class })
|
||||
class MammalConfiguration {
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.importannotation.zoo;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import com.baeldung.importannotation.animal.AnimalScanConfiguration;
|
||||
|
||||
@Configuration
|
||||
@Import(AnimalScanConfiguration.class)
|
||||
class ZooApplication {
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = { AnimalConfiguration.class })
|
||||
class AnimalConfigUnitTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void givenImportedBeans_whenGettingEach_shallFindOnlyTheImportedBeans() {
|
||||
assertThatBeanExists("dog", Dog.class);
|
||||
assertThatBeanExists("cat", Cat.class);
|
||||
assertThatBeanExists("bird", Cat.class);
|
||||
}
|
||||
|
||||
private void assertThatBeanExists(String beanName, Class<?> beanClass) {
|
||||
assertTrue(context.containsBean(beanName));
|
||||
assertNotNull(context.getBean(beanClass));
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = BugConfig.class)
|
||||
class BugConfigUnitTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void givenImportInComponent_whenLookForBean_shallFindIt() {
|
||||
assertTrue(context.containsBean("bug"));
|
||||
assertNotNull(context.getBean(Bug.class));
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = { BirdConfig.class, CatConfig.class, DogConfig.class })
|
||||
class ConfigUnitTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void givenImportedBeans_whenGettingEach_shallFindIt() {
|
||||
assertThatBeanExists("dog", Dog.class);
|
||||
assertThatBeanExists("cat", Cat.class);
|
||||
assertThatBeanExists("bird", Bird.class);
|
||||
}
|
||||
|
||||
private void assertThatBeanExists(String beanName, Class<?> beanClass) {
|
||||
assertTrue(context.containsBean(beanName));
|
||||
assertNotNull(context.getBean(beanClass));
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.baeldung.importannotation.animal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = { MammalConfiguration.class })
|
||||
class MammalConfigUnitTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void givenImportedBeans_whenGettingEach_shallFindOnlyTheImportedBeans() {
|
||||
assertThatBeanExists("dog", Dog.class);
|
||||
assertThatBeanExists("cat", Cat.class);
|
||||
|
||||
assertFalse(context.containsBean("bird"));
|
||||
}
|
||||
|
||||
private void assertThatBeanExists(String beanName, Class<?> beanClass) {
|
||||
assertTrue(context.containsBean(beanName));
|
||||
assertNotNull(context.getBean(beanClass));
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.importannotation.zoo;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(classes = ZooApplication.class)
|
||||
class ZooApplicationUnitTest {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
void givenTheScanInTheAnimalPackage_whenGettingAnyAnimal_shallFindItInTheContext() {
|
||||
assertNotNull(context.getBean("dog"));
|
||||
assertNotNull(context.getBean("bird"));
|
||||
assertNotNull(context.getBean("cat"));
|
||||
assertNotNull(context.getBean("bug"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user