Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e5e748

Browse files
Spring component scan example
1 parentf9b5ffb commit9e5e748

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagespring_component_scan_example;
2+
3+
importlombok.Getter;
4+
importlombok.ToString;
5+
6+
importorg.springframework.stereotype.Service;
7+
8+
@Service
9+
@ToString
10+
publicclassCareer {
11+
@Getter
12+
privateStringjobType;
13+
14+
@Getter
15+
privateintjobLevel;
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagespring_component_scan_example;
2+
3+
importorg.springframework.stereotype.Repository;
4+
5+
importlombok.Getter;
6+
importlombok.Setter;
7+
importlombok.ToString;
8+
9+
@Repository
10+
@ToString
11+
publicclassCity {
12+
@Getter
13+
privateStringcityName;
14+
15+
@Getter
16+
privateStringarea;
17+
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagespring_component_scan_example;
2+
3+
importorg.springframework.context.support.AbstractApplicationContext;
4+
importorg.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+
publicclassMainApp {
13+
14+
publicstaticvoidmain(String...args) {
15+
AbstractApplicationContextcontext =newClassPathXmlApplicationContext("spring_component_scan_example/spring.xml");
16+
Personp =(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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagespring_component_scan_example;
2+
3+
importlombok.Getter;
4+
5+
importorg.springframework.beans.factory.annotation.Autowired;
6+
importorg.springframework.stereotype.Service;
7+
8+
@Service
9+
publicclassPerson {
10+
@Getter
11+
@Autowired
12+
privateCitycity;
13+
14+
@Getter
15+
@Autowired
16+
privateWifewife;
17+
18+
@Getter
19+
@Autowired
20+
privateCareercareer;
21+
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagespring_component_scan_example;
2+
3+
importorg.springframework.stereotype.Component;
4+
importorg.springframework.stereotype.Repository;
5+
6+
importlombok.Getter;
7+
importlombok.Setter;
8+
importlombok.ToString;
9+
10+
@Component
11+
@ToString
12+
publicclassWife {
13+
14+
@Setter@Getter
15+
privateStringname;
16+
17+
@Setter@Getter
18+
privateStringfaith;
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beansxmlns="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-scanbase-package="spring_component_scan_example"/>
17+
18+
</beans>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp