1.
---jar包文件
Spring
org.example
1.0-SNAPSHOT
4.0.0
spring03-aop
junit
junit
4.12
org.springframework
spring-context
5.0.5.RELEASE
org.aspectj
aspectjweaver
1.8.4
org.springframework
spring-test
5.0.5.RELEASE
com.liuboss.aop.TargetInterface 接口
package com.liuboss.aop;
public interface TargetInterface {
public void save();
}
com.liuboss.aop.Target 实现接口方法
package com.liuboss.aop;
public class Target implements TargetInterface {
public void save() {
System.out.println("TargetInterface...save running");
}
}
com.liuboss.aop.MyAspect 实现切面的方法
package com.liuboss.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
public void before(){
System.out.println("前值增强");
}
public void after(){
System.out.println("后置增强");
}
//ProceedingJoinPoint 正在执行的连接点-切点
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前-增强");
//添加切点方法
Object proceed = pjp.proceed();
System.out.println("环绕后-增强");
return proceed;
}
public void agterThrowing(){
System.out.println("抛出异常增强。。。。。。");
}
}
---spring配置文件进行配置
---测试文件进行测试
package com.liuboss.demo;
import com.liuboss.aop.TargetInterface;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
@Autowired
private TargetInterface target;
@org.junit.Test
public void test1() {
target.save();
}
}
pom文件
Spring
org.example
1.0-SNAPSHOT
4.0.0
spring03-aop
junit
junit
4.12
org.springframework
spring-context
5.0.5.RELEASE
org.aspectj
aspectjweaver
1.8.4
org.springframework
spring-test
5.0.5.RELEASE
---com.liuboss.anno.TargetInterface 创建接口
package com.liuboss.anno;
public interface TargetInterface {
public void save();
}
com.liuboss.anno.Target 实现接口的方法
package com.liuboss.anno;
import org.springframework.stereotype.Component;
@Component("target")
public class Target implements TargetInterface {
public void save() {
System.out.println("TargetInterface...save running");
}
}
com.liuboss.anno.MyAspect 实现其中的方法
package com.liuboss.anno;
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.springframework.stereotype.Component;
@Component("MyAspect")
//声明为切面 标注当前MyAspect是一个切面类
@Aspect
public class MyAspect {
@Before("execution(* com.liuboss.anno.*.*(..))")
public void before(){
System.out.println("前值增强");
}
@After("execution(* com.liuboss.anno.*.*(..))")
public void after(){
System.out.println("后置增强");
}
//ProceedingJoinPoint 正在执行的连接点-切点
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前-增强");
//添加切点方法
Object proceed = pjp.proceed();
System.out.println("环绕后-增强");
return proceed;
}
public void agterThrowing(){
System.out.println("抛出异常增强。。。。。。");
}
}
spring文件进行配置 applicationContext-anno.xml
package com.liuboss.anno;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("MyAspect")
//声明为切面 标注当前MyAspect是一个切面类
@Aspect
public class MyAspect {
//@Before("execution(* com.liuboss.anno.*.*(..))")
@Around("MyAspect.pointcut()")
public void before(){
System.out.println("前值增强");
}
//@After("execution(* com.liuboss.anno.*.*(..))")
@After("MyAspect.pointcut()")
public void after(){
System.out.println("后置增强");
}
//ProceedingJoinPoint 正在执行的连接点-切点
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前-增强");
//添加切点方法
Object proceed = pjp.proceed();
System.out.println("环绕后-增强");
return proceed;
}
public void agterThrowing(){
System.out.println("抛出异常增强。。。。。。");
}
@Pointcut("execution(* com.liuboss.anno.*.*(..))")
public void pointcut(){
}
}