basics of spring autowire - first cut

This commit is contained in:
sivabalachandran
2016-05-15 17:47:30 -04:00
parent 3896065b80
commit 75d0eb499f
12 changed files with 188 additions and 0 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,5 @@
package com.baeldung.autowire.sample;
public class Bar {
}
@@ -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 Foo {
}
@@ -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,17 @@
package com.baeldung.autowire.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FooService {
@Autowired
@FormatterType("Foo")
private 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,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 FooServiceTest {
@Autowired
FooService fooService;
@Test
public void whenFooFormatterType_thenReturnFoo(){
Assert.assertEquals("foo", fooService.doStuff());
}
}