1.修改spring-config.xml文件
添加<context:annotation-config/>,表示告訴spring要用注解的方式進(jìn)行配置
<context:annotation-config/>
- 注釋掉<property name="category" ref="category" />,該行為在后面將使用注解來完成
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="category" class="com.jd.java.Category">
<property name="name" value="Spring"></property>
</bean>
<!--在創(chuàng)建Product的時候注入一個Category對象, 注意,這里要使用ref來注入另一個對象-->
<bean name="p" class="com.jd.java.Product">
<property name="name" value="product" />
<!--<property name="category" ref="category" />-->
</bean>
</beans>
3.@Autowired
在Product.java的category屬性前加上@Autowired注解

4.測試運(yùn)行
package com.jd.test;
import com.jd.java.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testSpring {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Product p1 = (Product) context.getBean("p");
System.out.println(p1.getName());
System.out.println(p1.getCategory().getName());
}
}

- @Autowired的位置
除了前面的 在屬性前加上@Autowired 這種方式外,也可以在setCategory方法前加上@Autowired,這樣來達(dá)到相同的效果
- @Resource
除了@Autowired之外,@Resource也是常用的手段
@Resource(name = "category")
private Category category;
7.對Bean的注解
上述例子是對注入對象行為的注解,那么bean對象本身,比如Category,Product可不可以移出spring-config.xml配置文件,也通過注解進(jìn)行呢?接下來是如何對Bean進(jìn)行注解配置
8.修改spring-config.xml文件
新增<context:component-scan base-package="com.jd.java"/>,其作用是告訴Spring,bean都放在com.jd.java這個包下
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.jd.java"/>
</beans>
9.@Component
為Product類加上@Component注解,即表明此類是bean
@Component("p")
public class Product {
為Category 類加上@Component注解,即表明此類是bean
@Component("category")
public class Category {
因?yàn)榕渲脧膕pring-config.xml中移出來了,所以屬性初始化放在屬性聲明上進(jìn)行了。
private String name="product";
private String name="spring";
Product.java
package com.jd.java;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("p")
public class Product {
private int id;
private String name = "product";
@Autowired
private Category category;
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;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
Category.java
package com.jd.java;
import org.springframework.stereotype.Component;
@Component("category")
public class Category {
private int id;
private String name = "spring";
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
}
10.測試運(yùn)行