spring(四)AOP相关概念

名词解释

AOP定义:面向切面编程

Joinpoint:连接点,即目标对象中,所有可以增强的方法

Pointcut:切入点,即目标对象中,已经增强的方法

Advice:通知,即需要增强的代码

Target:目标对象,即被代理的对象

Weaving:织入,将通知应用到切入点的过程

Proxy:代理,将通知织入到目标对象之后,形成代理对象

aspect:切面,切入点+通知



AOP实现原理:

1.动态代理:被代理对象必须要实现接口

2.CGLib代理:可以对任何类生成代理,代理的原理是对目标对象进行继承代理 


相关代码:

用户接口:

public interface UserService {

	void add();
	
	void delete();
	
	void update();
	
	void search();
	
}

用户接口实现类:

public class UserServiceImpl implements UserService {

	@Override
	public void add() {
		System.out.println("add");
	}

	@Override
	public void delete() {
		System.out.println("delete");
	}

	@Override
	public void update() {
		System.out.println("update");
	}

	@Override
	public void search() {
		System.out.println("search");
	}

}

通知类:

public class UserAdvice {

	//前置通知:目标方法运行前调用
	public void before(){
		System.out.println("before");
	}
	
	//后置通知:方法出现异常不会调用此通知
	public void afterReturning(){
		System.out.println("afterReturning");
	}
	
	//环绕通知:目标方法执行前后都被调用
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("around before");
		Object proceed = joinPoint.proceed();
		System.out.println("around after");
		return proceed;
	}
	
	public void afterException(){
		System.out.println("afterException");
	}
	
	//后置通知:方法出现异常也调用此通知
	public void after(){
		System.out.println("after");
	}
	
}

配置文件:






	

	

	
		
		
		
			
			
			
			
			
		
	

测试:

@RunWith(SpringJUnit4ClassRunner.class)//创建容器
@ContextConfiguration("classpath:applicationContext-advice.xml")//指定配置文件位置
public class AdviceTest {
	
	@Resource(name="userService")
	private UserService service;
	
	@Test
	public void Test1(){
		service.add();
	}
	
}
输出如下:

before
around before
add
after
around after
afterReturning



注释配置:

配置切面:

@Aspect
//表示该类为一个通知类
public class UserAdviceByAnnotation {

	@Pointcut("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void advice(){}
	
	
	//前置通知:目标方法运行前调用
	@Before("UserAdviceByAnnotation.advice()")
	public void before(){
		System.out.println("before");
	}
	
	//后置通知:方法出现异常不会调用此通知
	@AfterReturning("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void afterReturning(){
		System.out.println("afterReturning");
	}
	
	//环绕通知:目标方法执行前后都被调用
	@Around("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
		System.out.println("around before");
		Object proceed = joinPoint.proceed();
		System.out.println("around after");
		return proceed;
	}
	
	@AfterReturning("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void afterException(){
		System.out.println("afterException");
	}
	
	//后置通知:方法出现异常也调用此通知
	@After("execution(* com.milan.spring.service.impl.*ServiceImpl.*(..))")
	public void after(){
		System.out.println("after");
	}
	
}
配置文件:






	

	

	

	
测试:

@RunWith(SpringJUnit4ClassRunner.class)//创建容器
@ContextConfiguration("classpath:applicationContext-advice-annotation.xml")//指定配置文件位置
public class AdviceByAnnotationTest {
	
	@Resource(name="userService")
	private UserService service;
	
	@Test
	public void Test1(){
		service.add();
	}
	
}
输出如下:

around before
before
add
around after
after
afterException
afterReturning

你可能感兴趣的:(Spring)