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:
@@ -1,11 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
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 {
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.autowire.sample;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@FormatterType("Bar")
|
||||
@Component
|
||||
public class BarFormatter implements Formatter {
|
||||
|
||||
public String format() {
|
||||
return "bar";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.autowire.sample;
|
||||
|
||||
public class FooDAO {
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.autowire.sample;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@FormatterType("Foo")
|
||||
@Component
|
||||
public class FooFormatter implements Formatter {
|
||||
|
||||
public String format() {
|
||||
return "foo";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.autowire.sample;
|
||||
|
||||
public interface Formatter {
|
||||
|
||||
String format();
|
||||
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
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();
|
||||
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.order;
|
||||
|
||||
public interface Rating {
|
||||
|
||||
int getRating();
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
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)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user