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
【事务管理】提交事务
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
【事务管理】提交事务
相关文章推荐
- spring系列(二)——AOP的两种实现方式(xml和注解)
- spring框架—AOP编程的两种实现方式
- AOP编程实现(XML配置方式和注解方式)
- Spring系列之 (六):Spring实现IOC的两种方式(XML和注解)
- Spring AOP AspectJ注解和XML配置两种实现(Maven构建)
- 实例说明Spring实现AOP的2种方式(注解和XML配置)
- Spring AOP 使用注解的方式实现用户日志的两种方法
- 【Spring aop】Spring aop的XML和注解的两种配置实现
- Spring的AOP分为注解和配置两种方式实现
- AOP实现_AOP中的概念_注解方式_XML配置方式声明切面
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
- spring aop相关概念和xml与注解实现方式
- Spring 管理hibernate事物,xml配置,注解 两种实现方式
- 【Spring aop】Spring aop的XML和注解的两种配置实现
- (9) 使用Spring的注解方式实现AOP入门 以及 细节
- Spring IOC 依赖注入的两种方式XML和注解
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- spring aop的使用(注解方式以及基于xml配置方式)
- 使用Spring的注解方式实现AOP
- Spring学习笔记(14)----使用Spring的注解方式实现AOP