Spring框架实现AOP的xml和注解两种方式

Spring框架通过JavaSE动态代理和cglib代理实现AOP,如果指明的目标类实现了接口,那么Spring就使用JavaSE动态代理,也可以强制使用cglib代理,如果指明的目标类没有实现接口,那么Spring就使用cglib代理。

Spring中增强只能针对方法级别,在方法执行前,执行后,是否出现异常等情况进行增强。

1.使用xml实现AOP方式:

如下为增强类,被代理类,xml文件,测试类:

Manager.java:增强类

package service3;
/**
 * 增强类
 * @author ${HLoach}
 * 2017年3月15日
 */
public class Manager {

	public void beginTran(){
		System.out.println("【事务管理】开始事务");
	}
	
	public void commit(){
		System.out.println("【事务管理】提交事务");
	}
	
	public void rollback(){
		System.out.println("【事务管理】事务回滚");
	}
}
ServiceClass.java:被代理类

package service3;

public class ServiceClass{

	public void add(String name, int age) {
		System.out.println("执行添加操作:"+name+" "+age);
		
	}

	public void reRs(int id) {
		
		System.out.println("执行返回结果:"+id);
	}

}
applicationContext.xml:配置文件

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

<!-- 业务类 -->
<bean id="service" class="service3.ServiceClass"/>
<!-- 切面类 -->
<bean id="manager" class="service3.Manager"/>
<!-- 定义切入点 -->
<aop:config>
<aop:aspect id="managerAspect" ref="manager">
<aop:before method="beginTran" pointcut="execution(* service3.ServiceClass.*(..))"/>
<aop:after-returning method="commit" pointcut="execution(* service3.ServiceClass.*(..))"/>
<aop:after-throwing method="rollback" pointcut="execution(* service3.ServiceClass.*(..))"/>
</aop:aspect>
</aop:config>
</beans>
注:上面pointcut为切入点:切入到指定包、指定类、指定方法、指定参数列表的方法执行之前。

execution用法:execution(方法修饰符 方法返回值 方法所属的类.指定方法名.(参数列表))

*:表示任意

..:在参数中表示0个或者多个任意类型的参数
Test3.java:测试类

//通过读取xml文件创建ClassPathXmlApplicationContext对象
ApplicationContext context = new ClassPathXmlApplicationContext("service3/applicationContext.xml");
//通过context对象获取对应名称的bean动态代理对象
ServiceClass serviceClass = (ServiceClass) context.getBean("service");
//通过对象调用函数
serviceClass.add("zhangsan", 18);
serviceClass.reRs(24);
执行结果:

【事务管理】开始事务
执行添加操作:zhangsan 18
【事务管理】提交事务
【事务管理】开始事务
执行返回结果:24
【事务管理】提交事务

2.使用注解方式实现AOP:

如下为增强类,被代理类,xml文件,测试类:

Manager.java:增强类

package service4;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 增强类
 * @author ${HLoach}
 * 2017年3月15日
 */
@Aspect
public class Manager {

	public static final String POINT = "execution(* service4.ServiceClass.*(..))";

	
	@Before(POINT)
	public void beginTran(){
		System.out.println("【事务管理】开始事务");
	}
	
	@After(POINT)
	public void commit(){
		System.out.println("【事务管理】提交事务");
	}
	@AfterThrowing(POINT)
	public void rollback(){
		System.out.println("【事务管理】事务回滚");
	}
}
ServiceClass.java:被代理类

package service4;

public class ServiceClass{
	
	public void add(String name, int age) {
		System.out.println("执行添加操作:"+name+" "+age);
		
	}

	
	public void reRs(int id) {
		System.out.println("执行返回结果:"+id);
//		int i = id/0;
		
	}

}
applicationContext.xml:配置文件

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> 
<!-- 为有@Aspect的增强类自动代理 -->
<aop:aspectj-autoproxy/>
<!-- 业务类 -->
<bean id="service" class="service4.ServiceClass"/>
<!-- 切面类 -->
<bean id="manager" class="service4.Manager"/>

</beans>
Test4.java:测试类

package service4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test4 {

	public static void main(String[] args) {
		//通过读取xml文件创建ClassPathXmlApplicationContext对象
		ApplicationContext context = new ClassPathXmlApplicationContext("service4/applicationContext.xml");
		//通过context对象获取对应名称的bean动态代理对象
		ServiceClass serviceClass = (ServiceClass) context.getBean("service");
		//通过对象调用函数
		serviceClass.add("zhangsan", 18);
		serviceClass.reRs(24);
	}

}
执行结果:
【事务管理】开始事务
执行添加操作:zhangsan 18
【事务管理】提交事务
【事务管理】开始事务
执行返回结果:24
【事务管理】提交事务






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值