spring xml配置aop

接口

package cn.itheima.springxmlaop;


public interface UserService {
void save(int a, int b);
void update();
void find();
void delete();


}


接口实现类

package cn.itheima.springxmlaop;


public interface UserService {
void save(int a, int b);
void update();
void find();
void delete();


}

通知/增强

package cn.itheima.springxmlaop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
//这就是一个切面
public class MyAdvice {



//前置通知
public void before(JoinPoint point) {
System.out.println(point.getTarget());//cn.itheima.springxmlaop.UserServiceImpl@1fe47411
System.out.println(point.getTarget().getClass());//class cn.itheima.springxmlaop.UserServiceImpl
System.out.println(point.getTarget().getClass().getName());//cn.itheima.springxmlaop.UserServiceImpl
System.out.println(point.getTarget().getClass().getSimpleName());//UserServiceImpl
System.out.println(point.getSignature().getName());//save,or,delete,or,update
//System.out.println(point.getArgs()[0]);//[Ljava.lang.Object;@1165477e数组对象//2
System.out.println(point.getArgs());//[Ljava.lang.Object;@1165477e数组对象//2
System.out.println("前置通知");
}
//后置通知
public void after() {
System.out.println("后置通知");
}
//环绕通知
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕通知之前");
Object proceed = pjp.proceed();

System.out.println("环绕通知之后");
return proceed;
}
//异常拦截通知
public void exception() {
System.out.println("出现异常了");
}
//后置异常通知
public void afterException() {
System.out.println("出现异常了,后置通知");
}


}


测试:

package cn.itheima.springxmlaop;


import javax.annotation.Resource;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itheima/springxmlaop/applicationContext.xml")
public class Demo {


@Resource(name="userServiceImpl")
private UserService userServiceImpl;
@Test
public void fun() {
userServiceImpl.save(2,3);
userServiceImpl.delete();
}

}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<bean name="userServiceImpl" class="cn.itheima.springxmlaop.UserServiceImpl"></bean>
<bean name="myAdvice" class="cn.itheima.springxmlaop.MyAdvice"></bean>
<aop:config>
<!-- public void cn.itheima.service.UserServiceImpl.save()
    void cn.itheima.service.UserServiceImpl.save()
    * cn.itheima.service.UserServiceImpl.save()
    * cn.itheima.service.UserServiceImpl.*()
    * cn.itheima.service.UserServiceImpl.*(..)
    * cn.itheima.service.*ServiceImpl.*(..)
    * cn.itheima.service.*ServiceImpl.*(..)
-->
<aop:pointcut expression="execution(* cn.itheima.springxmlaop.*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<aop:after method="afterException" pointcut-ref="pc"/>
<aop:after-returning method="after" pointcut-ref="pc"/>
<aop:after-throwing method="exception" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:before method="before" pointcut-ref="pc"/>

</aop:aspect>
</aop:config>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值