Spring的注解问题
Annotation(注解)概述
从JDK5.0开始, Java增加了对元数据(MetaData)的支持,也就是 Annotation(注解)。
Annotation其实就是代码里的特殊标记,它用于替代配置文件,也就是说,传统方式通过配置文件告诉类如何运行,有了注解技术后,开发人员可以通过注解告诉类如何运行。在Java技术里注解的典型应用是:可以通过反射技术去得到类里面的注解,以决定怎么去运行类。
1.自定义一个注解:
@Target({ElementType.TYPE,ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String temp() default "";
}
(1).@Retention– 定义该注解的生命周期
RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。
(2).@Target – 表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括
ElementType.CONSTRUCTOR:用于描述构造器
ElementType.FIELD:成员变量、对象、属性(包括enum实例)
ElementType.LOCAL_VARIABLE:用于描述局部变量
ElementType.METHOD:用于描述方法
ElementType.PACKAGE:用于描述包
ElementType.PARAMETER:用于描述参数
ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明
2.Spring使用注解。
在Spring核心配置文件配置:
<context:component-scan base-package="cn.*"/> <!-- 扫描cn包下的所有Bean -->
<context:component-scan base-package="cn.pojo,cn.test"/> <!-- 扫描指定包下的Bean,用逗号隔开 -->
如果扫描到的类上带有特定的注解(以下注解),这个对象就会交给Spring容器管理。
@Component:是所有受Spring容器管理的通用组件形式,可以放在类头上,并不推荐使用。
@Component("userDao")//这里通过注解定义了一个DAO
public class UserDaoImp implements IUserDao {
@Override
public int addUser(User user) {
//这里并未实现数据库操作,仅作为说明问题
System.out.println("新增用户操作");
return 0;
}
}
@Repository:用于标注DAO类。
@Repository("userDao")
public class UserDaoImp implements IUserDao {
@Override
public int addUser(User user) {
System.out.println("新增用户操作");
return 0;
}
}
在数据访问层使用此标注userdao类,然后在业务逻辑层实现注解的注入。
@Resource(name="userDao")
@Autowired
@Qualifier("userDao")
private IUserDao userDao;
JDK提供的注解:@Resource:根据name指定相应的dao类进行注入到userDao属性。
Spring框架内部提供的注解:@Autowired:根据type进行自动装载,可配合@Qualifier注解根据name值进行注入。
1.@Autowired也可以对方法进行入参进行注入:
private IUserDao userDao;
@Autowired
public void setUserDao(@Qualifier("userDao")IUserDao userDao) {
this.userDao = userDao;
}
2.使用@autowired注解进行装配时,如果找不到相匹配的Bean组件,Spring容器会抛出异常。如果依赖不是必须的,则可以将required的属性的设置为false。默认是为true的,即必须找到相匹配的Bean完成装配,否则就抛出一个异常。
private IUserDao userDao;
@Autowired(required = false)
public void setUserDao(@Qualifier("userDao")IUserDao userDao) {
this.userDao = userDao;
}
3.如果对类中集合类型的成员变量或方法入参使用@autowired注解,Spring会将容器中所有和集合中的元素类型进行匹配的Bean组件都注入进来。
@Autowired(required = false)
private List<User> list;//Spring容器会将User类型的Bean组件都注入给list属性。
@Service:用于标注业务类。
@Service("userService")
public class UserServiceImp implements IUserService {
@Resource(name="userDao")
private IUserDao userDao;
@Override
public int addUser(User user) {
return userDao.addUser(user);
}
}
@Service("userService")注解是告诉Spring,当Spring要创建UserServiceImpl的实例时,bean的名字必须叫做“userService”,这样当Action需要使用UserServiceImpl的实例时,
就可以由Spring创建好的“userService”然后注入给Action:在Action只需要声明一个名字叫“userService”的变量来接收由Spring注入的“userService”。
@Controller:用来标注控制器类,也就是Action。
使用注解定义切面
AspectJ:是一个面向切面的框架,扩展了Java语言,定义了AOP语法,能够在编译期提供代码的织入,所有它拥有一个专门的编译器用来生成遵守字节编码的规范Class文件。
@AspectJ是AspectJ 5新增的功能,使用JDK 5.0注解技术和正规的切点表达式语言描述切面。所以在使用之前,需要保证JDK版本是5.0以上版本,否则无法使用。
1.使用注解标注切面
package cn.advice; import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component("serviceAdvice")
@Aspect
public class ServiceLoggingAdvice {
private Logger logger = Logger.getLogger(ServiceLoggingAdvice.class);
//定义切入点---可使用模糊查询进行匹配
@Pointcut("execution(* cn.dao..*.*(..))")
public void pointcut(){}
//标注前置增强
@Before(value = "pointcut()")
public void before(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();//获得目标方法名
String className = joinPoint.getTarget().getClass().getSimpleName();//获得目标方法的类名
logger.info("前置增强...."); }
//后置增强
@AfterReturning(pointcut="pointcut()")
public void after(JoinPoint joinPoint){
logger.info("后置增强....");
}
//异常抛出增强
@AfterThrowing(pointcut = "pointcut()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Exception e){
String exeMessage = e.getMessage();
logger.info(exeMessage);
}
//后置增强
@After("pointcut()")
public void afterEnd(JoinPoint joinPoint){
logger.info("最终增强....");
}
//环绕增强
@Around("execution(* cn.service..*.*(..))")
public Object round(ProceedingJoinPoint joinPoint){
Object result = null;
try {
logger.info("环绕增强 ------前面。。"); result = joinPoint.proceed();//此方法调用真正的目标方法,从而实现对连接点的完全控制。 logger.info("环绕增强 ------后面。。");
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}
}
2.在核心XML配置文件
(1)首先导入aop命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
(2)在核心配置文件里加入<aop:aspectj-autoproxy />元素,就可以启用对于@AspectJ注解的支持,Spring会自动为匹配的Bean创建代理。
小结:
1、被注解的java类当做Bean实例,Bean实例的名称默认是Bean类的首字母小写,其他部分不变。@Service也可以自定义Bean名称,但是必须是唯一的!
2、尽量使用对应组件注解的类替换@Component注解,在spring未来的版本中,@Controller,@Service,@Repository会携带更多语义。并且便于开发和维护!
Spring的注解问题的更多相关文章
- Spring MVC注解的一些案列
1. spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- spring @condition 注解
spring @condition注解是用来在不同条件下注入不同实现的 demo如下: package com.foreveross.service.weixin.test.condition; im ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- Spring的注解方式实现AOP
Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...
- Spring 之注解事务 @Transactional
众所周知的ACID属性: 原子性(atomicity).一致性(consistency).隔离性(isolation)以及持久性(durability).我们无法控制一致性.原子性以及持久性,但可以 ...
- 数据库事务中的隔离级别和锁+spring Transactional注解
数据库事务中的隔离级别和锁 数据库事务在后端开发中占非常重要的地位,如何确保数据读取的正确性.安全性也是我们需要研究的问题.ACID首先总结一下数据库事务正确执行的四个要素(ACID): 原子性(At ...
- Spring JSR-250注解
Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...
- 【SSM 2】spring常用注解
声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...
- atititt.java定时任务框架选型Spring Quartz 注解总结
atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...
随机推荐
- 《Windows via C/C++》学习笔记 —— 设备I/O之“同步的设备I/O”(系列文章)
前面曾经讲过,设备I/O的方式有两种:同步和异步.本篇介绍一下同步设备I/O.主要涉及到两个函数:ReadFile和WriteFile. 不要被这两个函数的名称迷惑,不仅可以将这两个作用于文件,也可以 ...
- 为什么使用剪切板时都用GlobalAlloc分配内存(历史遗留问题,其实没关系了)
我在使用剪切板时,发现通用的都是使用GlobalAlloc来分配内存,我就想不是说在Win32中GlobalAlloc和LocalAlloc是一样的那为什么不用LocalAlloc呢,原谅我的好奇心吧 ...
- vc++的学习目的
vc++支持多种编程方式,从结构化的编程,面向对象编程,泛型编程,com组件编程. 我想学习vc++的原因是它更接近底层.非常的高效,希望之后用它写出非常简洁高效的代码.
- asp.net mvc实现微信外H5支付方法
一.微信支付方式介绍 微信提供了各种支付方式,试用于各种不同的支付场景,主要有如下几种: 1.刷卡支付 刷卡支付是用户展示微信钱包内的“刷卡条码/二维码”给商户系统扫描后直接完成支付的模式.主要应用线 ...
- 使用Arcgis Pro 发布矢量切片
ArcGIS Pro 中的任何地图或底图都可以创建矢量切片,但是有一些局限性和特殊注意事项.为创建矢量切片制作地图的重点是构建一个有效的地图,以快速绘制生成的切片. 软件环境 操作系统:Windows ...
- 程序代写, CS代写, 代码代写, CS编程代写, java代写, python代写, c++/c代写, R代写, 算法代写, web代写
互联网一线工程师程序代写 微信联系 当天完成 查看大牛简介特色: 学霸代写,按时交付,保证原创,7*24在线服务,可加急.用心代写/辅导/帮助客户CS作业. 客户反馈与评价 服务质量:保证honor ...
- 9个WebGL的演示
1. WebGL Water This incredible demo is as fluid as you could believe. Raise and drop the ball into ...
- Spring Boot:快速入门教程
什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...
- Hyperledger Fabric1.4的多机部署
之前的文章深入解析Hyperledger Fabric启动的全过程主要讲解了Fabric的网络搭建,以及启动的整体流程,但是都是通过单机完成的.而区块链本身就是去中心化的,所以最终还是要完成Fabri ...
- 【设计模式】结构型02装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,装饰器模式相比生成子类更为灵活. 主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由 ...