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. selenium 速查手册 python版

    1.安装与配置 pip install selenium 基本使用selenium都是为了动态加载网页内容用于爬虫,所以一般也会用到phantomjs mac下如果要配置phantomjs环境的话 e ...

  2. Android开发-API指南-Fragment

    Fragments 英文原文:http://developer.android.com/guide/components/fragments.html 采集日期:2014-12-31 在本文中 设计理 ...

  3. Android开发-API指南-<intent-filter>

    <intent-filter> 英文原文:http://developer.android.com/guide/topics/manifest/intent-filter-element. ...

  4. nginx 代理配置文件实例

    安装NGINX前要先安装PCRE正则表达式库: ./configure --prefix=/usr/local/pcre 出现以下错误  (一般./configure即可, 笔者这里是直接./conf ...

  5. 关于javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;

    今天遇到这样一个异常: 严重: Servlet.service() for servlet jsp threw exceptionjavax.servlet.jsp.JspTagException: ...

  6. 洛谷P2727 01串 Stringsobits

    P2727 01串 Stringsobits 24通过 55提交 题目提供者该用户不存在 标签USACO 难度普及+/提高 提交  讨论  题解 最新讨论 这题的思路是啥啊!!!跪求- 题目背景 考虑 ...

  7. malloc、calloc、realloc的区别

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.       内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...

  8. Backup App's data without rooting the phone

    First I'd like to let you know that my phone is Android 6.0 Marshmallow. So it works on the latest A ...

  9. 何时使用 Em 与 Rem

    原文  http://www.w3cplus.com/css/when-to-use-em-vs-rem.html   你可能已经很熟练使用这两个灵活的单位,但你可能不完全了解何时使用rem,何时使用 ...

  10. 第六章_PHP数组

    1.PHP支持两种数组:索引数组(indexed array)和关联数组(associative array),前者使用数字作为键,后者使用字符串作为键. 2.遍历索引数组 2.1 for循环语句 2 ...