设为首页 加入收藏

TOP

Spring(三)__aop编程(二)
2017-10-13 10:41:50 】 浏览:9461
Tags:Spring __aop 编程
quot;http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" > <!-- 配置被代理的对象 --> <bean id="test1Service" class="com.hsp.aop.Test1Service"> <property name="name" value="顺平" /> </bean> <!-- 配置前置通知 --> <bean id="MyMethodBeforeAdvice" class="com.hsp.aop.MyMethodBeforeAdvice" /> <!-- 配置后置通知 --> <bean id="myAfterReturningAdvice" class="com.hsp.aop.MyAfterReturningAdvice"/> <!-- 配置环绕通知 --> <bean id="myMethodInterceptor" class="com.hsp.aop.MyMethodInterceptor" /> <!-- 配置异常通知 --> <bean id="myThrowsAdvice" class="com.hsp.aop.MyThrowsAdvice"/> <!-- 定义前置通知的切入点 --> <bean id="myMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" > <property name="advice" ref="MyMethodBeforeAdvice" /> <property name="mappedNames"> <list> <value>sayHello</value> </list> </property> </bean> <!-- 代理对象的实现原理.实现接口 proxyFactoryBean implements TestServiceInter,TestServiceInter2{ public void sayHello(); } 思考:多态下接口类型的转换 interface Inter1{}; class A implements Inter1,Inter2{ } Inter1 a=new A(); Inter2 b=(Inter2)a; --> <!-- 配置代理对象,只需配置而不要写 --> <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 代理接口集 这里name值是固定的--> <property name="proxyInterfaces"> <list> <value>com.hsp.aop.TestServiceInter</value> <value>com.hsp.aop.TestServiceInter2</value> </list> </property> <!-- 把通知织入到代理对象 这里name值是固定的 --> <property name="interceptorNames"> <list> <!-- 相当于把MyMethodBeforeAdvice前置通知和代理对象关联,我们也 可以把通知看成拦截器,struts2核心拦截器 --> <!-- 相当于自定义切入点来控制前置通知的使用 --> <value>myMethodBeforeAdviceFilter</value> <!-- 织入后置通知 --> <value>myAfterReturningAdvice</value> <!-- 织入环绕通知 --> <value>myMethodInterceptor</value> <!-- 织入异常通知 --> <value>myThrowsAdvice</value> </list> </property> <!-- 配置被代理对象,可以指定 --> <property name="target" ref="test1Service"/> </bean> </beans>

测试:

public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("com/hsp/aop/beans.xml"); TestServiceInter ts=(TestServiceInter)
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇设计模式学习之01状态模式 下一篇如何一步一步用DDD设计一个电商网..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目