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:
@@ -7,4 +7,8 @@ This module contains articles about dependency injection with Spring
|
||||
- [@Lookup Annotation in Spring](https://www.baeldung.com/spring-lookup)
|
||||
- [Spring @Autowired Field Null – Common Causes and Solutions](https://www.baeldung.com/spring-autowired-field-null)
|
||||
- [Finding All Beans with a Custom Annotation](https://www.baeldung.com/spring-injecting-all-annotated-beans)
|
||||
- [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire)
|
||||
- [@Order in Spring](http://www.baeldung.com/spring-order)
|
||||
- [How to dynamically Autowire a Bean in Spring](https://www.baeldung.com/spring-dynamic-autowire)
|
||||
- [Spring @Import Annotation](https://www.baeldung.com/spring-import-annotation)
|
||||
- More articles: [[<-- prev]](../spring-di-2)
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
+27
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -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";
|
||||
}
|
||||
}
|
||||
+9
@@ -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 {
|
||||
}
|
||||
+9
@@ -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();
|
||||
}
|
||||
}
|
||||
+9
@@ -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;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user