Spring AOP(一、xml文件配置实现)

来源:互联网 发布:川荣李奈 知乎 编辑:程序博客网 时间:2024/06/18 14:45

在Xml配置AOP有两种实现方式:一、基于代理实现AOP切面编程;二、通过自动代理实现AOP切面编程

项目结构

其中,StudentsDAOImpl.java为切面类

一、基于代理实现AOP切面编程

StudentsDAOImpl.java

package com.pro.proxy;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;import org.springframework.aop.MethodBeforeAdvice;public class StudentsDAOProxy implements MethodBeforeAdvice,AfterReturningAdvice{@Overridepublic void afterReturning(Object returnValue, Method method,Object[] args, Object target) throws Throwable {// TODO Auto-generated method stubSystem.out.println("执行方法之后");}@Overridepublic void before(Method method, Object[] args, Object target)throws Throwable {// TODO Auto-generated method stubSystem.out.println("执行方法之前");}}
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 基于代理的AOP实现 --><!-- 1.定义实现类bean --><bean id="studentsDaoImpl" class="com.pro.vo.StudentsDAOImpl"></bean><!-- 2.定义代理类 --><bean id="studentsDaoProxy" class="com.pro.proxy.StudentsDAOProxy"></bean><!-- 3.定义切入点 --><bean id="studentsDaoPointCut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"><property name="pattern" value=".*Students"/></bean><!-- 4.定义通知 --><bean id="studentsDaoAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"><property name="advice" ref="studentsDaoProxy"/><property name="pointcut" ref="studentsDaoPointCut"/></bean><!-- 5.定义代理工厂 --><bean id="studentsDaoFactory" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="studentsDaoImpl"></property><property name="interceptorNames" value="studentsDaoAdvisor"></property><property name="proxyInterfaces" value="com.pro.dao.StudentsDAO"></property></bean></beans>
测试类test.java

package com.pro.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pro.dao.StudentsDAO;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");StudentsDAO sDao = (StudentsDAO) ctx.getBean("studentsDaoFactory");  //获取代理工厂BeansDao.saveStudents();}}

二、自动代理的AOP实现

StudentsDAOProxy.java与上面一样

但是 在applicationContext.xml中稍有改动

applcationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!-- 自动代理的AOP实现 --><!-- 1.定义实现类bean --><bean id="studentsDaoImpl" class="com.pro.vo.StudentsDAOImpl"></bean><!-- 2.定义代理类 --><bean id="studentsDaoProxy" class="com.pro.proxy.StudentsDAOProxy"></bean><!-- 3.定义支持正则表达式的通知 --><bean id="studentsDaoAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name="advice" ref="studentsDaoProxy"/><property name="pattern" value=".*Students"></property></bean><!-- 4.声明支持自动代理 --><bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean></beans>
测试类test.java

package com.pro.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pro.dao.StudentsDAO;public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");StudentsDAO sDao = (StudentsDAO) ctx.getBean("studentsDaoImpl");//获取实现累的beansDao.saveStudents();}}


相比较,自动代理实现的AOP会比基于代理实现的AOP会方便很多

源码下载点击打开链接

0 0
原创粉丝点击