Spring4.0系列4-Meta Annotation(元注解)
Spring4.0系列6-Generic Qualifier(泛型限定)
Spring4.0系列7-Ordering Autowired Collections
更多正在编写中。。。
Spring框架自2.0开始添加注解的支持,之后的每个版本都增加了更多的注解支持。注解为依赖注入,AOP(如事务)提供了更强大和简便的方式。这也导致你要是用一个相同的注解到许多不同的类中去。这篇文章介绍meta annotation来解决这个问题。
Meta Annotation(元注解)定义
Spring4.0的许多注解都可以用作meta annotation(元注解)。元注解是一种使用在别的注解上的注解。这意味着我们可以使用Spring的注解组合成一个我们自己的注解。
创建组合注解
自定义注解或组合注解是从其他的Spring元注解创建的。这种注解分为两类:
- 1、只是为了编码简单将多个注解组合成一个注解;
- 2、可以定义一个可复用的注解,这个注解可以解决问题,但是不用记住所有单独的注解。
如以下的DAO操作:
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Repository @Scope("prototype") @Transactional(propagation = Propagation.REQUIRES_NEW, timeout = 30, isolation=Isolation.SERIALIZABLE) public class OrderDaoImpl implements OrderDao { ... }
没有任何的业务代码,但是类上注解的申明就让代码有10几行之多。为了简化DAO,我们提供一个组合DAO注解,使用Spring元注解来自定义一个注解。
import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Repository @Scope("prototype") @Transactional(propagation = Propagation.REQUIRES_NEW, timeout = 30, isolation=Isolation.SERIALIZABLE) public @interface MyDao { }
使用这个自定义/组合注解,我们可以移除掉DAO上的许多注解:
@MyDao public class OrderDaoImpl implements OrderDao { ... }
注解的属性
大部分情况下,注解是需要属性的。自定义的注解也可以提供属性。比如:@Repositoy可以提供一个name属性。
@Repository("myOrderDao")
由于自定义的@MyDao可能被很多DAO使用,所以对@Repository的name硬编码是不行的。所以我们简单的添加一个value属性到@MyDao上(和@Repository中的相同的value属性)。
@Repository @Scope("prototype") @Transactional(propagation = Propagation.REQUIRES_NEW, timeout = 30, isolation=Isolation.SERIALIZABLE) public @interface MyDao { String value(); }
用新的自定义注解定义bean如下:
@MyDao("myOrderDao") public class OrderDaoImpl implements OrderDao { ... }
更多关于元数据注解的问题访问这里:Srping Framework Reference Documentation
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
相关推荐
NULL 博文链接:https://tonl.iteye.com/blog/2093289
NULL 博文链接:https://tonl.iteye.com/blog/1966822
spring-security-helloworld-annotation
这里zip压缩包囊括了学习Spring过程中用到的所有的jar包; 有: ...spring-web-4.1.2.RELEASE.jar //aop注解需要 对于Spring环境的插件,你们可以到Eclipase中的help>Eclipse marketplace...去搜索安装
point-cloud-annotation-tool win10解压直接运行
JsonAnnotation利用注解自动生成Gson‘s Model的库.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis(注解版)多数据源最简解决方案 spring-boot-thymeleaf:simple spring boot thymeleaf demo spring-boot-jpa-thymeleaf-curd:spring boot + ...
spring-boot-mybatis-annotation-mulidatasource:springboot+mybatis(注解版)多数据源最简解决方案 spring-boot-thymeleaf:simple spring boot thymeleaf demo spring-boot-jpa-thymeleaf-curd:spring boot +...
github链接链接:https://github.com/springzfx/point-cloud-annotation-tool 在windows平台编译完成后的exe和dll文件,可以直接打开使用,并对原项目进行了优化,增加了列表和标注文件的显示功能,标注起来更快,...
spring的Annotation注解.
赠送jar包:mybatis-plus-annotation-3.2.0.jar; 赠送原API文档:mybatis-plus-annotation-3.2.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.2.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
赠送jar包:geronimo-annotation_1.0_spec-1.1.1.jar; 赠送原API文档:geronimo-annotation_1.0_spec-1.1.1-javadoc.jar; 赠送源代码:geronimo-annotation_1.0_spec-1.1.1-sources.jar; 赠送Maven依赖信息文件:...
Spring注解驱动开发实战 组件注册,生命周期,自动装配
赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
boot-mybatis-annotation-mulidatasource](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-annotation-mulidatasource):Spring Boot + Mybatis(注解版...
spring-boot实现原理 ...spring-boot-alias-annotation-sample注解属性别名模块 spring-boot-actuator-sample监控组件模块 spring-boot-enable-sample自动装配模块 formatter-spring-boot-starter-sample自定义启动模
赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
通过spring boot mybatis 连接多数据库
赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...