package com.haut.grain.junit.test;

public  class Command {
private Object state;
public void setState(Object state) {
    this.state = state;
}
public Object getState() {
    return this.state;
}
public  void execute(){
    System.out.println("当前的状态是:"+state.toString());
}
}

以上是command类为基类

实现类UpCommand

package com.haut.grain.junit.test;

public class UpCommand extends Command{

@Override
public void execute() {
System.out.println("向上命令的状态是:"+this.getState());
} }

CommandManager类

package com.haut.grain.junit.test;

public abstract class CommandManager {

    public void process(Object commandState) {
// grab a new instance of the appropriate Command interface
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
command.execute();
} // okay... but where is the implementation of this method?
protected abstract Command createCommand();
}

配置文件

	<bean id="upCommand" class="com.haut.grain.junit.test.UpCommand" scope="singleton"></bean>
<bean id="commandManager" class="com.haut.grain.junit.test.CommandManager">
<lookup-method name="createCommand" bean="upCommand"/>
</bean>

    总结:上面就是通过createCommand方法来注入command对象,当是抽象方法时替换,如果是实现的方法了覆盖。

spring 方法注入的更多相关文章

  1. spring 方法注入、方法替换

    spring 提供了很多的注入方式,set注入.构造注入.p命名空间.c命名空间.字段注入等等,这些没啥可说的了. 方法注入 因为开发时我需要spring管理一个实例,但是这个实例并非单例,应该每一次 ...

  2. Spring方法注入的使用与实现原理

    一.前言   这几天为了更详细地了解Spring,我开始阅读Spring的官方文档.说实话,之前很少阅读官方文档,就算是读,也是读别人翻译好的.但是最近由于准备春招,需要了解很多知识点的细节,网上几乎 ...

  3. Spring应用教程-2 方法注入

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 我们通常使用lookup方法注入,它可使Spring替换一个Bean的抽象或具体方法,返回查找容器中,其他Bea ...

  4. Spring 依赖注入,在Main方法中取得Spring控制的实例

    Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...

  5. Spring揭秘 读书笔记 四----方法注入

    我们知道,拥有prototype类型scope的bean,在请求方每次向容器请求该类型对象的时候,容器都会返回一个全新的该对象实例. 我们看下面的例子: public class MockNewsPe ...

  6. spring依赖注入之构造函数注入,set方法注入

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  7. spring注入 属性注入 构造器注入 set方法注入

    spring注入 属性注入 构造器注入 set方法注入(外部bean注入)

  8. Spring依赖注入的Setter注入(通过get和set方法注入)

    Spring依赖注入的Setter注入(通过get和set方法注入) 导入必要的jar包(Spring.jar和commonslogging.jar) 在src目录下建立applicationCont ...

  9. Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入

    所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入   1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...

随机推荐

  1. memcached 学习(一)

    memcached 是以LiveJournal 旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件.现在已成为 mixi. hatena. Faceboo ...

  2. 缓存之Memcached

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  3. C语言,不是从hello world开始

    开始看C语言,主要是复习,所以就没必要从hello world开始了,写点例子熟悉下就好了. 使用公式℃=(5/9)(℉-32)打印下列华氏温度与摄氏温度对照表: #include <stdio ...

  4. spring batch学习笔记

    Spring Batch是什么?       Spring Batch是一个基于Spring的企业级批处理框架,按照我师父的说法,所有基于Spring的框架都是使用了spring的IoC特性,然后加上 ...

  5. 学习练习 java面向对象封装汽车

    package com.hanqi; //汽车 public class Car { // 车牌 private String CheP; // 油箱容量 private double YouXRL ...

  6. python常用内置函数

    Python所以内置函数如下: 下面列举一些常用的内置函数: chr()和ord() chr()将数字转换为对应的ascii码表字母 >>> r=chr(65) >>&g ...

  7. MFC ComboBox的使用

    前言 Combo Box (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项,同时也可以直接在文本框里面输 ...

  8. HTML转义字符集合

    readme:这次可以不readme了,因为这个是我copy过来的~ ISO Latin-1字符集:  — 制表符Horizontal tab  — 换行Line feed  — 回车Carriage ...

  9. Could not resolve this reference. Could not locate the assembly

    Rebuild Project 的时候提示找不到NewtonJson 组件,重新添加了Dll(Newtonsoft.Json.dll),依然抛错. 解决办法,将Dll(Newtonsoft.Json. ...

  10. webstorm & phpstorm破解

    webstorm: http://idea.qinxi1992.cn/ http://idea.goxz.gq http://v2mc.net:1017 http://idea.imsxm.com h ...