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配置:
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(); } }
相关文章推荐
- java中关于爬楼梯算法的研究
- spring mvc 从excel导入数据
- java开发学习工具,必须收藏
- 【Leetcode】:238. Product of Array Except Self 问题 in JAVA
- Spring事务管理
- Java内部类的使用小结
- SpringMvc的自动注入与Struts2的区别
- Mybatis整合Spring(不需实现类 namespace方式)
- java常用线程池
- java jvm perf
- SpringMVC处理静态资源
- Ubuntu 14.04 安装配置 JDK8
- JavaWeb 项目——起点新闻社区
- java Spring集合
- log4j2 配置 基于JAVA 以Web为例
- Java多线程总结
- java的Socket连接简单案例
- Java中<T>泛型意思
- FileUtil.java
- Eclipse:An internal error occurred during: "Build Project". GC overhead limit exceeded