spring 方法注入
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 方法注入的更多相关文章
- spring 方法注入、方法替换
spring 提供了很多的注入方式,set注入.构造注入.p命名空间.c命名空间.字段注入等等,这些没啥可说的了. 方法注入 因为开发时我需要spring管理一个实例,但是这个实例并非单例,应该每一次 ...
- Spring方法注入的使用与实现原理
一.前言 这几天为了更详细地了解Spring,我开始阅读Spring的官方文档.说实话,之前很少阅读官方文档,就算是读,也是读别人翻译好的.但是最近由于准备春招,需要了解很多知识点的细节,网上几乎 ...
- Spring应用教程-2 方法注入
作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 我们通常使用lookup方法注入,它可使Spring替换一个Bean的抽象或具体方法,返回查找容器中,其他Bea ...
- Spring 依赖注入,在Main方法中取得Spring控制的实例
Spring依赖注入机制,在Main方法中通过读取配置文件,获取Spring注入的bean实例.这种应用在实训的时候,老师曾经说过这种方法,而且学Spring入门的时候都会先学会使用如何在普通的jav ...
- Spring揭秘 读书笔记 四----方法注入
我们知道,拥有prototype类型scope的bean,在请求方每次向容器请求该类型对象的时候,容器都会返回一个全新的该对象实例. 我们看下面的例子: public class MockNewsPe ...
- spring依赖注入之构造函数注入,set方法注入
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- spring注入 属性注入 构造器注入 set方法注入
spring注入 属性注入 构造器注入 set方法注入(外部bean注入)
- Spring依赖注入的Setter注入(通过get和set方法注入)
Spring依赖注入的Setter注入(通过get和set方法注入) 导入必要的jar包(Spring.jar和commonslogging.jar) 在src目录下建立applicationCont ...
- Spring第六弹—-依赖注入之使用构造器注入与使用属性setter方法注入
所谓依赖注入就是指:在运行期,由外部容器动态地将依赖对象注入到组件中. 使用构造器注入 1 2 3 4 <constructor-arg index=“0” type=“java.lang. ...
随机推荐
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part8:Perfect Forwarding(完美转发):解决方案
本文为第八部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...
- Android开发-API指南-Intent和Intent过滤器
Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...
- 恶心的Oracle的if else if...
出处:http://blog.sina.com.cn/s/blog_407d47e60100d8ig.html 前段时间写Oracle存储过程就遇到问题.原来写成这样if 1=2 then null ...
- gem5 运行x86全系统仿真
使用gem5可以启动Linux内核,称为全系统模拟,启动之后,可以通过telent连接,进行访问,但四telent有时不稳定,gem5推荐使用m5term进行连接访问,整个步骤如下: (1)打开终端, ...
- confluence启动关闭
cd /opt/atlassian/confluence/bin startup.sh shutdown.sh
- CODEVS1291 火车线路
http://codevs.cn/problem/1291/ 题目描述 Description 某列火车行使在C个城市之间(出发的城市编号为1,结束达到的城市的编号为C),假设该列火车有S个座位,现在 ...
- php 调用.net的webservice 需要注意的
首先 SoapClient类这个类用来使用Web services.SoapClient类可以作为给定Web services的客户端.它有两种操作形式:* WSDL 模式* Non-WSDL 模式在 ...
- CLRS:median and order statistics
//maximum and minimum 暴力遍历 O(n) //i-th element dicide and conquer random_selected_partition ...
- README.md
#gzq-h5app 本项目依赖于nodejs,请尽量安装最新版本. 需要先将Grunt命令行(CLI)安装到全局环境中.安装时可能需要使用sudo(针对OSX.*nix.BSD等系统中)权限或者作为 ...
- Unieap3.5-Grid翻页不提示修改
<toolbar export="{defaultType:'server'}" paging="{onPagingModified:ssSettleCheck.o ...