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
分享到:
相关推荐
ReflectionUtils.replaceMethod(classA, "getClassB", proxy); return classA; } } ``` 在这个Java配置中,我们使用了Java代理和反射来实现Lookup方法注入。`LookupMethodInvocationHandler`是一个自定义的...
"详解获取Spring MVC中所有RequestMapping以及对应方法和参数" Spring MVC是一个基于模型-视图-控制器(MVC)模式的Web应用程序框架, RequestMapping是一个重要的注解,它可以将HTTP请求映射到控制器的方法上。在...
${user}" method="post"> *{name}" /> <!-- 更多字段 --> 提交 ``` 四、Thymeleaf模板布局 Thymeleaf支持模板布局,通过`th:replace`和`th:fragment`实现页面复用和组件化。创建一个片段并在其他页面中引入: ``...
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomPermission { String value(); } // 在控制器中使用自定义权限注解 @RestController @RequestMapping("/api") ...
IntelliJ IDEA是一款广受欢迎的Java集成开发环境(IDE),由JetBrains公司开发。...同时,IntelliJ IDEA还提供了许多高级功能,如Live Templates、 Structural Search and Replace等,等待你去探索和发掘。
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.http.ResponseEntity; @RequestMapping(value = "/...
1. **创建 Spring Boot 项目** `agent-demo-web` - 在 `main` 方法中,创建一个线程每 500 毫秒调用一次 `Test` 类中的 `test` 方法。 - 需要在项目的 `resources` 目录下放置 `Test.class` 文件。 - 为了使 Java...
- `replace()`, `replaceAll()`: 替换字符串中的特定字符或模式。 - `split()`: 使用正则表达式分割字符串成数组。 3. **集合框架操作**: - `ArrayList`, `LinkedList`:两种常见的列表实现,前者适合随机访问...
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @RequestMapping(value = "/getTreeData", method = ...
富士康作为全球知名的电子产品制造商,其在招聘过程中也会对技术人才进行严格的...同时,对于Java开发者来说,不断学习和理解最新的Java技术和框架,如Spring Boot、Spring Cloud、MyBatis等,也是保持竞争力的关键。
在准备华为Java笔试和面试...以上只是部分核心Java知识点,实际面试题可能会更深入,涉及JVM原理、网络编程、数据库连接、Spring框架等。确保对这些内容有扎实的理解,并能灵活运用,是应对华为Java笔试和面试的关键。
15. **Spring框架**:Spring是Java企业级应用的基石,理解IoC(控制反转)和AOP(面向切面编程)概念,以及Spring Boot、Spring MVC、Spring Data JPA等子模块的应用。 以上内容仅涵盖了Java面试中的一些主要知识点...
@RequestMapping(value = "cutimage.dhtml", method = {RequestMethod.POST}) @ResponseBody public String cutImage(@RequestParam("src") String base64Data) { try { // 解析Base64编码数据 byte[] data = ...
在实际生产环境中,可能需要考虑更多的因素,比如错误处理、文件大小限制、文件类型检查以及使用更高级的库如Apache Commons FileUpload或Spring MVC的MultipartFile来简化处理过程。同时,为了安全起见,保存路径不...
<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> *1024*1024" /> ``` 步骤 3:上传文件 在控制器中,我们可以使用 MultipartFile 对象来接收上传...
<form action="upload" method="POST" enctype="multipart/form-data"> *"> 上传"> ``` 2. **Servlet处理上传** 在服务器端,我们需要一个Servlet来处理这个POST请求。首先,确保在`web.xml`或使用Java配置...
JAVA模版引擎Freemarker常用标签(一) 1. if指令 这是一个典型的分支控制指令,该指令的作用完全类似于Java语言中的if,if指令的语法格式如下: <#if condition>... <#elseif condition>... <#elseif condition>......
return item.substring(item.indexOf('=') + 1).trim().replace("\"", ""); } } return null; } } ``` 这段代码中,我们遍历了请求的每一个`Part`,并提取出文件名,然后将其写入服务器指定的位置。注意,为了...
HashMap)、多线程(同步机制、线程池)、反射(Class、Constructor、Method 的使用)、对象拷贝(浅拷贝与深拷贝)、Java Web(Servlet、JSP、过滤器、监听器)、异常处理、网络编程、设计模式(工厂模式、单例模式...
Files.move(outputFilePath, Paths.get("/path/to/your/directory/image.jpg"), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } ``` 四、解决Base64编码过大问题 ...