博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring基本使用(元素lookup-method使用)
阅读量:4109 次
发布时间:2019-05-25

本文共 4753 字,大约阅读时间需要 15 分钟。

0.0
  • 可以使用 元素 lookup-method 来取代 ApplicationContextAware 接口,避免应用代码直接和和 Spring 代码耦合在一起
  • 注意:元素 lookup-method 会 override 覆盖指定名称的所有同名方法,如果想要覆盖指定的 overload 重载方法,可是使用元素 replaced-method 替代。
1. 使用 ApplicationContextAware 接口实现
  • 假设对单例 bean A 中某个方法 method-a 的每次调用,都需要获取一个新的 bean B 实例。这种情景中,可以让 bean A 实现 ApplicationContextAware 接口,从而可以在 bean A 中访问 bean 工厂,再从 bean 工厂每次获取一个新的 bean B 的实例。代码示例如下:
  1. 定义一个接口
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;public interface FFruit {
void say();}
  1. 接口 FFruit 的一个具体实现类(用作上述中的 bean B)
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;public class FApple implements FFruit {
public void say() {
System.out.println(String.format("fapple, 苹果[%d]", new Object[] {
this.hashCode()})); }}
  1. 定义另一个类(用作上述中的 bean A),该类实现了 ApplicationContextAware 接口
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class PersonA_use_lookup_method implements ApplicationContextAware{
private ApplicationContext context; public void eatFruit() {
FFruit fruit = getAFFruit(); if (fruit != null) {
fruit.say(); }else {
System.out.println("没有水果吃"); } } public FFruit getAFFruit() {
// 每次从 bean工厂获取一个 apple return (FFruit) context.getBean("apple"); } // 保存 applicationContext 引用 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext; }}
  1. spring 配置文件如下(定义了一个单例 bean 即 persona 作为上述场景中的 bean A,和一个非单例 bean 即 apple 作为上述场景中的 bean B)
  1. 测试代码如下
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 元素 lookup-method 使用 * @author jokee * */public class Test_Test_1 {
@Test public void test_use_lookup_method() throws Exception {
String pathA = "com/willhonor/test/configs/application.d.xml"; String[] path = new String[] {
pathA}; ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path); // PersonA_use_lookup_method persona = (PersonA_use_lookup_method) context.getBean("persona"); persona.eatFruit(); persona.eatFruit(); persona.eatFruit(); persona.eatFruit(); }}
  1. 执行结果如下(可见单例 bean persona 连续执行了 4 次,每次都获取一个新的 apple)
...#此处省略 spring 日志打印...fapple, 苹果[226170135]fapple, 苹果[381707837]fapple, 苹果[589446616]fapple, 苹果[1321640594]
2. 使用元素 lookup-method 实现
  • 使用 lookup-method 来实现前面示例的效果,仅仅修改 bean A 以及 spring 配置文件即可,其它部分保持不变。
  1. bean A 修改如下(不再需要实现 ApplicationContextAware 接口)
package com.willhonor.test.useApplicationContext3LookupOrReplaceMethods;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;public class PersonA_use_lookup_method{
public void eatFruit() {
FFruit fruit = getAFFruit(); if (fruit != null) {
fruit.say(); }else {
System.out.println("没有水果吃"); } } /** 方法返回 null,别慌,IOC 容器会帮我们覆盖这个方法 */ public FFruit getAFFruit() {
return null; }}
  1. spring 配置文件修改如下:
  1. 再次执行测试,执行结果达到预期
...#此处省略 spring 日志打印...fapple, 苹果[710714889]fapple, 苹果[551734240]fapple, 苹果[1757293506]fapple, 苹果[687780858]
3. 其它
  1. 我使用的 Maven 文件配置如下(虽然我测试使用的 spring 版本是 1.2.6,这是 2005 年发布的版本,但是对比 1.2.6 版本的 spring-beans.dtd 和最新的 spring 5 的 spring-beans-2.0.dtd,会发现这两者几乎保持一致,可见 spring 的向后兼容做的很好。)
4.0.0
com.willhonor
todos
0.0.1-SNAPSHOT
spring-1.2.6
org.springframework
spring-context
1.2.6
org.springframework
spring-webmvc
1.2.6
org.springframework
spring-jdbc
1.2.6
org.springframework
spring-aop
1.2.6
javax.servlet
servlet-api
2.3
org.mariadb.jdbc
mariadb-java-client
2.3.0
junit
junit
4.12
cglib
cglib
2.1_3

转载地址:http://ajlsi.baihongyu.com/

你可能感兴趣的文章
matplotlib.pyplot.plot()参数详解
查看>>
拉格朗日对偶问题详解
查看>>
MFC矩阵运算
查看>>
ubuntu 安装mysql
查看>>
c# 计算器
查看>>
C# 简单的矩阵运算
查看>>
gcc 常用选项详解
查看>>
c++输出文件流ofstream用法详解
查看>>
firewalld的基本使用
查看>>
Linux下SVN客户端使用教程
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
OpenFeign学习(七):Spring Cloud OpenFeign的使用
查看>>