Basic method replacement
Code:
public interface Performer(){
public void perform();
}
public class Magician implements Performer {
public Magician() {}
public void perform() throws PerformanceException {
System.out.println(magicWords);
System.out.println("The magic box contains...");
System.out.println(magicBox.getContents());
}
// injected
private MagicBox magicBox;
public void setMagicBox(MagicBox magicBox) {
this.magicBox = magicBox;
}
private String magicWords;
public void setMagicWords(String magicWords) {
this.magicWords = magicWords;
}
}
public class MagicBoxImpl implements MagicBox {
public MagicBoxImpl() {}
public String getContents() {
return new String("A beautiful assistant");
}
}
public class TigerReplacer implements MethodReplacer {
public Object reimplement(Object target, Method method,
Object[] args) throws Throwable {
return "A ferocious tiger";
}
}
XML
<bean id="harry" class="com.springinaction.springidol.Magician">
<property name="magicBox" ref="magicBox" />
<property name="magicWords" value="Bippity boppity boo" />
</bean>
<bean id="magicBox" class="com.springinaction.springidol.MagicBoxImpl" />
<bean id="magicBox" class="com.springinaction.springidol.MagicBoxImpl">
<replaced-method name="getContents" replacer="tigerReplacer" />
</bean>
<bean id="tigerReplacer"
class="com.springinaction.springidol.TigerReplacer" />
TigerReplacer implements Spring’s MethodReplacer interface. MethodReplacer only requires that the reimplement() method be implemented. reimplement() takes three arguments: the target object that the method will be replaced on, the method that will be replaced, and the arguments passed to the method. In our case, we won’t be using those parameters, but they’re available if you need them. The body of the reimplement() method effectively becomes the new implementation for the magic box’s getContents() method. For our example, the only thing we want to do is return “A ferocious tiger.” Effectively, the contents of the box are replaced with a tiger, Now when we invoke the magician’s perform() method, the following is printed to the console:
Bippity boppity boo
The magic box contains...
A ferocious tiger
Ta-da! The beautiful assistant has been replaced by a ferocious tiger—without changing the actual MagicBoxImpl code. The magic trick has been successfully performed with the help of Spring’s .It’s worth noting here that although MagicBoxImpl has a concrete implementation of the getContents() method, it would’ve also been possible for getContents() to have been written as an abstract method. In fact, method injection is a trick that is useful when the actual implementation of the method isn’t known until deployment time. At that time, the actual method replacer class can be provided in a JAR file placed in the application’s classpath.
Reference:Spring in action
分享到:
相关推荐
开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6.RELEASE-dist开发工具 框架JAR spring-framework-4.3.6....
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6.RELEASE开发工具 spring-core-4.3.6....
赠送jar包:spring-data-commons-2.0.6.RELEASE.jar; 赠送原API文档:spring-data-commons-2.0.6.RELEASE-javadoc.jar; 赠送源代码:spring-data-commons-2.0.6.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:spring-session-data-redis-2.0.4.RELEASE.jar; 赠送原API文档:spring-session-data-redis-2.0.4.RELEASE-javadoc.jar; 赠送源代码:spring-session-data-redis-2.0.4.RELEASE-sources.jar; 赠送...
赠送jar包:spring-security-crypto-5.5.2.jar; 赠送原API文档:spring-security-crypto-5.5.2-javadoc.jar; 赠送源代码:spring-security-crypto-5.5.2-sources.jar; 赠送Maven依赖信息文件:spring-security-...
赠送jar包:spring-security-oauth2-2.3.5.RELEASE.jar; 赠送原API文档:spring-security-oauth2-2.3.5.RELEASE-javadoc.jar; 赠送源代码:spring-security-oauth2-2.3.5.RELEASE-sources.jar; 赠送Maven依赖信息...
赠送jar包:spring-security-core-5.3.9.RELEASE.jar; 赠送原API文档:spring-security-core-5.3.9.RELEASE-javadoc.jar; 赠送源代码:spring-security-core-5.3.9.RELEASE-sources.jar; 赠送Maven依赖信息文件:...
spring-framework-5.3.0-SNAPSHOT-dist, spring-aop, spring-aspects, spring-beans, spring-context, spring-context-indexer, spring-context-support, spring-core, spring-expression, spring-instrument, ...
赠送jar包:spring-plugin-core-2.0.0.RELEASE.jar; 赠送原API文档:spring-plugin-core-2.0.0.RELEASE-javadoc.jar; 赠送源代码:spring-plugin-core-2.0.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring...
spring源码缺失jar。spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar
spring源码编译之后,各个项目可能缺失jar包 spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.5.1.jar
开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6....
赠送jar包:spring-boot-actuator-autoconfigure-2.3.12.RELEASE.jar; 赠送原API文档:spring-boot-actuator-autoconfigure-2.3.12.RELEASE-javadoc.jar; 赠送源代码:spring-boot-actuator-autoconfigure-2.3.12....
spring源码导入SpringObjenesis类报错解决,缺少jar包spring-cglib-repack-3.2.0.jar和spring-objenesis-repack-2.1.jar
开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE开发工具 spring-web-4.3.6.RELEASE...
开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring-context-4.3.6.RELEASE开发工具 spring...
赠送jar包:spring-cloud-gateway-server-3.1.1.jar; 赠送原API文档:spring-cloud-gateway-server-3.1.1-javadoc.jar; 赠送源代码:spring-cloud-gateway-server-3.1.1-sources.jar; 赠送Maven依赖信息文件:...
spring-cglib-repack-3.2.6.jar和spring-objenesis-repack-2.6.jar 需要的拿走吧