Ⅳspring的点点滴滴--方法和事件
承接上文
方法和事件
.net篇(环境为vs2012+Spring.Core.dll v1.31)
public abstract class MethodDemo
{
protected abstract SingleMethod CreateMethodByAbstract();
public virtual SingleMethod CreateMethodByVireual() { return null; }
public virtual String add(int x,int y){return x+y+"";}
public virtual int add(int x, int y,int z) { return 0; }
public SingleMethod Process1()
{
return this.CreateMethodByAbstract();
}
}
public class MethodReplace :
Spring.Objects.Factory.Support.IMethodReplacer
{
public object Implement(object target,
System.Reflection.MethodInfo method, object[] arguments)
{
return "2";
}
}
public delegate string Handler(String arg);
public class HandlerDemo
{
public event Handler eventHandler;
public void fire()
{
//调用事件
if (eventHandler != null)
{
Console.WriteLine(eventHandler(null));
}
}
}
public class SingleMethod
{
public String Demo { get; set; }
public string HandlerTigger(String arg)
{
return arg+"触发";
}
}<object id="single" type="SpringBase.SingleMethod, SpringBase" singleton="false">
<property name="Demo" value="1"></property>
</object>
<object id="myObject" type="SpringBase.MethodDemo, SpringBase"
depends-on="methReplace,hadlerDemo" >
<lookup-method name="CreateMethodByAbstract" object="single"/>
<lookup-method name="CreateMethodByVireual" object="single"/>
<replaced-method name="add" replacer="methReplace">
<arg-type match="Int"/>
<arg-type match="Int"/>
</replaced-method>
</object>
<object id="methReplace" type="SpringBase.MethodReplace, SpringBase"></object>
<object id="hadlerDemo" type="SpringBase.HandlerDemo, SpringBase"></object>下面是事件的触发
IApplicationContext ctx = ContextRegistry.GetContext("test");
HandlerDemo dao = (HandlerDemo)ctx.GetObject("hadlerDemo");
ctx.PublishEvents(dao);
SingleMethod single = (SingleMethod)ctx.GetObject("single");
ctx.Subscribe(single);
dao.fire();
- lookup-method和replaced-method都必须是虚方法或者抽象方法
- replaced-method的类必须继承Spring.Objects.Factory.Support.IMethodReplacer,
实现Implement方法,替换方法返回int的时候会有问题- PublishEvents发布事件,Subscribe订阅事件
java篇(环境为Maven+Jdk1.7+IntelliJ IDEA 12.1.4)
package springdemo;
import org.springframework.beans.factory.support.MethodReplacer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import java.lang.reflect.Method;
public abstract class MethodDemo {
protected abstract SingleMethod CreateMethodByAbstract();
public SingleMethod CreateMethodByVireual() {
return null;
}
public Integer add(Integer x, Integer y) {
return x + y;
}
}
class SingleMethod {
private String demo;
public SingleMethod(String demo) {
this.demo = demo;
}
public String getDemo() {
return demo;
}
public void setDemo(String demo) {
this.demo = demo;
}
}
class MethodReplace implements MethodReplacer {
@Override
public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
System.out.println("MethodReplace: true");
return args.length;
}
}
class EventHandler extends ApplicationEvent {
public EventHandler(Object source) {
super(source);
}
}
class EventListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof EventHandler) {
EventHandler singleMethod = (EventHandler) event;
System.out.println("DemoEvent: " + singleMethod.toString());
}
}
}<bean class="springdemo.EventListener"/>
<bean id="singleMethod" class="springdemo.SingleMethod">
<constructor-arg index="0" value="test"/>
</bean>
<bean id="methodReplace" class="springdemo.MethodReplace"/>
<bean id="methodDemo" class="springdemo.MethodDemo">
<lookup-method name="CreateMethodByAbstract" bean="singleMethod" />
<lookup-method name="CreateMethodByVireual" bean="singleMethod" />
<replaced-method name="add" replacer="methodReplace" />
</bean>下面是事件的触发
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:bean.xml");
ctx.publishEvent(new EventHandler(ctx));
- replaced-method的类必须继承MethodReplacer,实现reimplement方法
- 事件监听必须继承ApplicationListener并且要在bean.xml文件里面配置,
否则无效,不过id可以忽略,因为是所有的事件都会进所以
最好instanceof判断下是不是自己要的事件,否则会报错,- 事件必须继承ApplicationEvent,构造函数的参数为发起者
- 通过ctx.publishEvent(new EventHandler(ctx))来触发事件
java和Csharp的共同点
- lookup-method为实现方法,replaced-method为替换方法,并且方法不能包含任何参数
- depends-on表示依赖那个对象用逗号分隔
- replaced-method的类继承接口后的方法第一个参数为要替换的对象,第二个为替换的方法
第三个为方法参数- replaced-method里面的arg-type是为了匹配参数类型重载的时候,
当只有一个方法的时候可以忽略
- 下一篇:Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
- 上一篇:Ⅲ.spring的点点滴滴--赋值
- 本文链接地址:Ⅳspring的点点滴滴--方法和事件
Ⅳspring的点点滴滴--方法和事件的更多相关文章
- Ⅲ.spring的点点滴滴--赋值
承接上文 对象的赋值(调用方式都一样不再阐述) .net篇(环境为vs2012+Spring.Core.dll v1.31) public class PropertyDemo{ public Sys ...
- Ⅴ.spring的点点滴滴--引用其他对象或类型的成员
承接上文 引用其他对象或类型的成员 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class Person { public string Name { ...
- Ⅱ.spring的点点滴滴--对象
承接上文 对象的各种实例化 .net篇(环境为vs2012+Spring.Core.dll) 修改原来的PersonDao对象为 public class PersonDao : IPersonDao ...
- XI.spring的点点滴滴--IObjectFactoryPostProcessor(工厂后处理器)
承接上文 IObjectFactoryPostProcessor(工厂后处理器)) 前提是实现接口的对象注册给当前容器 直接继承的对象调用这个postProcessBeanFactory方法,参数为工 ...
- Ⅹ.spring的点点滴滴--IObjectPostProcessor(对象后处理器)
承接上文 IObjectPostProcessor(对象后处理器) 前提是实现接口的对象注册给当前容器 C#: 通过继承AbstractObjectFactory对象的AddObjectPostPro ...
- Ⅷ.spring的点点滴滴--抽象对象和子对象
承接上文 抽象对象和子对象 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class parent { public string Name { get; ...
- Ⅵ.spring的点点滴滴--自定义类型转换器
承接上文 自定义类型转换器 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class CustomeConverter : TypeConverter{ ...
- Ⅶ.spring的点点滴滴--自定义对象行为
承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...
- Spring ApplicationContext(八)事件监听机制
Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...
随机推荐
- [转] ArcEngine中打开各种数据源(WorkSpace)的连接
原文 ArcEngine中打开各种数据源(WorkSpace)的连接(SDE.personal/File.ShapeFile.CAD数据.影像图.影像数据集) ArcEngine 可以接受多种数据源. ...
- HNU OJ10086 挤挤更健康 记忆化搜索DP
挤挤更健康 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 339, A ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_list_items(self, locator)
def get_list_items(self, locator): """Returns the values in the select list identifie ...
- 安装zabbix server
本文安装的zabbix版本为2.2 步骤 1.安装php 5.3.3 rpm -e `rpm -qa | grep php` rpm -ivh http://mirrors.163.com/cento ...
- Tomcat普通用户部署教程(生产服务器)
1.环境准备 JDK安装 解压 tar xf tomcat-xx.tar.gz -C /data/soft cd /data/soft 重命名 mv tomcat-xx tom ...
- linux下ssh使用rsa验证登陆MACOX
由于项目的需求,我这边ubuntu下常常需要SSH访问另外一台MACOS. 每次输入密码有点烦,就想到RSA公钥和密钥验证的方法. 像所有教程上讲的一样,本机执行 gong@hzsx:~$ ssh-k ...
- 【组队训练】2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest
好多oj都崩掉了,于是打了cf.. 开始开的最后一题...尼玛题好长终于看完了...神题不会.... I过了好多人..看了下,一眼题...随便敲了下,1A ]; int main(){ int n, ...
- 现代程序设计——homework-02
关于题目: 题目地址:http://www.cnblogs.com/xinz/p/3318230.html 首先,不得不说自从写完第一次作业,我就开始“抠”这个题,第一眼看这个题就感觉好“坑”,读一遍 ...
- hdoj 4325 Flowers【线段树+离散化】
Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- oracle表数据误删还原
首先,找到数据删除前的一个时间点. select timestamp_to_scn(to_timestamp('2013-10-12 8:30:00', 'YYYY-MM-DD HH24:MI:SS' ...