Spring中如何使用注解的,以@Resource为例

在前面,看到自定义注解,并且也简单的使用了一下,

然后就再次用个简单的例子,来看看s,pring里面是如何使用注解的。如下:

先看J,ava代码:简单,就是2个bean和一个主方法。

[java] view plain copy
  1. import org.springframework.context.ApplicationContext;  
  2. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  3.   
  4. import javax.annotation.Resource;  
  5.   
  6. class Student {  
  7.     void say() {  
  8.         System.out.println("hello");  
  9.     }  
  10. }  
  11.   
  12. class Person {  
  13.     @Resource(name="student")  
  14.     private Student student;  
  15.   
  16.     //Access can be package-private  
  17.     //所以方法的 public就不要啦  
  18.     void say(){  
  19.         this.student.say();  
  20.     }  
  21. }  
  22. /** 
  23.  * Created by lxk on 2016/9/29 
  24.  */  
  25. class AtInterfaceTest {  
  26.     public static void main(String[] args) {  
  27.         //ApplicationContext ctx = new ClassPathXmlApplicationContext("file:E:/xxx/intellij_work/TrunkNew/sss.xml");  
  28.         ApplicationContext ctx = new FileSystemXmlApplicationContext("sss.xml");  
  29.         Person p = (Person) ctx.getBean("person");  
  30.         p.say();  
  31.     }  
  32. }  


注意上面的Person里面的student属性是没有getter和setter的。但是在测试main方法里面确直接可以使用say方法,这个方法里面的student对象何来?


然后是配置文件:

[html] view plain copy
  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:context="http://www.springframework.org/schema/context"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context  
  8.         http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.     <!--  
  10.             (只有下面的是需要自己添加的,其他的都是在新建spring配置xml文件的时候,就自带的啦)  
  11.             1、导入基于注解的xsd  
  12.                  xmlns:context="http://www.springframework.org/schema/context"  
  13.                     http://www.springframework.org/schema/context  
  14.                     http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  15.           2、导入注解解析器  
  16.               <context:annotation-config></context:annotation-config>  
  17.              3、导入person和student  
  18.      -->  
  19.   
  20.     <context:annotation-config/>  
  21.   
  22.     <bean id="person" class="com.xxx.x.model.s.Person"/>  
  23.     <bean id="student" class="com.xxx.x.model.s.Student"/>  
  24.   
  25. </beans>  

关于配置文件里面的东西,可以翻看以前的spring分类里面的东西。
简单介绍:
<bean>里面的class对应model的全路径。
id命名最好是类名的首字母缩写。

这个配置超简单了点,就是示例而已。用的东西不多。


执行结果的图:

Spring中如何使用注解的,以@Resource为例



上面关于ApplicationContext 的初始化的问题,以及该如何使用,下次再说吧。

如下:

关于初始化ApplicationContext时报错怎么解决


对上面的文章做一下补充:加深对注解的工作原理的理解(这次是重点)

可以做如下修改:(直接上代码吧,来的快些)

[java] view plain copy
  1. //1.一般正常形式(测试结果:正常)  
  2. class Person {  
  3.     @Resource(name = "student")  
  4.     private Student student;  
  5.     void say(){ this.student.say(); }  
  6. }  
  7.   
  8. //2.删除注解后面的括号内容(测试结果:正常)  
  9. class Person {  
  10.     @Resource  
  11.     private Student student;  
  12.     void say(){ this.student.say(); }  
  13. }  
  14.   
  15. //3.在上面的基础上修改属性名称(测试结果:正常)  
  16. class Person {  
  17.     @Resource  
  18.     private Student ss;  
  19.     void say(){ this.ss.say(); }  
  20. }  
  21.   
  22. //4.在上面的基础上修改括号内容(测试结果:失败)  
  23. class Person {  
  24.     @Resource(name="ss")  
  25.     private Student ss;  
  26.     void say(){ this.ss.say(); }  
  27. }  
  28.   
  29. //5.在上面的基础上修改括号内容和配置文件bean的id为ss(测试结果:正常)  
  30. class Person {  
  31.     @Resource(name="ss")  
  32.     private Student ss;  
  33.     void say(){ this.ss.say(); }  
  34. }  
  35. <bean id="ss" class="com.xxx.x.model.s.Student"/>  
  36.   
  37. //6.在上面的基础上修改注解(测试结果:正常)  
  38. class Person {  
  39.     @Autowired  
  40.     private Student ss;  
  41.     void say(){ this.ss.say(); }  
  42. }  
  43.   
  44. //7.在上面的基础上修改注解(测试结果:正常)  
  45. class Person {  
  46.     //下面2个注解的作用相当于 @Resource(name="ss") 一个  
  47.     //区别在于@Resource是javax的,下面2个是spring自己的  
  48.     @Autowired  
  49.     @Qualifier(value = "ss")  
  50.     private Student ss;  
  51.     void say(){ this.ss.say(); }  
  52. }  
  53.   
  54. //然后看@Resource的源码部分如下:  
  55. public @interface Resource {  
  56.     String name() default "";//解释了在使用注解不写name = "xxx"的时候,默认是""这个值,  
  57.     。。。。  
  58. }  


具体总结如下:

[java] view plain copy
  1. 工作原理:  
  2.   当spring容器启动的时候,  
  3.   ApplicationContext ctx = new FileSystemXmlApplicationContext("sss.xml");  
  4.   spring容器会创建纳入spring容器管理的bean.分别为person和student;   
  5.   spring容器会解析配置文件,会解析到<context:annotation-config/> 会在纳入spring的bean范围内查找属性上是否存在  
  6.   注解@Resource(name="student")  
  7.        * 如果存在:  
  8.            * 继续解析@Resource有没有name属性  
  9.                * 如果没有name属性  
  10.                          就会在所属的属性上,把属性的名称解析出来。会让属性的名称和spring中的bean中的id进行匹配  
  11.                                    如果匹配成功,则把spring容器中相应的对象赋值给该属性  
  12.                                    如果匹配失败,则按照类型(Class)进行匹配  
  13.                * 如果有name属性  
  14.                          就会解析name属性的值,把这个值和spring容器中的bean的id进行匹配  
  15.                        如果匹配成功,则把spring容器中的相应的对象赋值给该属性  
  16.                        如果匹配失败,则直接报错  
  17.            
  18.        * 如果不存在:  
  19.               不做任何事情  
  20.                 
  21. xml注入属性和注解注入属性的写法的对比:  
  22.     xml : 书写比较麻烦,但是效率比较高(直接在配置文件里面全有啦)  
  23.     注解:书写比较简单,但是效率比较低(一遍遍的扫描)  
  24. 注解的写法只适合引用  

再有总结,如下:

Spring中如何使用注解的,以@Resource为例