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. 在使用 百度编辑器 Ueditor 时,不能进入 Controller 相应的 Action 的处理方法

    如果在前端的页面中使用了 Ueditor 编辑器,那么在提交表单数据时,将不会执行 期望的 Controller 中的 Action ,造成这样的原因是: 在 MVC 4 的框架中,当前端页面需要提交 ...

  2. powerdesigner逆向导出oracle数据库结构显示备注

    最近接到命令,要将oracle数据库的结构导出为pdm文件供其他同事使用,逆向工程导出数据库结构比较方便,但是发现导出的数据库结构没有注释,这是很郁闷的事情: 查过网上很多资料都是sqlserver的 ...

  3. Android常见包

    Android.jar常见包 android.app-----------提供高层的程序模型.提供基本的运行环境android.content-------包含各种的对设备上的数据进行访问和发布的类a ...

  4. ES6生成器基础

    ES6引进的最令人兴奋的特性就是一种新的函数生成方式,称为生成器(generator).名称有点奇怪,但是第一眼看上去行为更加奇怪.文章主要介绍生成器如何工作,然后让你明白为什么他们对于未来的JS会有 ...

  5. 五、用户数据报传输(UDP)

    1.UDP常用的发送和接收函数 int recvfrom(int sockfd,void *buf,int len,unsigned int flags,struct sockaddr *from,i ...

  6. 10 Code Coverage Tools for C & C++

    Code coverage is a measure used in software testing that describes the degree to which the source co ...

  7. 0511 backlog 项目管理

    SCRUM 这次的作业就是确定SCRUM的计划,确定sprint backlog的一个冲刺周期,而这个周期是两个星期.争取在两周内发布1.0版本. 本次作业以网站构建为主: ID       NAME ...

  8. leetcode 36

    36. Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudok ...

  9. poj1942 Paths on a Grid

    处理阶乘有三种办法:(1)传统意义上的直接递归,n的规模最多到20+,太小了,在本题不适用,而且非常慢(2)稍快一点的算法,就是利用log()化乘为加,n的规模虽然扩展到1000+,但是由于要用三重循 ...

  10. 自制docker basic image

    docker的安装和入门见官网教程:http://docs.docker.com/ 下面是自制docker basic image的步骤,以ubuntu为例. 1. 安装debootstrap apt ...