赞
踩
学习目的:在spring初识和spring注入对象的学习中,主要是用到的XML配置文件,在本小节学习中,学会使用注解方式实现注入对象。
修改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>

修改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;
- }
-
- }

运行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());
- }
- }
使用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配置文件,也通过注解进行呢?
修改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>

为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; } }
总结
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。