您的位置:首页 > 编程语言 > Java开发

spring aop纯注解

2016-04-19 12:32 651 查看
接口不在贴了,实现类:package com.leige.aspect3;

import org.springframework.stereotype.Service;

@Service
public class PersonServiceImpl implements PersonService {

public void addPerson() {
System.out.println("person --------addperson");

}

}


定义aspect切面类

package com.leige.aspect3;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component//将aspect交给spring管理
@Aspect//定义切面类
public class MyAspect {
//前置增强,匹配切入点
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint){
System.out.println("前置通知, 方法名称:" + joinPoint.getSignature().getName());
}

public void myAfterReturning(JoinPoint joinPoint,Object xxx){
System.out.println("后置通知, 返回值:" + xxx);
}

public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{

System.out.println("前");
//必须执行目标方法,joinPoint可以执行目标方法
Object obj = joinPoint.proceed();

System.out.println("后");
return obj;
}

public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
System.out.println("抛出异常通知, " + e.getMessage());
}
@After("myPointCut()")
//最终通知
public void myAfter(){
System.out.println("最终");
}
//定义切入点表达式
@Pointcut("execution(* com.leige.aspect3..*.*(..))")
public void myPointCut(){

}
}
xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">

<!--      注解开发
首先要配置注解扫描 -->
<context:component-scan base-package="com.leige.aspect3"></context:component-scan>
<!-- 启用aop注解 -->
<aop:aspectj-autoproxy/>
</beans>
测试类:
package com.leige.aspect3;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:com/leige/aspect3/beans.xml")
public class TestApp {
@Autowired
PersonService service;
@Test
public void fun(){
/*ApplicationContext context=new ClassPathXmlApplicationContext("com/leige/aspect2/beans.xml");
PersonService service= (PersonService) context.getBean("personServiceId");*/
service.addPerson();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: