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. Oracle 物化视图创建

    create materialized view MV_XXXXrefresh fast on commitwith rowidenable query rewriteasselect * from ...

  2. 学习练习 java产生6个不同的数字

    public static void main(String[] args) { Random r=new Random(); int arr[]=new int[6]; for(int i=0;i& ...

  3. android大项目运行中出现问题汇总

    Android 项目中,特别是当项目文件和规模达到一定的程度后,会引发一些平常不常见的问题. 下面对遇到的一些问题做一个汇总和总结. scenario 1: 在项目中,我们采用了chromimum内核 ...

  4. Date获取时间段

    /** * */ package com.chinabase.common.util; /** * @author yuanji * @created on:Sep 19, 2008 */ impor ...

  5. VirtualBox是什么

    VirtualBox 是一款 x86 虚拟机软件.原由德国innotek公司开发,2008年Sun收购了Innotek,而Sun于2010年被Oracle收购,2010年1月21日改 名成 Oracl ...

  6. 网站屏蔽指定ip

    修改.htaccess文件 Order Deny,Allow     //开启屏蔽Deny from 124.64.242.117  //要屏蔽的ip

  7. Android IOS WebRTC 音视频开发总结(四三)-- 诚信交易案例分享

    本文主要记录一些诚信交易的案例(两个陌生人之间没有合同,没有订金,没有讨价还价,完全靠诚信完成的交易), 特别纪录下来并不是因为金额有多高,而是因为在现在这种社会要完成这样的交易太难,特别是像咨询这种 ...

  8. projecteuler Sum square difference

    The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square of ...

  9. 2_2数据类型与C#部分语法[wp8特色开发与编程技巧]

    2_2数据类型 -5min 类型介绍 在上个视频中我们构建了我们第一个应用.这一次我们要来了解下c#的数据类型 众所周知,在我们已认知的世界里,我们把文字分为数字与字符.在程序的世界里面我们把数据分为 ...

  10. 必须会的SQL语句(一) 创建数据库与删除数据库

    1.创建数据库   Create database 名称 on primary {    name ='名称',    filename ='c:\xx\名称.mdf',    size = 10mb ...