Spring的AOP配置.docx

上传人:b****6 文档编号:8705871 上传时间:2023-02-01 格式:DOCX 页数:13 大小:27.79KB
下载 相关 举报
Spring的AOP配置.docx_第1页
第1页 / 共13页
Spring的AOP配置.docx_第2页
第2页 / 共13页
Spring的AOP配置.docx_第3页
第3页 / 共13页
Spring的AOP配置.docx_第4页
第4页 / 共13页
Spring的AOP配置.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

Spring的AOP配置.docx

《Spring的AOP配置.docx》由会员分享,可在线阅读,更多相关《Spring的AOP配置.docx(13页珍藏版)》请在冰豆网上搜索。

Spring的AOP配置.docx

Spring的AOP配置

Spring的AOP配置

1.先写一个普通类:

packagecom.spring.aop;

publicclassCommon{

 publicvoidexecute(Stringusername,Stringpassword){

    System.out.println("------------------普通类----------------");

  }

}

2.写一个切面类,用于合法性校验和日志添加:

packagecom.spring.aop;

publicclassCheck{

 publicvoidcheckValidity(){

    System.out.println("------------------验证合法性----------------");

 }

publicvoidaddLog(JoinPointj){

  System.out.println("------------------添加日志----------------");

  Objectobj[]=j.getArgs();

  for(Objecto:

obj){

   System.out.println(o);

  }

  System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名

 }

}

3.配置AOP,使用XML方式:

(注意红色标志的内容)

xmlversion="1.0"encoding="UTF-8"?

>

 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/beanshttp:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd

 http:

//www.springframework.org/schema/aophttp:

//www.springframework.org/schema/aop/spring-aop-2.5.xsd">

 

 

   

 

config>

   

aspectid="myAop"ref="check">

     

pointcutid="target"expression="execution(*com.spring.aop.Common.execute(..))"/>

     

beforemethod="checkValidity"pointcut-ref="target"/>

     

aftermethod="addLog"pointcut-ref="target"/>

   

aspect>

 

config>

注意:

execution(*com.spring.aop.*.*(..))"/

这样写应该就可以了

这是com.aptech.jb.epet.dao.hibimpl包下所有的类的所有方法。

第一个*代表所有的返回值类型

第二个*代表所有的类

第三个*代表类所有方法

最后一个..代表所有的参数。

 

4.最后写一个测试:

packagecom.spring.aop;

importorg.springframework.beans.factory.BeanFactory;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclassClient{

 publicstaticvoidmain(String[]args){

    BeanFactoryfactory=newClassPathXmlApplicationContext("applicationContext-aop.xml");

    Commonc=(Common)factory.getBean("common");

    c.execute("zhengjunhua","zhengjunhua");

  }

}

注意:

需要添加三个包:

spring-aop.jar,aspectjrt.jar,aspectjweaver.jar,否则会报错。

 

输出结果:

------------------验证合法性----------------

------------------普通类----------------

------------------添加日志----------------

zhengjunhua

zhengjunhua

========checkSecurity==execute

SpringAOP配置选项、

Spring实现动态代理配置是有两种配置文件:

1、  xml文件方式;

2、  annotation方式(使用AspectJ类库实现的。

一、      AOP配置annotation方式

(一) 搭建annotation开发环境

首先:

需要在配置文件中加入@AspectJ标签

aspectj-autoproxy/>

自动帮我产生代理

注意:

Spring默认并没有加入aop的xsd文件,因为我们需要手动加入(红色部分)

//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">

annotation-config/>

component-scanbase-package="com.wjt276"/>

aspectj-autoproxy/>

 

另外需要引用aspectJ的jar包:

aspectjweaver.jar

aspectjrt.jar

(二) aspectJ类库

AspectJ是一个专门用来实现动态代理(AOP编程)的类库

AspectJ是面向切面编程的框架

Spring使用就是这个类库实现动态代理的

(三) AOP的annotation实例

要求:

在执行save()方法之前加入日志逻辑

1、  spring的配置文件同上面的

2、  model类、dao层类、service层类都与上面天下一致

3、  切面类(LogInterceptor)

 

importorg.aspectj.lang.annotation.Aspect;

importorg.aspectj.lang.annotation.Before;

importorg.springframework.stereotype.Component;

@Aspect

@Component

publicclassLogInterceptor{

 

@Before("execution(publicvoidcom.wjt276.dao.impl.UserDaoImpl.save(com.wjt276.model.User))")

publicvoidbefore(){

System.out.println("methodstart...");

}

}

结果:

这样在运行publicvoidcom.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.*.*(..))")

publicvoidbusinessService(){}

 

3、 Aspect

切面

