您的位置:首页 > 编程语言 > Java开发

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

2017-03-16 12:23 603 查看
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
4000
.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

【事务管理】提交事务
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息