Spring注解IOC/DI(4)

2019-03-10 11:55:36来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

2019-03-08/11:10:17

演示:使用注解的方式完成注入对象中的效果

注解参考链接:https://www.cnblogs.com/szlbm/p/5512931.html

Spring中id与name的区别:https://blog.csdn.net/qq_22063697/article/details/51912386

1.修改applicationContext.xml

 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" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6    http://www.springframework.org/schema/beans
 7    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8    http://www.springframework.org/schema/aop
 9    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
10    http://www.springframework.org/schema/tx
11    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12    http://www.springframework.org/schema/context     
13    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14   
15     <context:annotation-config/>   //告诉Spring用注解的方式进行配置
16     <bean name="c" class="pojo.Category">
17         <property name="name" value="category 1" />
18     </bean>
19     <bean name="p" class="pojo.Product">
20         <property name="name" value="product1" />
21 <!--         <property name="category" ref="c" /> -->   //用注解的方式替代
22     </bean>
23   
24 </beans>

 

2.在Product.java的category属性前加上@Autowired注解

  @Autowired:顾名思义,就是自动装配,其作用是为了消除代码Java代码里面的getter/setter与bean属性中的property。当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。例子中统一不去掉get和set.

 1 package pojo;
 2  
 3 import org.springframework.beans.factory.annotation.Autowired;
 4  
 5 public class Product {
 6  
 7     private int id;
 8     private String name;
 9     @Autowired
10     private Category category;
11  
12     public int getId() {
13         return id;
14     }
15  
16     public void setId(int id) {
17         this.id = id;
18     }
19  
20     public String getName() {
21         return name;
22     }
23  
24     public void setName(String name) {
25         this.name = name;
26     }
27  
28     public Category getCategory() {
29         return category;
30     }
31  
32     public void setCategory(Category category) {
33         this.category = category;
34     }
35 }
Product.java

 

3.测试代码

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

 

4.@Autowired的位置

除了前面的在属性前加上@Autowired这种方式外,也可以在setCategory方法前加上@Autowired,这样来达到相同的效果

 1 package pojo;
 2  
 3 import org.springframework.beans.factory.annotation.Autowired;
 4  
 5 public class Product {
 6  
 7     private int id;
 8     private String name;
 9      
10     private Category category;
11  
12     public int getId() {
13         return id;
14     }
15  
16     public void setId(int id) {
17         this.id = id;
18     }
19  
20     public String getName() {
21         return name;
22     }
23  
24     public void setName(String name) {
25         this.name = name;
26     }
27  
28     public Category getCategory() {
29         return category;
30     }
31     @Autowired
32     public void setCategory(Category category) {
33         this.category = category;
34     }
35 }
View Code

 

5.@Resource

@Resource注解与@Autowired它们作用非常相似,这个就简单说了,例子过后点明一下@Resource和@Autowired的区别。先看一下@Resource,直接写Product.java了:

 1 package pojo;
 2  
 3 import javax.annotation.Resource;
 4  
 5 public class Product {
 6  
 7     private int id;
 8     private String name;
 9     @Resource(name="c")
10     private Category category;
11  
12     public int getId() {
13         return id;
14     }
15  
16     public void setId(int id) {
17         this.id = id;
18     }
19  
20     public String getName() {
21         return name;
22     }
23  
24     public void setName(String name) {
25         this.name = name;
26     }
27  
28     public Category getCategory() {
29         return category;
30     }
31      
32     public void setCategory(Category category) {
33         this.category = category;
34     }
35 }
Product.java

 

6.@Resource与@Autowired 

@Resource的装配顺序:

1、@Resource后面没有任何内容,默认通过name属性去匹配bean,找不到再按type去匹配

2、指定了name或者type则根据指定的类型去匹配bean

3、指定了name和type则根据指定的name和type去匹配bean,任何一个不匹配都将报错

然后,区分一下@Autowired和@Resource两个注解的区别:

1、@Autowired默认按照byType方式进行bean匹配,@Resource默认按照byName方式进行bean匹配

2、@Autowired是Spring的注解,@Resource是J2EE的注解,这个看一下导入注解的时候这两个注解的包名就一清二楚了

Spring属于第三方的,J2EE是Java自己的东西,因此,建议使用@Resource注解,以减少代码和Spring之间的耦合。

 

7.对Bean的注解  @Component

上面这个例子,还可以继续简化,因为spring的配置文件里面还有16行~20行两个个bean,下一步的简化是把这两个bean也给去掉,使得spring配置文件里面只有一个自动扫描的标签,增强Java代码的内聚性并进一步减少配置文件。

要继续简化,可以使用@Componet。先看一下配置文件,当然是全部删除了:

<context:component-scan base-package="pojo"/>        其作用时告诉Spring,bean都放在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" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="
 6    http://www.springframework.org/schema/beans
 7    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 8    http://www.springframework.org/schema/aop
 9    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
10    http://www.springframework.org/schema/tx
11    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
12    http://www.springframework.org/schema/context     
13    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
14   
15     <context:component-scan base-package="pojo"/>
16      
17 </beans>

 

8.@Component

  Product类加上@Component注解,即表明此类是bean       @Autowired注入Category对象不能取消

  Category类加上@Component注解,即表明此类是bean

  另外,因为配置从applicationContext.xml中移出来了,所以属性初始化放在属性声明上进行了。

   

 1 package pojo;
 2  
 3 import javax.annotation.Resource;
 4  
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Component;
 7  
 8 @Component("p")
 9 public class Product {
10  
11     private int id;
12     private String name="product 1";
13      
14     @Autowired
15     private Category category;
16  
17     public int getId() {
18         return id;
19     }
20  
21     public void setId(int id) {
22         this.id = id;
23     }
24  
25     public String getName() {
26         return name;
27     }
28  
29     public void setName(String name) {
30         this.name = name;
31     }
32  
33     public Category getCategory() {
34         return category;
35     }
36      
37     public void setCategory(Category category) {
38         this.category = category;
39     }
40 }
Product.java
 1 package pojo;
 2  
 3 import org.springframework.stereotype.Component;
 4  
 5 @Component("c")
 6 public class Category {
 7     
 8     private int id;
 9     private String name="category 1";
10   
11     public int getId() {
12         return id;
13     }
14     public void setId(int id) {
15         this.id = id;
16     }
17     public String getName() {
18         return name;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23    
24 }
Category.java
 1 package test;
 2  
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5  
 6 import pojo.Product;
 7  
 8 public class TestSpring {
 9  
10     public static void main(String[] args) {
11         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
12         Product p = (Product) context.getBean("p");
13         System.out.println(p.getName());
14         System.out.println(p.getCategory().getName());
15     }
16 }
TestSpring.java

 


原文链接:https://www.cnblogs.com/bencoper/p/10494964.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:浅谈SpringMVC执行过程

下一篇:Spring(1)