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
- Spring AOP(一、xml文件配置实现)
- Spring配置xml文件实现AOP
- Spring Xml文件配置实现AOP通知
- Spring AOP XML配置实现
- spring-aop基于xml文件配置
- spring.AOP(XML实现)
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP(3)基于XML配置实现的示例
- Spring AOP xml配置实现(sxt小结)
- 配置xml方式实现Spring的aop
- spring(14)------xml配置AOP实现
- Spring-通过xml配置实现AOP
- Spring(十二)使用Spring的xml文件配置方式实现AOP
- Spring、XML配置AOP
- spring aop xml配置
- (一)Spring AOP:基于XML配置文件
- Spring AOP (三) XML配置方式
- Spring学习:AOP配置1(xml)
- Activity-Lifecyle认识(5)
- JavaScript new关键字
- adroid 源码实例
- Git链接到自己的Github
- PIC
- Spring AOP(一、xml文件配置实现)
- android中获取json数据
- 混合高斯模型算法
- winfrom SQL server 控件Listbox 输出数据表中的所有行和指定列
- Hdu 2152 Fruit 母函数
- 关于TCP打洞技术(P2P)
- 恒天云技术分享系列3 – KVM性能调优
- java单例模式
- Nginx缓存实现