第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已。
3.7.1一般增强的使用
a、目标类
public class Target { public void say(){ System.out.println("say..."); } public String getName(int id,String name){ System.out.println("getName..."); return "MR"+name+id; } public void around(){ System.out.println("around..."); } public void targetThrow(){ System.out.println("targetThrow"); throw new RuntimeException("我是一个运行期异常..."); } }
b、POJO(增强所在的类)
public class Pojo { public void before() { System.out.println("前置增强"); } public void afterReturning(String retName, int id, String name) { System.out.println("后置增强,返回值为:"+retName+" 入参为:"+id+"-"+name); } public Object around(ProceedingJoinPoint point) throws Throwable{ System.out.println("方法执行之前"); Object object = point.proceed(); System.out.println("方法执行之后"); return object; } public void throwEx(Exception ex){ System.out.println("抛出异常增强,异常信息:"+ex.getMessage()); } public void finalEx(){ System.out.println("Final增强"); } }
c、aop命名空间与Schema方式配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 目标类 --> <bean ></bean> <!-- 增强所在的类 --> <bean ></bean> <!-- 配置基于schema的切面 --> <aop:config proxy-target-> <!-- 确定增强所在类,并引入 --> <aop:aspect ref="advisor"> <!— 前置增强 method是配置增强所在类中的方法 --> <aop:before method="before" pointcut="target(cn.framelife.spring.schema.Target)"/> <!— 后置增强 Returning 是返回值,必须和method中的参数名是一样的 在Schema配置中,多个切点函数的与操作是and,或操作是or --> <aop:after-returning method="afterReturning" pointcut="execution(* cn.framelife.spring.schema..getName(..)) and args(id,name)" returning="retName" arg-names="retName,id,name"/> <!-- 环绕增强 --> <aop:around method="around" pointcut="execution(* cn.framelife.spring.schema..around(..))"/> <!— 抛出异常增强 throwing是异常对象,必须和method中的参数是一样的 --> <aop:after-throwing method="throwEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))" throwing="ex"/> <!-- Final增强 --> <aop:after method="finalEx" pointcut="execution(* cn.framelife.spring.schema..targetThrow(..))"/> </aop:config> </beans>
d、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); Target target = (Target) context.getBean("target"); target.say(); target.getName(10, "Zhang"); target.around(); target.targetThrow();
e、结果
前置增强 say... 前置增强 getName... 后置增强,返回值为:MRZhang10 入参为:10-Zhang 前置增强 方法执行之前 around... 方法执行之后 前置增强 targetThrow 抛出异常增强,异常信息:我是一个运行期异常... Final增强 Exception in thread "main" java.lang.RuntimeException: 我是一个运行期异常...
3.7.2引介增强的使用
我们还是使用3.6.2@DeclareParents中的例子:Waiter为目标类,然后让目标类拥有ISeller接口的功能:
https://blog.csdn.net/p_3er/article/details/9269407
a、两个接口与两个类
目标类与其接口:
- public interface IWaiter {
- public void service();
- }
- @Component
- public class Waiter implements IWaiter {
- @Override
- public void service() {
- System.out.println("service");
- }
- }
运行期织入到目标类的功能类与其接口:
- public interface ISeller {
- public void sell();
- }
- public class Seller implements ISeller {
- @Override
- public void sell() {
- System.out.println("sell");
- }
- }
b、配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p" xmlns:context="https://www.springframework.org/schema/context" xmlns:aop="https://www.springframework.org/schema/aop" xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.0.xsd https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 目标类 --> <bean ></bean> <!-- 增强所在的类 --> <bean ></bean> <aop:config proxy-target-> <!— 虽然引介增强不需要在增强所在的类中定义一个方法用于增强的实现,但<aop:aspect ref="advisor">中的ref属性依然要指定一个增强Bean --> <aop:aspect ref="advisor"> <!— 引介增强 types-matching 目标类 implement-interface 要织入目标类的接口 default-impl 织入接口的实现类 --> <aop:declare-parents types-matching="cn.framelife.spring.schema.IWaiter+" implement-interface="cn.framelife.spring.schema.ISeller" default-impl="cn.framelife.spring.schema.Seller"/> </aop:aspect> </aop:config> </beans>
c、测试
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); IWaiter waiter = (IWaiter) context.getBean("waiter"); waiter.service(); ISeller seller = (ISeller)waiter; seller.sell();
d、结果
service sell
最后更新:2017-04-03 16:48:31
上一篇:
boa服务器 说明
下一篇:
struts标签格式化时间<s:date name="neowaveBloodPressure.monitorTime" format="yyyy-MM-dd HH:mm:ss&q
删除一个目录下部分类型之外的所有文件的三种方法
利用Doug Lea的并发包实现带超时机制的线程池
数据显示 Google Reader 流量仍然远超 Google+
PostgreSQL 聚合函数讲解 - 2 相关性统计
面向对象中的重载、覆盖和隐藏
晚绑定场景下对象属性赋值和取值可以不需要PropertyInfo
android仪表统计柱状等图标的绘制(开源库XCL-Charts)
Vmware、Virtuozoo、Virtual Server 、Xen四款虚拟机的性能比较
彭安杰:身份验证和数据安全标准可嵌入物联网设计里
【云栖精选7月刊】抛开晦涩的算法、模型,让我们来谈谈互联网架构