使用Spring Introducation 让Java类实现动态语言特性
2007-10-10 16:14
906 查看
当我们没有一个实现类源代码以致不能为实现类增加新的方法时,我们在java语言中往往是无法实现的,但动态语言比(如JS),对动态对象增加可操作的方法是很容易得,我们借助Spring的Introduction这个特殊的advise,同样可以实现动态语言的这个特性
原始的业务接口及实现
package Introduction;
public interface ISome ...{
public void doSome();
}
package Introduction;
public class Some implements ISome ...{
public void doSome() ...{
System.out.println("原来的方法");
}
}
我们新增的业务接口和实现,其中实现类同时实现了业务接口和Spring Introduction接口
package Introduction;
public interface IOther ...{
public void doOther();
}
package Introduction;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;
public class Other implements IOther, IntroductionInterceptor ...{
public void doOther() ...{
System.out.println("增加的职责");
}
public Object invoke(MethodInvocation methodInvocation) throws Throwable ...{
if(implementsInterface(methodInvocation.getMethod().getDeclaringClass()))...{
return methodInvocation.getMethod().invoke(this, methodInvocation.getArguments());
}else...{
return methodInvocation.proceed();
}
}
//判断是否来自与IOther接口的方法调用
public boolean implementsInterface(Class clazz) ...{
return clazz.isAssignableFrom(IOther.class);
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="some" class="Introduction.Some"/>
<bean id="other" class="Introduction.Other"/>
<bean id="otherAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="other"></constructor-arg>
<constructor-arg value="Introduction.IOther"></constructor-arg>
</bean>
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="Introduction.ISome"></property>
<property name="target" ref="some"></property>
<property name="interceptorNames">
<list>
<value>otherAdvisor</value>
</list>
</property>
</bean>
</beans>
测试代码:
package Introduction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring...{
public static void main(String args[]) throws Exception...{
ApplicationContext ctx=new ClassPathXmlApplicationContext("Introduction/applicationContext.xml");
ISome some=(ISome)ctx.getBean("proxyFactoryBean");
some.doSome();//执行原来的方法
System.out.println("------------------");
((IOther)some).doOther(); //执行新增的方法
}
}
测试结果:
原来的方法
------------------
增加的职责
原始的业务接口及实现
package Introduction;
public interface ISome ...{
public void doSome();
}
package Introduction;
public class Some implements ISome ...{
public void doSome() ...{
System.out.println("原来的方法");
}
}
我们新增的业务接口和实现,其中实现类同时实现了业务接口和Spring Introduction接口
package Introduction;
public interface IOther ...{
public void doOther();
}
package Introduction;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.IntroductionInterceptor;
public class Other implements IOther, IntroductionInterceptor ...{
public void doOther() ...{
System.out.println("增加的职责");
}
public Object invoke(MethodInvocation methodInvocation) throws Throwable ...{
if(implementsInterface(methodInvocation.getMethod().getDeclaringClass()))...{
return methodInvocation.getMethod().invoke(this, methodInvocation.getArguments());
}else...{
return methodInvocation.proceed();
}
}
//判断是否来自与IOther接口的方法调用
public boolean implementsInterface(Class clazz) ...{
return clazz.isAssignableFrom(IOther.class);
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="some" class="Introduction.Some"/>
<bean id="other" class="Introduction.Other"/>
<bean id="otherAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
<constructor-arg ref="other"></constructor-arg>
<constructor-arg value="Introduction.IOther"></constructor-arg>
</bean>
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="Introduction.ISome"></property>
<property name="target" ref="some"></property>
<property name="interceptorNames">
<list>
<value>otherAdvisor</value>
</list>
</property>
</bean>
</beans>
测试代码:
package Introduction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring...{
public static void main(String args[]) throws Exception...{
ApplicationContext ctx=new ClassPathXmlApplicationContext("Introduction/applicationContext.xml");
ISome some=(ISome)ctx.getBean("proxyFactoryBean");
some.doSome();//执行原来的方法
System.out.println("------------------");
((IOther)some).doOther(); //执行新增的方法
}
}
测试结果:
原来的方法
------------------
增加的职责
相关文章推荐
- 使用Spring Introducation 让Java类实现动态语言特性
- Java版C语言版简单使用静态语言实现动态数组的方法
- JAVAWEB开发之Servlet3.0新特性的使用以及注解的详细使用和自定义注解的方法、动态代理的使用、利用动态代理实现细粒度的权限控制以及类加载和泛型反射
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
- 数据结构与算法分析(Java 语言描述)(35)—— 使用两个栈实现一个队列
- JavaWeb开发之使用jQuery与Ajax实现动态联级菜单效果
- JAVAWEB开发之mybatis详解(一)——mybatis的入门(实现增删改查操作)、自定义别名、抽取代码块以及动态SQL的使用
- 使用Java 动态代理实现AOP
- java语言基础入门——用反射实现动态代理
- 使用JAVA中的动态代理实现数据库连接池
- JAVA中使用代码创建多数据源,并实现动态切换(二)-集成分布式事务
- 使用JAVA中的动态代理实现数据库连接池
- Hadoop Streaming: 使用Java以外的语言去实现Map/Reduce
- 使用JAVA中的动态代理实现数据库连接池
- java使用动态代理来实现AOP(日志记录)
- 使用JAVA中的动态代理实现数据库连接池
- JAVA动态代理--使用JAVA API实现JAVA动态代理
- 通过使用java.lang.reflect.Proxy实现动态代理
- java语言基础(92)——使用Lock锁实现线程安全
- 使用Java的动态代理技术实现对象适配器模式