Spring的AOP配置
Spring的AOP配置
1.先写一个普通类:
package com.spring.aop;
public class Common {
public void execute(String username,String password){
System.out.println("------------------普通类----------------");
}
}
2.写一个切面类,用于合法性校验和日志添加:
package com.spring.aop;
public class Check {
public void checkValidity(){
System.out.println("------------------验证合法性----------------");
}
public void addLog(JoinPoint j){
System.out.println("------------------添加日志----------------");
Object obj[] = j.getArgs();
for(Object o :obj){
System.out.println(o);
}
System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名
}
}
3.配置AOP,使用XML方式:(注意红色标志的内容)
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="common" class="com.spring.aop.Common"/>
<bean id="check" class="com.spring.aop.Check"/>
<aop:config>
<aop:aspect id="myAop" ref="check">
<aop:pointcut id="target" expression="execution(* com.spring.aop.Common.execute(..))"/>
<aop:before method="checkValidity" pointcut-ref="target"/>
<aop:after method="addLog" pointcut-ref="target"/>
</aop:aspect>
</aop:config>
</beans>
注意:
execution(* com.spring.aop.*.*(..))"/
这样写应该就可以了
这是com.aptech.jb.epet.dao.hibimpl
包下所有的类的所有方法。。
第一个*代表所有的返回值类型
第二个*代表所有的类
第三个*代表类所有方法
最后一个..代表所有的参数。
4.最后写一个测试:
package com.spring.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext-aop.xml");
Common c=(Common) factory.getBean("common");
c.execute("zhengjunhua","zhengjunhua");
}
}
注意:
需要添加三个包:spring-aop.jar , aspectjrt.jar
,aspectjweaver.jar,否则会报错。
输出结果:
------------------验证合法性----------------
------------------普通类----------------
------------------添加日志----------------
zhengjunhua
zhengjunhua
========checkSecurity==execute
Spring实现动态代理配置是有两种配置文件:
1、
xml文件方式;
2、
annotation方式(使用AspectJ类库实现的。)
一、
AOP配置annotation方式(一)
搭建annotation开发环境
首先:需要在配置文件中加入@AspectJ标签
<aop:aspectj-autoproxy/>
自动帮我产生代理
注意:Spring默认并没有加入aop的xsd文件,因为我们需要手动加入(红色部分)
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config/>
<context:component-scan
base-package="com.wjt276"/>
<aop:aspectj-autoproxy/>
</beans>
另外需要引用aspectJ的jar包:
aspectjweaver.jar
aspectjrt.jar
(二)
aspectJ类库
AspectJ是一个专门用来实现动态代理(AOP编程)的类库
AspectJ是面向切面编程的框架
Spring使用就是这个类库实现动态代理的
(三)
AOP的annotation实例
要求:在执行save()方法之前加入日志逻辑
1、
spring的配置文件同上面的
2、
model类、dao层类、service层类都与上面天下一致
3、
切面类(LogInterceptor)
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
@Before("execution(public void com.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User))")
public
void before(){
System.out.println("method start...");
}
}
结果:这样在运行public void com.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User)方法之前就会先执行这个逻辑了。
注意:
1、@Aspect:意思是这个类为切面类
2、@Componet:因为作为切面类需要Spring管理起来,所以在初始化时就需要将这个类初始化加入Spring的管理;
3、@Befoe:切入点的逻辑(Advice)
4、execution…:切入点语法
(四)
三个连接点(切入点) |
AspectJ的专业术语
1、
JoinPoint
切入面 |
连接点(切入点)
程序执行过程 |
2、
PointCut
切入点人集合
当需要定义一个切入点时,则需要使用这个
@Pointcut("execution(* com.xyz.someapp.service.*.*(..))")
public void businessService() {}
3、
Aspect
切面
4、
Advice
切入点的逻辑
例如上例中的@Before
5、
Target
被代理对象
6、
Weave
织入
(五)
织入点语法
1、 无返回值、com.wjt276.dao.impl.UserDaoImpl.save方法
参数为User
execution(public void com.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User))
2、 任何包、任何类、任何返回值、任何方法的任何参数
execution(public * *(..))
3、 任何包、任何类、任何返回值、任何set开头方法的任何参数
execution(* set*(..))
4、 任何返回值、com.xyz.service.AccountService类中的任何方法、任何参数
execution(* com.xyz.service.AccountService.*(..))
5、 任何返回值、com.xyz.service包中任何类中的任何方法、任何参数
execution(* com.xyz.service.*.*(..))
6、 任何返回值、com.xyz.service包中任何层次子包(..)、任何类、任何方法、任何参数
execution(* com.xyz.service..*.*(..))
7、 void
和 !void(非void)
execution(public void com.xyz.service..*.*(..))
execution(public !void com.xyz.service..*.*(..))
注意:上以是AspectJ的织入点语法,Spring AOP也实现了自己的织入点语法,同样可以使用
within(com.xyz.service.*)
within(com.xyz.service..*)
this(com.xyz.service.AccountService)
target(com.xyz.service.AccountService)
args(java.io.Serializable)
@target(org.springframework.transaction.annotation.Transactional)
@within(org.springframework.transaction.annotation.Transactional)
@annotation(org.springframework.transaction.annotation.Transactional)
@args(com.xyz.security.Classified)
bean(tradeService)
bean(*Service)
(六)
Advice1、
@Before
执行方法之前
@Aspect
public class BeforeExample {
@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doAccessCheck() {
// ... }}
@Aspect
public class BeforeExample {
@Before("execution(* com.xyz.myapp.dao.*.*(..))")
public void doAccessCheck() {
// ... }}
2、
@ AfterReturning
方法正常执行完之后
@Aspect
public class AfterReturningExample {
@AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doAccessCheck() {
// ... }}
@Aspect
public class AfterReturningExample {
@AfterReturning(
pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
returning="retVal")
public void doAccessCheck(Object retVal) {
// ... }}
3、
@ AfterThrowing
方法抛出异常之后
@Aspect
public class AfterThrowingExample {
@AfterThrowing("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doRecoveryActions() {
// ... }}
@Aspect
public class AfterThrowingExample {
@AfterThrowing(
pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
throwing="ex")
public void doRecoveryActions(DataAccessException ex) {
// ... }}
4、
@After (finally)
方法抛出异常被catch之后,需要进行的部分(相当于finally功能)
@Aspect
public class AfterFinallyExample {
@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doReleaseLock() {
// ... }}
5、
@ Around
在方法之前和之后都要加上
但是需要一个参数ProceedingJoinPoint,并者需要Object retVal = pjp.proceed();
和返回return retVal;
@Aspect
public class AroundExample {
@Around("com.xyz.myapp.SystemArchitecture.businessService()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
Object retVal = pjp.proceed();
// stop stopwatch
return retVal; }}
(七)
Pointcut
当多个Advice个有相同的织入点。那么我们可以定义一个织入点集合,在需要使用的地方,调用就可以了。
例如:
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.wjt276.dao..*.*(..))")
public
void myMethod(){};
@Before(value="myMethod()")
public
void before(){
System.out.println("method start...");
}
@AfterReturning("myMethod()")
public
void afterReturning(){
System.out.println("method after returning...");
}}
注意:那个空方法,只是为了给Pointcut起个名字,以方便别处使用
(八)
annotatin方式的AOP实例
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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;
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.wjt276.dao..*.*(..))")
public
void myMethod(){};
@Before(value="myMethod()")
public
void before(){
System.out.println("method start...");
}
@AfterReturning("myMethod()")
public
void afterReturning(){
System.out.println("method after returning...");
}
@Around(value="myMethod()")
public
void around(ProceedingJoinPoint pjp)
throws Throwable{
//因为@around需要传入一个参数ProceedingJoinPoint进行前后加逻辑
System.out.println("method around start...");
//在需要前后逻辑的中间加入下列语句。表示前后逻辑,可能会抛出异常Throwable。
pjp.proceed();
System.out.println("method around end...");
}
}
二、
AOP配置xml方式
xml方式是我们以后使用的比较多的,因为当切面类我们没有源代码时、当我们使用第三方的切面类时,我就不能使用annotation的方式,而且如果使用annotation方式一但程序编译后就不可以修改了。如果使用xml方式就不一样了,我们只需要修改xml文件就可以了。
xml方式与annotation的作用是一样。现在就是实例:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config/>
<context:component-scan
base-package="com.wjt276"/>
<bean
id="logInterceptor"
class="com.wjt276.aop.LogInterceptor"></bean>
<aop:config>
<!-- <aop:pointcut >在此处定义的pointcut是全局的pointcut可以供所有的aspect使用
id:表示这个pointcut的名称,以方便使用-->
<aop:pointcut
id="myMethod"
expression="execution(public * com.wjt276.service..*.*(..))"
/>
<!--
<aop:aspect>表示定义一个切面类(这需要Spring初始化加入其管理)
id:切面类的名称,
ref:引用哪个bean(需要使用<bean>标签初始化)-->
<aop:aspect
id="logAspect"
ref="logInterceptor">
<!--
在此处定义的pointcut是全局的pointcut只供当前的aspect使用
id:表示这个pointcut的名称,以方便使用
-->
<aop:pointcut
id="myMethod2"
expression="execution(public * com.wjt276.service..*.*(..))"
/>
<!--
定义advice时的参数
method:切面逻辑的方法名称(切面类中的方法名)
pointcut-ref:表示引用哪个pointcut(要求已经在上面定义好了)
pointcut:定义一个pointcut
-->
<aop:before
method="before"
pointcut-ref="myMethod"/>
<aop:after-returning
method="afterReturning"
pointcut="execution(public * com.wjt276.service..*.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
三、
AOP实现动态代理注意
因为Spring要实现AOP(面向切面编程),需要加入切面逻辑的类就会生成动态代理。在动态代理类中加入切面类从而实现面向切面编程,但生成动态代理存在以下注意事项:
1、
被动态代理的类如果实现了某一个接口,那么Spring就会利用JDK类库生成动态代理。
2、
如果被动态代理的类没有实现某一个接口,那么Spring就会利用CGLIB类库直接修改二进制码来生成动态代理(因为利用JDK生成动态代理的类必须实现一个接口),需要在项目中引用CGLIB类库
Spring的AOP配置的更多相关文章
- spring.net AOP配置基础
在第一篇中,我们用配置代理工厂的方式实现了面向切面记日志的功能.非常便捷的实现了AOP,但当我们需要对多个切入点配置通知的时候就需要声明多个代理工厂,这样导致配置文件内容过多,配置过程也很繁琐.spr ...
- 学习 Spring (十三) AOP 配置
Spring入门篇 学习笔记 Spring 所有的切面和通知器都必须放在一个 内(可以配置包含多个 元素),每一个 可以包含 pointcut, advisor 和 aspect 元素(它们必须按照这 ...
- Spring之AOP配置
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- 使用Spring实现AOP(XML+注解)
一.Spring对AOP的支持 AOP并不是Spring框架特有的,Spring只是支持AOP编程的框架之一,每一个框架对AOP的支持各有特点,有些AOP能够对方法的参数进行拦截,有些AOP对方法进行 ...
- Spring的aop思想
1.AOP思想 (1)在解决中文乱码的应用: 一般情况下如果在Servlet中要解决中文乱码问题需要在每一个Servlet中书写解决乱码的代码,但是,在运用了过滤器之后,就不再需要每一个Servlet ...
- Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理
一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...
- spring之aop概念和配置
面向切面的一些概念: 简单说: 连接点就一些方法,在这些方法基础上需要额外的一些业务需求处理. 切入点就是方法所代表的功能点组合起来的功能需求. 通知就是那些额外的操作. 织入就是使用代理实现整个切入 ...
- spring aop配置及用例说明(1)
欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...
随机推荐
- 【2-SAT】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
先反复地扫(不超过n次),把所有可以确定唯一取法的给确定下来. 然后对于剩下的不能确定的,跑2-SAT.输出可行解时,对于a和¬a,如果a所在的强连通分量序号在¬a之前,则取a,否则不取a.如果a和¬ ...
- 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...
- 【二分答案】【最大流】bzoj3130 [Sdoi2013]费用流
二分最大的边的cap,记作Lim. 把所有的边的cap设为min(Lim,cap[i]). Bob一定会把单位费用加到最大边上. #include<cstdio> #include< ...
- css sprite demo
一张图片,用CSS分割成多个小图标. css样式: .icon{ background:url(../images/tabicons.png) no-repeat;width:18px; line-h ...
- iOS 取消警告
第一步找到要取消的警告类型 在相应的警告上右击->Reveal in Log 被选中的-Wdeprecated-declarations就是我们所要的警告类型了. -W是前缀,这个前缀表示的是 ...
- VHDL硬件描述语言实现数字钟
--VHDL上机的一个作业,程序太长实验报告册上写不下了.于是就在博客上留一份吧.LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOG ...
- 重写规则为什么要options +followsymlinks
重写规则为什么要options +followsymlinks Web服务器的Apache安装编译成功mod_rewrite编译成模块,但当我在网站根目录下放了一个.htaccess配置文件,却得到了 ...
- Java二进制指令代码解析
http://www.blogjava.net/DLevin/archive/2011/09/13/358497.html http://blog.csdn.net/sum_rain/article/ ...
- vb6转16进制
Public Function xEncode(ByVal strEncode As String) As String If strEncode <> "" Then ...
- [转] 利用Matlab提取图片中曲线数据
原文地址 网易博客 前一段时间看到一篇文章"利用Matlab提取图图片中的数据",觉得思路挺好,遂下载下来研究了一番,发现作者所编写的程序没有考虑原始图片非水 平放置的情况,而实际 ...