Skip to content

Commit 9e5e748

Browse files
Spring component scan example
1 parent f9b5ffb commit 9e5e748

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package spring_component_scan_example;
2+
3+
import lombok.Getter;
4+
import lombok.ToString;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
@ToString
10+
public class Career {
11+
@Getter
12+
private String jobType;
13+
14+
@Getter
15+
private int jobLevel;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package spring_component_scan_example;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import lombok.ToString;
8+
9+
@Repository
10+
@ToString
11+
public class City {
12+
@Getter
13+
private String cityName;
14+
15+
@Getter
16+
private String area;
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package spring_component_scan_example;
2+
3+
import org.springframework.context.support.AbstractApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**This app is running fine! It helps me understand how component scan works! Cool!
7+
* Look at this project along with other two:
8+
* Spring@ResourceAnnotationExample
9+
* SpringComponentScanExample
10+
* to help me better understand the three possible ways to do DI using Spring.*/
11+
12+
public class MainApp {
13+
14+
public static void main(String... args) {
15+
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring_component_scan_example/spring.xml");
16+
Person p =(Person) context.getBean("person");
17+
18+
System.out.println(p.getCity().toString());
19+
System.out.println(p.getWife().toString());
20+
System.out.println(p.getCareer().toString());
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package spring_component_scan_example;
2+
3+
import lombok.Getter;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class Person {
10+
@Getter
11+
@Autowired
12+
private City city;
13+
14+
@Getter
15+
@Autowired
16+
private Wife wife;
17+
18+
@Getter
19+
@Autowired
20+
private Career career;
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package spring_component_scan_example;
2+
3+
import org.springframework.stereotype.Component;
4+
import org.springframework.stereotype.Repository;
5+
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
import lombok.ToString;
9+
10+
@Component
11+
@ToString
12+
public class Wife {
13+
14+
@Setter @Getter
15+
private String name;
16+
17+
@Setter @Getter
18+
private String faith;
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9+
10+
<!-- All magic hides within this line:
11+
It does a component scan for this package, so any beans that have any one of the following three annotations:
12+
@Repository, @Service or @Component
13+
will be scanned and autowired up.
14+
Also I'll have to put @Autowired annotation on those private fields inside that Person clalss. -->
15+
16+
<context:component-scan base-package="spring_component_scan_example"/>
17+
18+
</beans>

0 commit comments

Comments
 (0)