Code:
public abstract class Instrumentalist implements Performer {
public Instrumentalist() {}
public void perform() throws PerformanceException {
System.out.print("Playing " + song + " : ");
getInstrument().play();
}
private String song;
public void setSong(String song) {
this.song = song;
}
public abstract Instrument getInstrument();
}
XML:
<bean id="stevie" class="com.springinaction.springidol.Instrumentalist">
<lookup-method name="getInstrument" bean="guitar" />
<property name="song" value="Greensleeves" />
</bean>
<bean id="guitar" class="com.springinaction.springidol.Guitar"
scope="prototype" />
Notice: Bean "guitar" must be prototype otherwise always same instance will be returned
As with <replaced-method>, the name attribute of <lookup-method> indicates the method that is to be replaced. Here we’re replacing the getInstrument() method. The bean attribute refers to a bean that should be returned when getInstrument() is called. In this case, we’re wiring in the bean whose id is guitar. As a result, the getInstrument() method is effectively overridden as follows:
public Instrument getInstrument() {
ApplicationContext ctx = …;
return (Instrument) ctx.getBean("guitar");
}
On its own, getter injection is just a reversal of setter injection. However, it makes a difference when the referenced bean is prototype scoped:Even though it’s prototype scoped, the guitar method would only be injected once into a property if we were using setter injection. By injecting it into the get-
Instrument() method through getter injection, however, we ensure that every call to getInstrument() will return a different guitar. This can come in handy if a guitarist breaks a string in the middle of a performance and needs a freshly stringed instrument.
You should be aware that although we used <lookup-method> to perform getter injection on the getInstrument() method, there’s nothing about <lookupmethod> that requires that the replaced method actually be a getter method (i.e., one whose name starts with get). Any non-void method is a candidate for replacement with <lookup-method>.
It’s important to note that while method injection allows you to change a method’s implementation, it’s not capable of replacing a method’s signature. The parameters and return type must
分享到:
相关推荐
eclipse修改setter/getter配置,实现自动生成方法时自动抽取属性上的注释:https://blog.csdn.net/yyaijunji/article/details/84639609
eclipse中的setter与getter注释,实体的编写,在代码编写工程中能清晰了解此意
第二十五课 getter 设置学习目录getter 设置介绍与用法一.getter 设置介绍与用法Vuex 允许我们在 store 中定义 getter,就像计
java 的eclipse或idea等 定义变量时 直接引入lombok.jar包 在类外部 引入@AllArgsConstructor @NoArgsConstructor @Data 简便方法引入getter/setter/tostring等方法
主要介绍了Vue getter setter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
已经拥有一年经验的你真的了解get set方法吗?也许不!以前我以为都有上架项目,这都是太基础的东西,但今天我重新回头看了以前做的笔记,真的是书读百遍,其义自见。
java普通类编译成json但只是当前类的有getter、setter方法的版本
eclipse 自动为getter和setter添加中文注释 详细参见 http://blog.csdn.net/kongguoan/article/details/38293137
c++的成员变量的get和set生成器,简单易用。
jfinal model自动生成工具,自带setter与getter
NULL 博文链接:https://lydawen.iteye.com/blog/501004
自动生成带注释的getter和setter方法(注释)插件
NULL 博文链接:https://cjk87927.iteye.com/blog/1149855
go-getter -一个 Go库用于从各种来源使用URL下载文件或目录
eclipse中setter/getter方法自动添加属性注释,详情参考http://blog.csdn.net/yuxinabc/article/details/49179113
FastCoding是一个对Xcode所扩展的插件,主要是根据java的开发工具 eclipse 快速生成属性的getter ,setter 方法有感而发.出于兴趣开发本插件,目前主要功能是根据.h,.m文件声明的属性,自动生成get,set,lazy实例化方法
功能:批量生成属性的懒加载 getter 应用场景 :手写ui代码可以批量生成 github地址:https://github.com/hackxhj/getterMake-Xcode
吸气剂这个 Javascript 库旨在创建一个简单而轻便的 getter/setter。如何使用它使用 getter/setter 方法创建对象 // Create an object with getter/setter methodvar obj = { name : new getterSetter ( 'John' )} ;...
与setter和getter方法有关的案例
C-Sharp-Getter-SetterNEW:您可以了解如何使用getter setter选项运行c#方法。(我们着重于Java getter setter)