Spring注解原理的详细剖析与实现
2016-05-25 08:47
561 查看
Spring注解原理的详细剖析与实现
本文主要分为三部分:一、注解的基本概念和原理及其简单实用
二、
三、编码剖析spring@Resource的实现原理
一、注解的基本概念和原理及其简单实用
注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析注解来使用这些数据),用来将任何的信息或者元数据与程序元素(类、方法、成员变量等)进行关联。其实就是更加直观更加明了的说明,这些说明信息与程序业务逻辑没有关系,并且是供指定的工具或框架使用的。Annotation像一种修饰符一样,应用于包、类型、构造方法、方法、成员变量、参数及本地变量的申明语句中。
Annotation其实是一种接口。通过
Annotation和interface的异同:
1、
annotition的类型使用关键字@interface而不是interface。它继承了java.lang.annotition.Annotition接口,并非申明了一个interface。
2、
Annotation类型、方法定义是独特的、受限制的。Annotation类型的方法必须申明为无参数、无异常抛出的。这些方法定义了Annotation的成员:方法名称为了成员名,而方法返回值称为了成员的类型。而方法返回值必须为primitive类型、Class类型、枚举类型、Annotation类型或者由前面类型之一作为元素的一位数组。方法的后面可以使用default和一个默认数值来申明成员的默认值,null不能作为成员的默认值,这与我们在非Annotation类型中定义方法有很大不同。Annotation类型和他的方法不能使用Annotation类型的参数,成员不能是generic。只有返回值类型是Class的方法可以在Annotation类型中使用generic,因为此方法能够用类转换将各种类型转换为Class。
3、
Annotation类型又与接口有着近似之处。它们可以定义常量、静态成员类型(比如枚举类型定义)。Annotation类型也可以如接口一般被实现或者继承。
*元注解@Target,@Retention,@Documented,@Inherited
*
*@Target表示该注解用于什么地方,可能的ElemenetType参数包括:
*ElemenetType.CONSTRUCTOR构造器声明
*ElemenetType.FIELD域声明(包括enum实例)
*ElemenetType.LOCAL_VARIABLE局部变量声明
*ElemenetType.METHOD方法声明
*ElemenetType.PACKAGE包声明
*ElemenetType.PARAMETER参数声明
*ElemenetType.TYPE类,接口(包括注解类型)或enum声明
*
*@Retention表示在什么级别保存该注解信息。可选的RetentionPolicy
参数包括:
*RetentionPolicy.SOURCE注解将被编译器丢弃
*RetentionPolicy.CLASS注解在class文件中可用,但会被VM丢弃
*RetentionPolicy.RUNTIMEVM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
*@Documented将此注解包含在javadoc中
*
*@Inherited允许子类继承父类中的注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
----------------------------------------Demo----------------------------------------------------
packagecom.wxy.annotation;
importjava.lang.annotation.Documented;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Inherited;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
/**
*定义注解Test
*注解中含有两个元素id和description
*description元素有默认值"nodescription"
*@create-time2011-8-12下午02:22:28
*@revision$Id
*/
//该注解用于方法声明
@Target(ElementType.METHOD)
//VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
@Retention(RetentionPolicy.RUNTIME)
//将此注解包含在javadoc中
@Documented
//允许子类继承父类中的注解
@Inherited
public@interfaceTest{
publicintid();
publicStringdescription()default"nodescription";
}
<strong>packagecom.wxy.annotation;</strong>
importjava.lang.reflect.Method;
/**
*使用注解和解析注解
*
*@creatorxiaoyu.wang
*@create-time2011-8-12下午03:49:17
*@revision$Id
*/
publicclassTest_1{
/**
*被注释的三个方法
*/
@Test(id=1,description="hellomethod1")
publicvoidmethod1(){
}
@Test(id=2)
publicvoidmethod2(){
}
@Test(id=3,description="lastmethod3")
/**
*解析注释,将Test_1类所有被注解方法的信息打印出来
*@paramargs
*/
publicstaticvoidmain(String[]args){
Method[]methods=Test_1.class.getDeclaredMethods();
for(Methodmethod:methods){
//判断方法中是否有指定注解类型的注解
booleanhasAnnotation=method.isAnnotationPresent(Test.class);
if(hasAnnotation){
//根据注解类型返回方法的指定类型注解
Testannotation=method.getAnnotation(Test.class);
System.out.println("Test(method="+method.getName()+",id="+annotation.id()
+",description="+annotation.description()+")");
}
}
}
}
Spring@Resource注解实现原理编码分析
二、spring中注解@Resource的使用
1、修改bean.xml,引入命名空间(保证项目能引用到common-annotations.jar)
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns=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-2.5.xsd
http://www.springframework.org/schema/context
'target='_blank'>http://www.springframework.org/schema/context/spring-context-2.5.xsd">[/code] <context:annotation-config/><beanid="peopleDao"class="com.wxy.dao.impl.PeopleDaoBean"></bean><beanid="peopleService"class="com.wxy.service.impl.PeopleServiceBean"</bean></beans>
2、在PeopleServiceBean中使用@Resource注解publicclassPeopleServiceBeanimplementsPeopleService{@ResourceprivatePeopleDaopeopleDao;privateStringname="wxy";privateIntegerid=1;…}
3、测试:publicclassTest{publicstaticvoidmain(String[]args){//IOC容器实例化ClassPathXmlApplicationContextac=newClassPathXmlApplicationContext("beans.xml");PeopleServicepeopleService=(PeopleService)ac.getBean("peopleService");peopleService.save();}}
4、结果:-->themethodiscalledsave()!name=wxy,id=1thisisthemethodPeopleDaoBean.add()!
也可以在setter方法上使用@Resource:publicclassPeopleServiceBeanimplementsPeopleService{privatePeopleDaopeopleDao;privateStringname="wxy";privateIntegerid=1;/***@returnthepeopleDao*/publicPeopleDaogetPeopleDao(){returnpeopleDao;}/***@parampeopleDaothepeopleDaotoset*/@ResourcepublicvoidsetPeopleDao(PeopleDaopeopleDao){this.peopleDao=peopleDao;}…..}
结果一样。
三、@Resource注解的实现原理(只用于配置,不用于干活)
1、新建Annotation类型文件packagecom.wxy.annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD,ElementType.METHOD})public@interfaceWxyResource{Stringname()default"";}
2、使用自定义的注解@WxyResourcepublicclassPeopleServiceBeanimplementsPeopleService{privatePeopleDaopeopleDao;privateStringname="wxy";privateIntegerid=1;/***@parampeopleDaothepeopleDaotoset*/@WxyResourcepublicvoidsetPeopleDao(PeopleDaopeopleDao){this.peopleDao=peopleDao;}…}
3、事实上上一步没有实现,这一步开始实现注入原理,在自定义spring容器中添加注解功能publicclassWxyClassPathXMLApplicationContext{//存放BeanDefinition的列表,在beans.xml中定义的bean可能不止一个privatefinalList<BeanDefinition>beanDefines=newArrayList<BeanDefinition>();//将类名作为索引,将创建的Bean对象存入到Map中privatefinalMap<String,Object>sigletons=newHashMap<String,Object>();publicWxyClassPathXMLApplicationContext(StringfileName){//读取xml配置文件this.readXML(fileName);//实例化beanthis.instanceBeans();//处理注解方式this.annotationInject();//注入对象this.injectObject();}/***使用注解方式注入对象方法实现*@throwsIntrospectionException*/privatevoidannotationInject(){//循环所有bean对象for(StringbeanName:sigletons.keySet()){//获取bean对象Objectbean=sigletons.get(beanName);//如果bean不为空,取得bean的属性if(bean!=null){try{//按属性注入PropertyDescriptor[]ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();for(PropertyDescriptorproperdesc:ps){//获取属性的setter方法Methodsetter=properdesc.getWriteMethod();//判断注解是否存在if(setter!=null&&setter.isAnnotationPresent(WxyResource.class)){//取得注解WxyResourceresource=setter.getAnnotation(WxyResource.class);Objectvalue=null;//如果按名字找到if(resource.name()!=null&&!"".equals(resource.name())){//取得容器中的bean对象value=sigletons.get(resource.name());}else{//没有按名字找到,按类型寻找//取得容器中的bean对象value=sigletons.get(resource.name());if(value==null){for(Stringkey:sigletons.keySet()){if(properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){value=sigletons.get(key);break;}}}}//把引用对象注入到属性setter.setAccessible(true);setter.invoke(bean,value);}}//按字段注入Field[]fields=bean.getClass().getDeclaredFields();for(Fieldfield:fields){//如果注解存在if(field.isAnnotationPresent(WxyResource.class)){//取得注解WxyResourceresource=field.getAnnotation(WxyResource.class);Objectvalue=null;//如果按名字找到if(resource.name()!=null&&!"".equals(resource.name())){//取得容器中的bean对象value=sigletons.get(resource.name());}else{//没有按名字找到,按类型寻找//取得容器中的bean对象value=sigletons.get(field.getName());if(value==null){for(Stringkey:sigletons.keySet()){if(field.getType().isAssignableFrom(sigletons.get(key).getClass())){value=sigletons.get(key);break;}}}}//允许访问privatefield.setAccessible(true);field.set(bean,value);}}}catch(IntrospectionExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IllegalArgumentExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IllegalAccessExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(InvocationTargetExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}/***为bean对象的属性注入值*/privatevoidinjectObject(){….}….}
4、使用自定义容器测试:publicclassMyTest{/***@paramargs*/publicstaticvoidmain(String[]args){//MyIOC容器实例化WxyClassPathXMLApplicationContextac=newWxyClassPathXMLApplicationContext("beans.xml");PeopleServicepeopleService=(PeopleService)ac.getBean("peopleService");peopleService.save();}}
6、测试结果:-->themethodiscalledsave()!name=wxy,id=1thisisthemethodPeopleDaoBean.add()!
注解本身不做任何事情,只是像xml文件一样起到配置作用。注解代表的是某种业务意义,注解背后处理器的工作原理如上源码实现:首先解析所有属性,判断属性上是否存在指定注解,如果存在则根据搜索规则取得bean,然后利用反射原理注入。如果标注在字段上面,也可以通过字段的反射技术取得注解,根据搜索规则取得bean,然后利用反射技术注入。
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 浅析Java中的set集合类型及其接口的用法