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工厂获取一个 applereturn (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)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "spring-beans.dtd">
<beans><bean id="persona" singleton="true"class="com.willhonor.test.useApplicationContext3LookupOrReplaceMethods.PersonA_use_lookup_method"></bean><!-- 非单例,以便每次都能获取一个新的 apple --><bean id="apple" singleton="false"class="com.willhonor.test.useApplicationContext3LookupOrReplaceMethods.FApple"></bean>
</beans>
  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 {@Testpublic 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 配置文件修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "spring-beans.dtd">
<beans><bean id="persona" singleton="true"class="com.willhonor.test.useApplicationContext3LookupOrReplaceMethods.PersonA_use_lookup_method"><!-- 告诉容器覆盖 getFFruit 方法,并期望新的方法返回 bean apple 的实例, --><!-- 因为 bean apple 定义为非单例,所以新的方法每次返回一个新的 apple --><lookup-method name="getAFFruit" bean="apple"/></bean><bean id="apple" singleton="false"class="com.willhonor.test.useApplicationContext3LookupOrReplaceMethods.FApple"></bean>
</beans>
  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 的向后兼容做的很好。)
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.willhonor</groupId><artifactId>todos</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>spring-1.2.6</artifactId><dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-core --><!-- <dependency> --><!-- <groupId>org.springframework</groupId> --><!-- <artifactId>spring-core</artifactId> --><!-- <version>1.2.6</version> --><!-- </dependency> --><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>1.2.6</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>1.2.6</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>1.2.6</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>1.2.6</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.3</version><!-- <scope>provided</scope> --></dependency><!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client --><dependency><groupId>org.mariadb.jdbc</groupId><artifactId>mariadb-java-client</artifactId><version>2.3.0</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><!-- <scope>test</scope> --></dependency><!-- https://mvnrepository.com/artifact/cglib/cglib --><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.1_3</version></dependency></dependencies>
</project>

Spring基本使用(元素lookup-method使用)相关推荐

  1. Spring查找方法注入(Lookup method injection)的底层实现原理

    2019独角兽企业重金招聘Python工程师标准>>> 美女邀我去歌舞厅娱乐,我拒绝了,我觉得跟技术宅男们分享技术更为重要. Spring方法注入的概念:一个由容器管理的single ...

  2. Error creating bean with name ‘multipartResolver‘: Lookup method resolution failed; 上传文件异常

    浏览器错误信息: 错误信息: HTTP状态 500 - 内部服务器错误 类型 异常报告消息 Servlet[springmvctest]的Servlet.init()引发异常描述 服务器遇到一个意外的 ...

  3. Spring Security 入门(1-3-2)Spring Security - http元素 - intercept-url配置

    http元素下可以配置登录页面,也可以配置 url 拦截. 1.直接配置拦截url和对应的访问权限 <security:http use-expressions="false" ...

  4. Spring 通过工厂方法(Factory Method)来配置bean

    在spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  5. Spring —— context:property-placeholder/元素

    外在化应用参数的配置 在开发企业应用期间,或者在将企业应用部署到生产环境时,应用依赖的很多参数信息往往需要调整,比如LDAP连接.RDBMS JDBC连接信息.对这类信息进行外在化管理显得格外重要.P ...

  6. java为什么引入注解_说说Spring中为何要引入Lookup注解

    前言 我们先探一探官方文档关于Method Injection的章节是怎么说的: In most application scenarios, most beans in the container ...

  7. Spring学习详解(1)——Spring入门详解

    2019独角兽企业重金招聘Python工程师标准>>> 一:spring的基本用法: 1,关于spring容器: spring容器是Spring的核心,该 容器负责管理spring中 ...

  8. Spring Java-based容器配置

    多年以来,Spring大量的XML配置及复杂的依赖管理饱受非议. 为了实现免XML的开发体验.Spring加入了新的配置注解以支持Java Config开发模式,当中最重要的注解就是@Configur ...

  9. Spring源码之启动过程(四)—— Bean的实例化详解

    前面,我们把Bean的生命周期做了一个概述和梳理,为的是更深刻的理解容器启动及Bean的生命周期,最主要的是Bean的实例化过程,没有看过的,可以进去先看一下(文章链接:Spring源码之Bean的生 ...

  10. 标签系列二:spring 中bean解释以及bean标签里面的属性

    一.bean标签: 英文解释:Defines a single (usually named) bean. A bean definition may contain nested tags for ...

最新文章

  1. Js+Dhtml:WEB程序员简易开发工具包(预先体验版)
  2. SAP PM 初级系列3 - 主数据相关的基础设置
  3. 三线压力传感器原理_进气压力传感器原理与检修
  4. python与或非位运算_python位运算
  5. java sql server 存储过程_修改SQL SERVER内置存储过程
  6. PC登录Citrix WI时报CPS license acquisition error(500)错误 截图及解决
  7. MiniDao1.8.3 版本发布,轻量级Java持久化框架
  8. Linux 安装Eclipse
  9. 微信小程序用RSA加密和java后台对接。
  10. LightOJ-1054 Efficient Pseudo Code
  11. Access denied for user ‘root‘@‘localhost‘ (using password: YES)
  12. 视频教程-OllyDbg(OD)使用教程-其他
  13. 魅族mx4服务器无响应,魅族MX4刷机失败解决方法
  14. win10磁盘使用率100%的解决方法
  15. 华为服务器麒麟系统,通用服务器麒麟os
  16. ComboBox 智能过滤,模糊匹配,拼音首字母匹配
  17. 带你读论文系列之计算机视觉--Inception V4
  18. Win32开发笔记(一):整体流程
  19. Arduino 读取 Pin2 的电平信号,并把结果打印到串口,也同时反映到 LED 灯
  20. 重磅!腾讯优图20篇论文入选CVPR 2021

热门文章

  1. Python Web学习笔记之TCP/IP协议原理与介绍
  2. 麓言信息超实用的平面设计构图原则
  3. 【转】突破区块链不可能三角:异步共识组 [Monoxide]
  4. 电脑企业微信怎么发送本地文件给微信好友?
  5. iframe内嵌文件去除打印,下载功能
  6. 系统学习Python——单元测试unittest:测试报告
  7. 详解 TCP 连接的“三次握手”与“四次挥手”
  8. 手机闪存速度排行_小米9闪存规格是多少
  9. 微信上线“密信”新功能:聊天可以加密了?
  10. Hadoop离线_网站流量日志数据分析系统_概述