当前位置:   article > 正文

Java自学之spring:注解方式IOC/DI

category和product的装配方式改为注解方式

Part 1

学习目的:在spring初识spring注入对象的学习中,主要是用到的XML配置文件,在本小节学习中,学会使用注解方式实现注入对象。

Part 2

修改XML配置文件,在第一个bean前面增加一行 <context:annotation-config/>,目的是告诉spring要使用注解的方式进行配置,同时将bean c中的 <property name="category" ref="c" />进行注释,因为要使用注解的方式进行注入对象。

  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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16. <context:annotation-config/>
  17. <bean name="c" class="cn.vaefun.pojo.Category">
  18. <property name="name" value="category 1" />
  19. </bean>
  20. <bean name="p" class="cn.vaefun.pojo.Product">
  21. <property name="name" value="product 1" />
  22. <!-- <property name="category" ref="c" /> -->
  23. </bean>
  24. </beans>

Part 3

修改cn.vaefun.pojo.Product类,在category属性前加@Autowired注解(在setCategory方法前加注解也可以),注意导包:import org.springframework.beans.factory.annotation.Autowired。

  1. package cn.vaefun.pojo;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class Product {
  4. private String name;
  5. @Autowired
  6. private Category category;
  7. public Category getCategory() {
  8. return category;
  9. }
  10. //@Autowired
  11. public void setCategory(Category category) {
  12. this.category = category;
  13. }
  14. public String getName() {
  15. return name;
  16. }
  17. public void setName(String name) {
  18. this.name = name;
  19. }
  20. }

Part 4

运行TestSpring,成功获取到Product对象以及注入的Category对象。

  1. package cn.vaefun.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import cn.vaefun.pojo.Product;
  5. public class TestSpring {
  6. public static void main(String[] args) {
  7. ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
  8. Product product = (Product)context.getBean("p");
  9. System.out.println(product.getName());
  10. System.out.println(product.getCategory().getName());
  11. }
  12. }

v2-52199158176e55a32774246bff15e797_b.jpg

Part 5

使用Resource注解完成上述需求,同上修改cn.vaefun.pojo.Product类(XML文件与Part 2中保持一致),在category属性前加@Resource注解,同时告诉spring该属性是bean c(@Resource(name ="c")),注意导包import javax.annotation.Resource。运行测试也可以得到Part 4中的结果。

  1. package cn.vaefun.pojo;
  2. import javax.annotation.Resource;
  3. //import org.springframework.beans.factory.annotation.Autowired;
  4. public class Product {
  5. private String name;
  6. //@Autowired
  7. @Resource(name = "c")
  8. private Category category;
  9. public Category getCategory() {
  10. return category;
  11. }
  12. //@Autowired
  13. public void setCategory(Category category) {
  14. this.category = category;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. }

上述例子是对 注入对象行为的注解,那么bean对象本身,比如Category,Product可不可以移出applicationContext.xml配置文件,也通过注解进行呢?

Part 6

修改applicationContext.xml配置文件,将配置文件的所有配置删除,并新增一句<context:component-scanbase-package="cn.vaefun.pojo"/>,目的是告诉spring,所有的bean对象都在cn.vaefun.pojo这个包里。

  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:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16. <context:component-scan base-package="cn.vaefun.pojo"/>
  17. </beans>

Part 7

为Product、Category添加@Component注解,并分别命名为"p"、"c",同时分别为其对应的name赋值为product 1、category 1。运行TestSpring,得到与Part 4相同的结果。

  1. package cn.vaefun.pojo;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Component;
  4. //import org.springframework.beans.factory.annotation.Autowired;
  5. @Component("p")
  6. public class Product {
  7. private String name = "product 1";
  8. //@Autowired
  9. @Resource(name = "c")
  10. private Category category;
  11. public Category getCategory() {
  12. return category;
  13. }
  14. //@Autowired
  15. public void setCategory(Category category) {
  16. this.category = category;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. }
  25. ———————————————分割线——————————————————————————
  26. package cn.vaefun.pojo;
  27. import org.springframework.stereotype.Component;
  28. @Component("c")
  29. public class Category {
  30. private int id;
  31. private String name = "category 1";
  32. public int getId() {
  33. return id;
  34. }
  35. public void setId(int id) {
  36. this.id = id;
  37. }
  38. public String getName() {
  39. return name;
  40. }
  41. public void setName(String name) {
  42. this.name = name;
  43. }
  44. }

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包。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/769195
推荐阅读
相关标签
  

闽ICP备14008679号