Spring2 实现AOP编程的两种实现方法

Spring 实现AOP编程的两种实现方法 --- 1、基于注解方式进行AOP开发。2、基于XML配置方式进行AOP开发

1、基于注解方式开发AOP --- 使用注解需在applicationContext.xml中引入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: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">

<aop:aspectj-autoproxy /> <!--启用@Aspect注解的支持-->

<bean id="porsonService" class="com.service.impl.PorsonServiceImpl" />

</beans>

定义切面,将基将于Spring进行管理,在xml中加入<bean id="interceptor" class="com.interceptor.MyInterceptor" />

@Aspect //此注解是将类声明为切面
public class MyInterceptor {
@Pointcut("execution(* com.service.impl.PorsonServiceImpl.*(..))")//execution代表执行业务方法时进行拦截,* 代表返回值的类型 *表示任何返回类型,对此包下的类进行拦截,(..)代表方法的参数随意
private void anyMethod(){} //声明一个切入点

@Before("anyMethod() && args(name)")//带输入参数
private void doAccessCheck(String name)
{
System.out.println("前置通知"+name);
}
@AfterReturning(pointcut="anyMethod()",returning="revalue")//带返回结果
public void doReturnCheck(String revalue)
{
System.out.println("后置通知"+revalue);
}
@After("anyMethod()")//定义最终通知
public void After()
{
System.out.println("最终通知");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doAfterThrowing(Exception e)
{
System.out.println("异常通知");
System.out.println(e);
}
@Around("anyMethod()")//环绕通知
public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable
{
System.out.println("进入方法");
Object result=pjp.proceed();
System.out.println("退出方法");
return result;
}
}

客户端测试代码

package com.junit;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.service.PorsonService;
public class SpringAOPTest {

@Test public void interceptorTest()
{
ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext.xml");
PorsonService porsonService=(PorsonService)con.getBean("porsonService");
porsonService.save("Smiles");
porsonService.get(141);
}
}

2、基于XML配置方式开发AOP --- applicationContext.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: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">
<bean id="porsonService" class="com.service.impl.PorsonServiceImpl" />
<!-- 基于XML的AOP编程 -->
<bean id="xmlinterceptor" class="com.interceptor.XMLInterceptor" />
<aop:config>
<aop:aspect id="asp" ref="xmlinterceptor">
<!-- execution(* com.service.impl.PorsonServiceImpl.*(java.lang.String,..)) -->
<!-- 方法返回类型 ALL 参数类型-->
<!-- <aop:pointcut id="mycat"
expression="execution(* com.service.impl.PorsonServiceImpl.*(java.lang.String,..))" />
-->
<aop:pointcut id="mycat"
expression="execution(!void com.service.impl.PorsonServiceImpl.*(..))" />
<aop:before pointcut-ref="mycat" method="doAccessCheck"/>
<aop:after-returning pointcut-ref="mycat" method="doReturnCheck"/>
<aop:after-throwing pointcut-ref="mycat" method="doAfterThrowing"/>
<aop:after pointcut-ref="mycat" method="After"/>
<aop:around pointcut-ref="mycat" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
</beans>

用于切面的普通类,将基交于spring管理

package com.interceptor;

import org.aspectj.lang.ProceedingJoinPoint;
public class XMLInterceptor {

private void doAccessCheck() {
System.out.println("XML前置通知");
}

public void doReturnCheck() {
System.out.println("XML后置通知");
}

public void After() {
System.out.println("XML最终通知");
}

public void doAfterThrowing() {
System.out.println("XML异常通知");
}

public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("XML进入方法");
Object result = pjp.proceed();
System.out.println("XML退出方法");
return result;
}
}

客户端测试代码与基于注解的一样
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值