-----------------------------基于XML配置方案
目标对象接口
1 public interface IUserService {
public void add();
public void update();
public void del();
public void search();
}
1.创建目标对象
public class UserServiceImpl implements IUserService {
@Override
public void add() {
System.out.println("add....");
}
@Override
public void update() {
System.out.println("update....");
}
@Override
public void del() {
System.out.println("del....");
}
@Override
public void search() {
System.out.println("search....");
}
}
2.创建通知(增强 advice)
//adcice增强类 Aspectj框架定义的通知类型
public class UserServiceHelper {
//前置通知
public void before(JoinPoint jp){
System.out.println("拦截目标类"+jp.getSignature().getDeclaringTypeName());
System.out.println("拦截目标方法名"+jp.getSignature().getName());
System.out.println("前置通知"); }
public void afterreturning(JoinPoint jp,Object val){
System.out.println("目标返回值"+val);
System.out.println("后置通知");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕前通知");
Object value = pjp.proceed();
System.out.println("环绕后通知");
return value;
}
public void afterthrowing(JoinPoint jp,Throwable ex){
//有异常时才输出
System.out.println("异常抛出通知"+ex);
}
public void after(JoinPoint jp){
System.out.println("拦截目标方法名"+jp.getSignature().getName());
System.out.println("最终通知");
}
}
3.1在Spring的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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="./aop4.xml" />
</beans>
3.2在Spring的xmp配置apo1.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- target 目标对象 -->
<bean id="userService" class="cn.aspectjs.UserServiceImpl"/>
<!-- advice 通知-->
<bean id="userServiceAdvice" class="cn.aspectjs.UserServiceHelper"/>
<!-- config是用来声明,内容表示不管有没有接口,都用aspectj框架实现aop,aspect用来配置切面 -->
<aop:config proxy-target-class="true">
<aop:aspect ref="userServiceAdvice">
<aop:pointcut expression="execution(* *.add(..))" id="MypointCut"/>
<!-- method是增强类 -->
<aop:before method=""/>
<aop:before method="before" pointcut-ref="MypointCut"/>
<aop:after-returning method="afterreturning" pointcut-ref="MypointCut" returning="val"/>
<aop:around method="around" pointcut-ref="MypointCut" />
<aop:after-throwing method="afterthrowing" pointcut-ref="MypointCut" throwing="ex"/>
<aop:after method="after" pointcut-ref="MypointCut" />
</aop:aspect>
</aop:config>
</beans>
4.测试类
//Spring整合JUnit测试
@RunWith(SpringJUnit4ClassRunner.class)
//参数定义了要装入的Spring配置文件
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AspectJTest{
@Autowired
private IUserService userService;
@Test
public void test1(){
//对应aop1.xml中的aspectj定义的通知.
userService.add();
}
}
基于annotation方案
1.编写目标
@Component //spring 扫描对象
public interface ICustomerService {
public void save();
public void search();
public void update();
}
@Service
public class CustomerService implements ICustomerService {
@Override
public void save() {
System.out.println("save......");
}
@Override
public void search() {
//System.out.println(10/0);
System.out.println("search......");
}
@Override
public void update() {
System.out.println("update......");
} }
2.编写增强
//增强类(通知)
@Component//声明Spring容器
@Aspect//声明当前的bean的就是一个切面
public class CustomerServiceHelper {
@Pointcut("execution(* *.s*(..))")
private void mypointcut(){}; @Pointcut("execution(* *.update(..))")
private void mypointcut1(){};
//前置通知
@Before("mypointcut()||mypointcut1()")
public void beforeA(JoinPoint jp){
System.out.println("目标类名称"+jp.getSignature().getDeclaringTypeName());
System.out.println("目标方法名称"+jp.getSignature().getName());
System.out.println("前置通知......");
}
@AfterReturning(value="execution(* *.s*(..))",returning="o")
public void AfterReturning(JoinPoint jp,Object o){
System.out.println("后置通知.,目标方法的返回是"+o);
}
@Around("execution(* *.s*(..))")
public Object Around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("执行前操作...");
//是否执行目标方法
Object value = pjp.proceed();
System.out.println("执行后操作...");
//环绕通知必须要有返回值,值为目标方法的返回值
System.out.println("目标方法的返回值"+value);
return value;
}
@AfterThrowing (value="execution(* *.s*(..))",throwing="t")
public void AfterThrowing(JoinPoint jp,Throwable t){
System.out.println("异常抛出通知......"+t);
}
@org.aspectj.lang.annotation.After(value="execution(* *.s*(..))")
public void After(JoinPoint jp){
System.out.println("最后通知......");
}
}
3.配置
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<import resource="./aop2.xml"/>
</beans>
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 在Spring配置文件中配置扫描注解 -->
<context:component-scan base-package="cn.annotation"/>
<!-- 开启aspectj注解自动代理 .false代表的是如果目标是有接口的使用proxy代理,如果没有接口使用cglib动态代理-->
<aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="false"/>
</beans>
aop2
4.测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class AnnotationTest {
@Autowired
private ICustomerService customerService;
@Test
public void test(){
customerService.search();
}
}
5.控制台打印结果
执行前操作...
目标类名称cn.annotation.CustomerService
目标方法名称search
前置通知......
search......
执行后操作...
执行通知,目标方法的返回值null
最后通知......
后置通知.,目标方法的返回是null