Skip to content

Commit b1eab75

Browse files
committed
Guide to Spring @Autowired
2 parents bcc31e1 + 75d0eb4 commit b1eab75

12 files changed

Lines changed: 188 additions & 0 deletions

File tree

‎spring-autowire/pom.xml‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>spring-autowire</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-autowire</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<org.springframework.version>4.2.5.RELEASE</org.springframework.version>
16+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
17+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.11</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-core</artifactId>
29+
<version>${org.springframework.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-context</artifactId>
34+
<version>${org.springframework.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-test</artifactId>
39+
<version>${org.springframework.version}</version>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<finalName>spring-autowire</finalName>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>${maven-compiler-plugin.version}</version>
51+
<configuration>
52+
<source>1.8</source>
53+
<target>1.8</target>
54+
</configuration>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>${maven-surefire-plugin.version}</version>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.autowire.sample;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
public class App {
6+
public static void main(String[] args) {
7+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
8+
FooService fooService = ctx.getBean(FooService.class);
9+
fooService.doStuff();
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.autowire.sample;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan("com.baeldung.autowire.sample")
8+
public class AppConfig {
9+
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.autowire.sample;
2+
3+
public class Bar {
4+
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.autowire.sample;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@FormatterType("Bar")
6+
@Component
7+
public class BarFormatter implements Formatter {
8+
9+
public String format() {
10+
return "bar";
11+
}
12+
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.autowire.sample;
2+
3+
public class Foo {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.autowire.sample;
2+
3+
public class FooDAO {
4+
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.autowire.sample;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@FormatterType("Foo")
6+
@Component
7+
public class FooFormatter implements Formatter {
8+
9+
public String format() {
10+
return "foo";
11+
}
12+
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.autowire.sample;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class FooService {
8+
9+
@Autowired
10+
@FormatterType("Foo")
11+
private Formatter formatter;
12+
13+
public String doStuff(){
14+
return formatter.format();
15+
}
16+
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.autowire.sample;
2+
3+
public interface Formatter {
4+
5+
String format();
6+
7+
}

0 commit comments

Comments
 (0)