4、 Advice

切入点的逻辑

例如上例中的@Before

5、 Target

被代理对象

6、 Weave

织入

 

(五) 织入点语法

1、   无返回值、com.wjt276.dao.impl.UserDaoImpl.save方法参数为User

execution(publicvoidcom.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(publicvoidcom.xyz.service..*.*(..))

execution(public!

voidcom.xyz.service..*.*(..))

注意:

上以是AspectJ的织入点语法,SpringAOP也实现了自己的织入点语法,同样可以使用

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)

(六) Advice

1、    @Before

执行方法之前

@Aspect

publicclassBeforeExample{

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

publicvoiddoAccessCheck(){//...}}

 

@Aspect

publicclassBeforeExample{

@Before("execution(*com.xyz.myapp.dao.*.*(..))")

publicvoiddoAccessCheck(){//...}}

2、    @ AfterReturning

方法正常执行完之后

@Aspect

publicclassAfterReturningExample{

@AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

publicvoiddoAccessCheck(){//...}}

 

@Aspect

publicclassAfterReturningExample{

@AfterReturning(

pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",

returning="retVal")

publicvoiddoAccessCheck(ObjectretVal){//...}}

3、    @ AfterThrowing

方法抛出异常之后

@Aspect

publicclassAfterThrowingExample{

@AfterThrowing("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

publicvoiddoRecoveryActions(){//...}}

 

@Aspect

publicclassAfterThrowingExample{

@AfterThrowing(

pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",

throwing="ex")

publicvoiddoRecoveryActions(DataAccessExceptionex){//...}}

4、     @After(finally)

方法抛出异常被catch之后,需要进行的部分(相当于finally功能)

@Aspect

publicclassAfterFinallyExample{

@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

publicvoiddoReleaseLock(){//...}}

5、    @ Around

在方法之前和之后都要加上

但是需要一个参数ProceedingJoinPoint,并者需要ObjectretVal=pjp.proceed();

和返回returnretVal;

@Aspect

publicclassAroundExample{

@Around("com.xyz.myapp.SystemArchitecture.businessService()")

publicObjectdoBasicProfiling(ProceedingJoinPointpjp)throwsThrowable{

//startstopwatch

ObjectretVal=pjp.proceed();

//stopstopwatch

returnretVal;}}

 

(七) Pointcut

当多个Advice个有相同的织入点。

那么我们可以定义一个织入点集合,在需要使用的地方,调用就可以了。

例如:

@Aspect

@Component

publicclassLogInterceptor{

 

@Pointcut("execution(public*com.wjt276.dao..*.*(..))")

publicvoidmyMethod(){};

@Before(value="myMethod()")

publicvoidbefore(){

System.out.println("methodstart...");

}

 

@AfterReturning("myMethod()")

publicvoidafterReturning(){

System.out.println("methodafterreturning...");

}}

注意:

那个空方法,只是为了给Pointcut起个名字,以方便别处使用

 

(八)   annotatin方式的AOP实例

importorg.aspectj.lang.ProceedingJoinPoint;

importorg.aspectj.lang.annotation.AfterReturning;

importorg.aspectj.lang.annotation.Around;

importorg.aspectj.lang.annotation.Aspect;

importorg.aspectj.lang.annotation.Before;

importorg.aspectj.lang.annotation.Pointcut;

importorg.springframework.stereotype.Component;

 

@Aspect

@Component

publicclassLogInterceptor{

 

@Pointcut("execution(public*com.wjt276.dao..*.*(..))")

publicvoidmyMethod(){};

@Before(value="myMethod()")

publicvoidbefore(){

System.out.println("methodstart...");

}

@AfterReturning("myMethod()")

publicvoidafterReturning(){

System.out.println("methodafterreturning...");

}

@Around(value="myMethod()")

publicvoidaround(ProceedingJoinPointpjp)throwsThrowable{

//因为@around需要传入一个参数ProceedingJoinPoint进行前后加逻辑

System.out.println("methodaroundstart...");

//在需要前后逻辑的中间加入下列语句。

表示前后逻辑,可能会抛出异常Throwable。

pjp.proceed();

System.out.println("methodaroundend...");

}

}

 

二、      AOP配置xml方式

xml方式是我们以后使用的比较多的,因为当切面类我们没有源代码时、当我们使用第三方的切面类时,我就不能使用annotation的方式,而且如果使用annotation方式一但程序编译后就不可以修改了。

如果使用xml方式就不一样了,我们只需要修改xml文件就可以了。

xml方式与annotation的作用是一样。

现在就是实例:

xmlversion="1.0"encoding="UTF-8"?

>

//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">

annotation-config/>

component-s

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1