aop 笔记 引入

本文介绍Spring AOP中的引入通知,展示了如何通过IntroductionInterceptor实现实现动态接口的复合对象,包括自定义IntroductionInterceptor及使用DelegatingIntroductionInterceptor简化过程。
引入和其它类型的spring通知有所不同.其它类型的通知是在方法调用的周围织入到不同的连接点.而引入则是影响整个类.
引入让你能动态地建立复合对象.
spring 通过一个特殊的方法拦截接口IntroductionInterceptor
                     
java 代码
 
  1. boolean implementsInterface(Class intf);  
  2. 这个方法判断是否是引入的接口  
要引入的接口
java 代码
 
  1. package aop.introduction;  
  2.   
  3. public interface IChange {  
  4.         public boolean ichange();  
  5. }  
现在我们实现一个introductionInterceptor
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.aopalliance.intercept.MethodInvocation;  
  4. import org.springframework.aop.IntroductionInterceptor;  
  5.   
  6.   
  7. public class IChangeAdvisora implements IntroductionInterceptor,IChange{  
  8.   
  9.     public Object invoke(MethodInvocation m) throws Throwable {  
  10.         if(implementsInterface(m.getMethod().getDeclaringClass())){  
  11.             return m.getMethod().invoke(this, m.getArguments());
  12.             //通过MethodInvocation 和invoke代理要引入的方法
  13.         }else{  
  14.             return m.proceed();  //委托其它方法,不是引入,就像那个环绕通知
  15.         }  
  16.     }  
  17.   
  18.     public boolean implementsInterface(Class intf) {  
  19.         return intf.isAssignableFrom(IChange.class);  
  20.     }  
  21.   
  22.     public boolean ichange() {  
  23.         System.out.println("intrduction");  
  24.         return false;  
  25.     }  
  26.   
  27. }  
spring 为我们提供一个方便类的处理大多数的应用.DelegatingIntroductInterceptor
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.springframework.aop.support.DelegatingIntroductionInterceptor;  
  4.   
  5. public class IChangeAdvisor extends DelegatingIntroductionInterceptor   
  6.                    implements IChange  {  
  7.   
  8.     public boolean ichange() {  
  9.         // TODO 自动生成方法存根  
  10.         System.out.println("ichage");  
  11.       
  12.         return false;  
  13.     }     
  14. }  //比起上面那个类算得比较简洁,DelegatingIntroductionInterceptor 替我们实现invoke()
如果你想实现方法的拦截调用invoke方法
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.aopalliance.intercept.MethodInvocation;  
  4. import org.springframework.aop.support.DelegatingIntroductionInterceptor;  
  5.   
  6. public class IChangeAdvisorb extends DelegatingIntroductionInterceptor implements IChange {  
  7.   
  8.     public boolean ichange() {  
  9.         // TODO 自动生成方法存根  
  10.         return false;  
  11.     }  
  12.     public Object invoke(MethodInvocation e) throws Throwable{  
  13.         String name = e.getMethod().getName();  
  14.         if(name.indexOf("set") == 0){   //这里拦截set方法  
  15.             throw new IllegalMonitorStateException();  
  16.         }  
  17.         return super.invoke(e);  
  18.     }  
  19.   
  20. }  
下面是配制
java 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.     <bean id="targetbean" class="aop.introduction.TagetBean" />  
  5.   
  6.     <bean id="iChangeAdvisor" class="aop.introduction.IChangeAdvisorb" />  
  7.  //这里配制引入advisor,看你用得是那个  
  8.     <bean id="advisor"  
  9.         class="org.springframework.aop.support.DefaultIntroductionAdvisor">  
  10.         <constructor-arg>  
  11.             <ref bean="iChangeAdvisor" />  
  12.         </constructor-arg>  
  13.     </bean>  
  14.     <bean id="bean"  
  15.         class="org.springframework.aop.framework.ProxyFactoryBean">  
  16.         <property name="target">  
  17.             <ref bean="targetbean" />  
  18.         </property>  
  19.         <property name="interceptorNames">  
  20.             <list>  
  21.                 <value>advisor</value>  
  22.             </list>  
  23.         </property>  
  24.         <property name="proxyInterfaces">  
  25.             <value>aop.introduction.IChange</value>  
  26.         </property>  
  27.         <property name="proxyTargetClass">  
  28.             <value>true</value>  
  29.         </property>  
  30.     </bean>  
  31. </beans>  
java 代码
 
  1. package aop.introduction;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  5.   
  6. public class IntroductionTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext ctx = new FileSystemXmlApplicationContext(  
  10.                 "./src/applicationContext.xml");  
  11.   
  12.         TagetBean bean = (TagetBean) ctx.getBean("bean");  
  13. //                 ((IChange) ctx.getBean("bean")).ichange();  
  14.        IChange mod = (IChange) bean;  
  15.   
  16.         // test interfaces  
  17.         System.out.println("Is TargetBean?: " + (bean instanceof TagetBean));  
  18.         System.out.println("Is IsModified?: " + (bean instanceof IChange));  
  19.   
  20.         // test is modified implementation  
  21. //        mod.ichange();  
  22.        // bean.setName("ddd");  
  23.         System.out.println("Has been modified?: " + mod.ichange());  
  24.     }  
  25. }             
  26.          
内容概要:本文围绕六自由度机械臂的人工神经网络(ANN)设计展开,重点研究了正向与逆向运动学求解、正向动力学控制以及基于拉格朗日-欧拉法推导逆向动力学方程,并通过Matlab代码实现相关算法。文章结合理论推导与仿真实践,利用人工神经网络对复杂的非线性关系进行建模与逼近,提升机械臂运动控制的精度与效率。同时涵盖了路径规划中的RRT算法与B样条优化方法,形成从运动学到动力学再到轨迹优化的完整技术链条。; 适合人群:具备一定机器人学、自动控制理论基础,熟悉Matlab编程,从事智能控制、机器人控制、运动学六自由度机械臂ANN人工神经网络设计:正向逆向运动学求解、正向动力学控制、拉格朗日-欧拉法推导逆向动力学方程(Matlab代码实现)建模等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握机械臂正/逆运动学的数学建模与ANN求解方法;②理解拉格朗日-欧拉法在动力学建模中的应用;③实现基于神经网络的动力学补偿与高精度轨迹跟踪控制;④结合RRT与B样条完成平滑路径规划与优化。; 阅读建议:建议读者结合Matlab代码动手实践,先从运动学建模入手,逐步深入动力学分析与神经网络训练,注重理论推导与仿真实验的结合,以充分理解机械臂控制系统的设计流程与优化策略。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值