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

Commitcb03c35

Browse files
Spring@resource annotation
1 parent4a500dc commitcb03c35

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagespring_atResource_annotation;
2+
3+
importlombok.Getter;
4+
importlombok.Setter;
5+
importlombok.ToString;
6+
7+
@ToString
8+
publicclassCareer {
9+
@Getter
10+
@Setter
11+
privateStringjobType;
12+
13+
@Getter
14+
@Setter
15+
privateintjobLevel;
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packagespring_atResource_annotation;
2+
3+
importlombok.Getter;
4+
importlombok.Setter;
5+
importlombok.ToString;
6+
7+
@ToString
8+
publicclassCity {
9+
@Getter
10+
@Setter
11+
privateStringcityName;
12+
13+
@Getter
14+
@Setter
15+
privateStringarea;
16+
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagespring_atResource_annotation;
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 @Resource annotation works! Cool!
7+
* Look at this project along with other two:
8+
* SpringPureXMLDependencyInjectionExample
9+
* SpringComponentScanExample
10+
* to help me better understand the three possible ways to do DI using Spring.*/
11+
12+
13+
publicclassMainApp {
14+
15+
publicstaticvoidmain(String...args) {
16+
AbstractApplicationContextcontext =newClassPathXmlApplicationContext("spring_resource_annotation/resource_annotation_config.xml");
17+
Personp =(Person)context.getBean("person");
18+
19+
System.out.println(p.getCity().toString());
20+
System.out.println(p.getWife().toString());
21+
System.out.println(p.getCareer().toString());
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagespring_atResource_annotation;
2+
3+
importjavax.annotation.Resource;
4+
5+
importorg.springframework.beans.factory.annotation.Autowired;
6+
importorg.springframework.stereotype.Service;
7+
8+
importlombok.Getter;
9+
importlombok.Setter;
10+
11+
publicclassPerson {
12+
@Resource(name ="home123")
13+
@Getter
14+
privateCitycity;
15+
16+
@Resource(name ="wife")
17+
@Getter
18+
privateWifewife;
19+
20+
//I gave it a shot here, I must use this @Resource annotation and define this bean in the .xml file, otherwise it throws exception!
21+
//B/c this one is using <context:annotation-config />, you can find this line in spring.xml.
22+
@Resource(name ="careerCurrent")
23+
@Getter
24+
privateCareercareer;
25+
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagespring_atResource_annotation;
2+
3+
importlombok.Getter;
4+
importlombok.Setter;
5+
importlombok.ToString;
6+
7+
@ToString
8+
publicclassWife {
9+
10+
@Getter
11+
@Setter
12+
privateStringname;
13+
14+
@Getter
15+
@Setter
16+
privateStringfaith;
17+
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
<context:annotation-config />
11+
<!-- All magic hides in the above line, it tells Spring to wire up all beans (take a look at Person class, it has three other fields that are being annotated with @Resource annotation).-->
12+
13+
<beanid="person"class="spring_resource_annotation.Person">
14+
</bean>
15+
<beanid="home123"class="spring_resource_annotation.City">
16+
<propertyname="cityName"value="San Jose"/>
17+
<propertyname="area"value="NorCal"/>
18+
</bean>
19+
<beanid="wife"class="spring_resource_annotation.Wife">
20+
<propertyname="name"value="Sophie Yan"/>
21+
<propertyname="faith"value="Christian"/>
22+
</bean>
23+
<bean id ="careerCurrent"class="spring_resource_annotation.Career">
24+
<propertyname="jobType"value="SDE"/>
25+
<propertyname="jobLevel"value="4"/>
26+
</bean>
27+
<beanid="careerFuture"class="spring_resource_annotation.Career">
28+
<propertyname="jobType"value="SDE"/>
29+
<propertyname="jobLevel"value="5"/>
30+
</bean>
31+
32+
</beans>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp