JAVA-19545 Move articles from spring-core-2 module to spring-di-3 module (#13708)

* JAVA-19545 Move articles from spring-core-2 module to spring-di-3 module

* JAVA-19545 Move articles from spring-core-4 module to spring-di-3 module
This commit is contained in:
anuragkumawat
2023-03-26 23:51:27 +05:30
committed by GitHub
parent 07ef7b2186
commit 2e129bd64b
41 changed files with 4 additions and 4 deletions
@@ -0,0 +1,11 @@
package com.baeldung.autowire.sample;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
FooService fooService = ctx.getBean(FooService.class);
fooService.doStuff();
}
}
@@ -0,0 +1,10 @@
package com.baeldung.autowire.sample;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.autowire.sample")
public class AppConfig {
}
@@ -0,0 +1,13 @@
package com.baeldung.autowire.sample;
import org.springframework.stereotype.Component;
@FormatterType("Bar")
@Component
public class BarFormatter implements Formatter {
public String format() {
return "bar";
}
}
@@ -0,0 +1,5 @@
package com.baeldung.autowire.sample;
public class FooDAO {
}
@@ -0,0 +1,13 @@
package com.baeldung.autowire.sample;
import org.springframework.stereotype.Component;
@FormatterType("Foo")
@Component
public class FooFormatter implements Formatter {
public String format() {
return "foo";
}
}
@@ -0,0 +1,31 @@
package com.baeldung.autowire.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* This class presents a field, a constructor, and a setter injection type.
* Usually, we'd stick with a single approach for a given property. This is just an educational code.
*/
@Component
public class FooService {
@Autowired
@FormatterType("Foo")
private Formatter formatter;
@Autowired
public FooService(@FormatterType("Foo") Formatter formatter) {
this.formatter = formatter;
}
@Autowired
public void setFormatter(@FormatterType("Foo") Formatter formatter) {
this.formatter = formatter;
}
public String doStuff() {
return formatter.format();
}
}
@@ -0,0 +1,7 @@
package com.baeldung.autowire.sample;
public interface Formatter {
String format();
}
@@ -0,0 +1,17 @@
package com.baeldung.autowire.sample;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
@Qualifier
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
public @interface FormatterType {
String value();
}
@@ -0,0 +1,27 @@
package com.baeldung.dynamic.autowire;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BeanFactoryDynamicAutowireService {
private static final String SERVICE_NAME_SUFFIX = "regionService";
private final BeanFactory beanFactory;
@Autowired
public BeanFactoryDynamicAutowireService(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public boolean isServerActive(String isoCountryCode, int serverId) {
RegionService service = beanFactory.getBean(getRegionServiceBeanName(isoCountryCode), RegionService.class);
return service.isServerActive(serverId);
}
private String getRegionServiceBeanName(String isoCountryCode) {
return isoCountryCode + SERVICE_NAME_SUFFIX;
}
}
@@ -0,0 +1,26 @@
package com.baeldung.dynamic.autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class CustomMapFromListDynamicAutowireService {
private final Map<String, RegionService> servicesByCountryCode;
@Autowired
public CustomMapFromListDynamicAutowireService(List<RegionService> regionServices) {
servicesByCountryCode = regionServices.stream()
.collect(Collectors.toMap(RegionService::getISOCountryCode, Function.identity()));
}
public boolean isServerActive(String isoCountryCode, int serverId) {
RegionService service = servicesByCountryCode.get(isoCountryCode);
return service.isServerActive(serverId);
}
}
@@ -0,0 +1,9 @@
package com.baeldung.dynamic.autowire;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.dynamic.autowire")
public class DynamicAutowireConfig {
}
@@ -0,0 +1,16 @@
package com.baeldung.dynamic.autowire;
import org.springframework.stereotype.Service;
@Service("GBregionService")
public class GBRegionService implements RegionService {
@Override
public boolean isServerActive(int serverId) {
return false;
}
@Override
public String getISOCountryCode() {
return "GB";
}
}
@@ -0,0 +1,7 @@
package com.baeldung.dynamic.autowire;
public interface RegionService {
boolean isServerActive(int serverId);
String getISOCountryCode();
}
@@ -0,0 +1,16 @@
package com.baeldung.dynamic.autowire;
import org.springframework.stereotype.Service;
@Service("USregionService")
public class USRegionService implements RegionService {
@Override
public boolean isServerActive(int serverId) {
return true;
}
@Override
public String getISOCountryCode() {
return "US";
}
}
@@ -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,15 @@
package com.baeldung.order;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class Average implements Rating {
@Override
public int getRating() {
return 3;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.order;
import org.springframework.stereotype.Component;
import org.springframework.core.annotation.Order;
@Component
@Order(1)
public class Excellent implements Rating {
@Override
public int getRating() {
return 1;
}
}
@@ -0,0 +1,14 @@
package com.baeldung.order;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(2)
public class Good implements Rating {
@Override
public int getRating() {
return 2;
}
}
@@ -0,0 +1,6 @@
package com.baeldung.order;
public interface Rating {
int getRating();
}
@@ -0,0 +1,22 @@
package com.baeldung.autowire.sample;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class FooServiceIntegrationTest {
@Autowired
FooService fooService;
@Test
public void whenFooFormatterType_thenReturnFoo() {
Assert.assertEquals("foo", fooService.doStuff());
}
}
@@ -0,0 +1,33 @@
package com.baeldung.dynamic.autowire;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DynamicAutowireConfig.class)
public class DynamicAutowireIntegrationTest {
@Autowired
private BeanFactoryDynamicAutowireService beanFactoryDynamicAutowireService;
@Autowired
private CustomMapFromListDynamicAutowireService customMapFromListDynamicAutowireService;
@Test
public void givenDynamicallyAutowiredBean_whenCheckingServerInGB_thenServerIsNotActive() {
assertThat(beanFactoryDynamicAutowireService.isServerActive("GB", 101), is(false));
assertThat(customMapFromListDynamicAutowireService.isServerActive("GB", 101), is(false));
}
@Test
public void givenDynamicallyAutowiredBean_whenCheckingServerInUS_thenServerIsActive() {
assertThat(beanFactoryDynamicAutowireService.isServerActive("US", 101), is(true));
assertThat(customMapFromListDynamicAutowireService.isServerActive("US", 101), is(true));
}
}
@@ -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"));
}
}
@@ -0,0 +1,37 @@
package com.baeldung.order;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class RatingRetrieverUnitTest {
@Configuration
@ComponentScan(basePackages = {"com.baeldung.order"})
static class ContextConfiguration {}
@Autowired
private List<Rating> ratings;
@Test
public void givenOrderOnComponents_whenInjected_thenAutowireByOrderValue() {
assertThat(ratings.get(0).getRating(), is(equalTo(1)));
assertThat(ratings.get(1).getRating(), is(equalTo(2)));
assertThat(ratings.get(2).getRating(), is(equalTo(3)));
}
}