Part 1
学习目的:在spring初识和spring注入对象的学习中,主要是用到的XML配置文件,在本小节学习中,学会使用注解方式实现注入对象。
Part 2
修改XML配置文件,在第一个bean前面增加一行 <context:annotation-config/>,目的是告诉spring要使用注解的方式进行配置,同时将bean c中的 <property name="category" ref="c" />进行注释,因为要使用注解的方式进行注入对象。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean name="c" class="cn.vaefun.pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="p" class="cn.vaefun.pojo.Product">
<property name="name" value="product 1" />
<!-- <property name="category" ref="c" /> -->
</bean>
</beans>
Part 3
修改cn.vaefun.pojo.Product类,在category属性前加@Autowired注解(在setCategory方法前加注解也可以),注意导包:import org.springframework.beans.factory.annotation.Autowired。
package cn.vaefun.pojo;
import org.springframework.beans.factory.annotation.Autowired;
public class Product {
private String name;
@Autowired
private Category category;
public Category getCategory() {
return category;
}
//@Autowired
public void setCategory(Category category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Part 4
运行TestSpring,成功获取到Product对象以及注入的Category对象。
package cn.vaefun.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.vaefun.pojo.Product;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
Product product = (Product)context.getBean("p");
System.out.println(product.getName());
System.out.println(product.getCategory().getName());
}
}

Part 5
使用Resource注解完成上述需求,同上修改cn.vaefun.pojo.Product类(XML文件与Part 2中保持一致),在category属性前加@Resource注解,同时告诉spring该属性是bean c(@Resource(name ="c")),注意导包import javax.annotation.Resource。运行测试也可以得到Part 4中的结果。
package cn.vaefun.pojo;
import javax.annotation.Resource;
//import org.springframework.beans.factory.annotation.Autowired;
public class Product {
private String name;
//@Autowired
@Resource(name = "c")
private Category category;
public Category getCategory() {
return category;
}
//@Autowired
public void setCategory(Category category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
上述例子是对 注入对象行为的注解,那么bean对象本身,比如Category,Product可不可以移出applicationContext.xml配置文件,也通过注解进行呢?
Part 6
修改applicationContext.xml配置文件,将配置文件的所有配置删除,并新增一句<context:component-scanbase-package="cn.vaefun.pojo"/>,目的是告诉spring,所有的bean对象都在cn.vaefun.pojo这个包里。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="cn.vaefun.pojo"/>
</beans>
Part 7
为Product、Category添加@Component注解,并分别命名为"p"、"c",同时分别为其对应的name赋值为product 1、category 1。运行TestSpring,得到与Part 4相同的结果。
package cn.vaefun.pojo;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
//import org.springframework.beans.factory.annotation.Autowired;
@Component("p")
public class Product {
private String name = "product 1";
//@Autowired
@Resource(name = "c")
private Category category;
public Category getCategory() {
return category;
}
//@Autowired
public void setCategory(Category category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
———————————————分割线——————————————————————————
package cn.vaefun.pojo;
import org.springframework.stereotype.Component;
@Component("c")
public class Category {
private int id;
private String name = "category 1";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Part 8
总结
- @Autowired注解可以放在属性上,也可以放在setter上,要配合配置了bean的xml配置文件使用,需要导入org.springframework.beans.factory.annotation.Autowired包。
- @Resource注解放在属性上使用,并指明该属性对应xml配置文件中的哪个bean(如:@Resource(name = "c")),需要导入javax.annotation.Resource包。
- @Componet注解放在类名上使用,并为该bean类进行命名(如:@Component("c")),且需要在xml配置文件中添加<context:component-scanbase-package="cn.vaefun.pojo"/>,用以告诉spring所有bean类所在的包,需要导入 org.springframework.stereotype.Component包。