Spring中的aop(生成代理对象)

1、Spring中的aop的介绍

Spring能够为容器中管理的对象创建代理对象。

以前我们创建动态代理对象要调用该方法:

proxy.newProxyInstance(xx,xx,xx)。

Spring中是使用动态代理和cglib代理混合使用,优先使用动态代理,如果不能使用动态代理,则使用cglib代理。

2、cglib代理和动态代理

动态代理:被代理对象必须要实现接口,才能产生代理对象.如果没有接口将不能使用动态代理技术。被代理对象和代理对象只是实现了同一接口。

cglib代理:第三方代理技术(Spring中整合了该jar,所以不用导包),cglib代理.可以对任何类生成代理.代理的原理是对目标对象进行继承代理. 如果目标对象被final修饰.那么该类无法被cglib代理。被代理和代理对象是继承和被继承的关系。

public class UserServiceProxyFactory2 implements MethodInterceptor {
	

	public UserService getUserServiceProxy(){
		Enhancer en = new Enhancer();//帮我们生成代理对象
		en.setSuperclass(UserServiceImpl.class);//设置对谁进行代理
		en.setCallback(this);//代理要做什么
		UserService us = (UserService) en.create();//创建代理对象
		return us;
	}

	@Override
	public Object intercept(Object prxoyobj, Method method, Object[] arg, MethodProxy methodProxy) throws Throwable {
		//打开事务
		System.out.println("打开事务!");
		//调用原有方法
		Object returnValue = methodProxy.invokeSuper(prxoyobj, arg);
		//提交事务
		System.out.println("提交事务!");
		
		return returnValue;
	}


}

3、aop名词

JoinPoint(连接点):目标对象中,所有可以增强的方法。

Pintcut(切入点):目标对象,已经增强的方法。

Advice(通知/增强):增强的代。

Target(目标对象):被代理对象。

Weaving(织入):将通知应用到切入点的过程。

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

aspect(切面):切入点+通知

4、Spring中使用aop步骤

使用Spring创建代理对象

(1)导包

      4+2+2+2

      Spring中的aop包

Spring需要的第三方包

(2)准备目标对象

public class Target{

	public void add() {
		System.out.println("add");
	}
	
	public void remove() {
		System.out.println("remove");
	}
	
	public void update() {
		System.out.println("update");
	}
	
	public void query() {
		System.out.println("query");
	}
	
}

该对象可以实现接口,也可以不用实现,实现了接口则Spring会优先使用动态代理。如果没有实现接口,Spring会使用cglib代理。

注意,如果使用接口,通过getBean获得对象时,记得强制转换对象时,使用接口,而不是目标类。否则会出现ClassCaseException

(3)准备通知

public class AdviceClass {

	public void before() {
		System.out.println("这是前置通知");
	}
	
	public void afterReturning() {
		System.out.println("这是后置通知(出现异常将不会调用)");
	}
	
	public Object around(ProceedingJoinPoint pj) throws Throwable {
		System.out.println("这是环绕通知之前的部分");
		Object proceed = pj.proceed();//调用目标方法
		System.out.println("这是环绕通知之后的部分");
		return proceed;
	}
	
	public void afterException() {
		System.out.println("出现异常后调用");
	}
	
	public void after() {
		System.out.println("这是后置通知(无论会不会出现异常都会调用)");
	}
	
}

(4)配置:将通知织入到目标中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xmlns="http://www.springframework.org/schema/beans"
  	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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

	<bean name="target" class="club.ityuchao.test.Target"></bean>
	<bean name="advice" class="club.ityuchao.test.AdviceClass"></bean>
	
	<aop:config>
		<!-- 配置切入点 
			public void cn.itcast.service.UserServiceImpl.save() 
			void cn.itcast.service.UserServiceImpl.save()
			* cn.itcast.service.UserServiceImpl.save()
			* cn.itcast.service.UserServiceImpl.*()
			
			* cn.itcast.service.*ServiceImpl.*(..)
			* cn.itcast.service..*ServiceImpl.*(..)
		-->
		<aop:pointcut expression="execution(public void club.ityuchao.test.Target.add())" id="pc"/>
		<aop:aspect ref="advice">
			<aop:before method="before" pointcut-ref="pc"/>
			<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
			<aop:around method="around" pointcut-ref="pc"/>
			<aop:after method="after" pointcut-ref="pc"/>
			<aop:after-throwing method="afterException" pointcut-ref="pc"/>
		</aop:aspect>
	</aop:config>

</beans>

5、测试

public class AopTest {

	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("club/ityuchao/test/applicationContext.xml");
		Target target = (Target) ac.getBean("target");
		target.add();
	}
	
}

测试结果:

出现异常:

出现异常后,环绕通知的后执行将不会执行,afterRetruning也不会执行

6、使用注解配置aop(了解)

(1)applicationContext.xml中配置

<!-- 表示使用注解完成织入 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

(2)advice中使用注解

 

//表示该类是一个通知类
@Aspect
public class AdviceClass {
	
	//使用该方法可以不用下面每一个方法都写expression
	@Pointcut("execution(* club.ityuchao.test.Target.*(..))")
	public void pc() {}

	@Before("execution(* club.ityuchao.test.Target.*(..))")
	public void before() {
		System.out.println("这是前置通知");
	}
	
	@AfterReturning("AdviceClass.pc()")
	public void afterReturning() {
		System.out.println("这是后置通知(出现异常将不会调用)");
	}
	
	@Around("AdviceClass.pc()")
	public Object around(ProceedingJoinPoint pj) throws Throwable {
		System.out.println("这是环绕通知之前的部分");
		Object proceed = pj.proceed();//调用目标方法
		System.out.println("这是环绕通知之后的部分");
		return proceed;
	}
	
	@AfterThrowing("AdviceClass.pc()")
	public void afterException() {
		System.out.println("出现异常后调用");
	}
	
	@After("AdviceClass.pc()")
	public void after() {
		System.out.println("这是后置通知(无论会不会出现异常都会调用)");
	}
	
}

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值