Skip to content

Commit cb03c35

Browse files
Spring @resource annotation
1 parent 4a500dc commit cb03c35

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package spring_atResource_annotation;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class Career {
9+
@Getter
10+
@Setter
11+
private String jobType;
12+
13+
@Getter
14+
@Setter
15+
private int jobLevel;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package spring_atResource_annotation;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class City {
9+
@Getter
10+
@Setter
11+
private String cityName;
12+
13+
@Getter
14+
@Setter
15+
private String area;
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package spring_atResource_annotation;
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 @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+
public class MainApp {
14+
15+
public static void main(String... args) {
16+
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring_resource_annotation/resource_annotation_config.xml");
17+
Person p =(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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package spring_atResource_annotation;
2+
3+
import javax.annotation.Resource;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import lombok.Getter;
9+
import lombok.Setter;
10+
11+
public class Person {
12+
@Resource(name = "home123")
13+
@Getter
14+
private City city;
15+
16+
@Resource(name = "wife")
17+
@Getter
18+
private Wife wife;
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+
private Career career;
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package spring_atResource_annotation;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
@ToString
8+
public class Wife {
9+
10+
@Getter
11+
@Setter
12+
private String name;
13+
14+
@Getter
15+
@Setter
16+
private String faith;
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
<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+
<bean id="person" class="spring_resource_annotation.Person">
14+
</bean>
15+
<bean id="home123" class="spring_resource_annotation.City">
16+
<property name="cityName" value="San Jose"/>
17+
<property name="area" value="NorCal"/>
18+
</bean>
19+
<bean id="wife" class="spring_resource_annotation.Wife">
20+
<property name="name" value="Sophie Yan"/>
21+
<property name="faith" value="Christian"/>
22+
</bean>
23+
<bean id = "careerCurrent" class="spring_resource_annotation.Career">
24+
<property name="jobType" value="SDE"/>
25+
<property name="jobLevel" value="4"/>
26+
</bean>
27+
<bean id="careerFuture" class="spring_resource_annotation.Career">
28+
<property name="jobType" value="SDE"/>
29+
<property name="jobLevel" value="5"/>
30+
</bean>
31+
32+
</beans>

0 commit comments

Comments
 (0)