java this context,java – Spring XML中applicationcontext的“this”引用
有没有办法在Spring中的bean配置文件中引用当前的应用程序上下文?
我想做这样的事情:
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
问题是SomeClass在其构造函数中需要一个ApplicationContext实例.有没有办法获得正在加载bean的ApplicationContext的引用?我知道我可以在XML中完成所有的加载,但这不是我想要的,因为我需要在我的java代码中进行bean加载.
解决方法:
你看过实施ApplicationContextAware吗?它不会在构造函数中出现,但它确实在init()调用之前发生,并且将在填充bean属性之后发生.
Invoked after population of normal bean properties but before an init
callback such as InitializingBean.afterPropertiesSet() or a custom
init-method. Invoked after
ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader),
ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
and MessageSourceAware, if applicable.
public class SomeClass implements ApplicationContextAware {
//your class definition
private ApplicationContext myContext;
public void setApplicationContext(ApplicationContext context) throws BeansException {
myContext = context;
//load beans here maybe?
}
}
如果使用Spring 2.5或更高版本,你也可以@Autowire(d)它.
public class SomeClass {
//your class definition
@Autowired
private ApplicationContext myContext;
}
当然,执行其中任何一项都会将代码绑定到Spring.
标签:java,spring
来源: https://codeday.me/bug/20190709/1417895.html
java this context,java – Spring XML中applicationcontext的“this”引用相关推荐
- java 从一个容器获取对象,Java 如何实现从spring容器中获取注入的bean对象
Java 如何实现从spring容器中获取注入的bean对象 发布时间:2020-11-03 15:27:37 来源:亿速云 阅读:70 作者:Leah 这期内容当中小编将会给大家带来有关Java 如 ...
- Spring框架中ApplicationContext与BeanFactory的区别
前言 Spring框架中ApplicationContext与BeanFactory的区别.其中,BeanFactory为ApplicationContext的父类,是一种继承关系,Applicati ...
- Spring配置中的bean直接引用其它bean的属性值
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! prin ...
- Java笔记-Log4j在Spring Boot中的使用
在Java企业级开发中都需要日志记录相关的操作. Maven相关配置如下,首先要去除Spring Boot自带的日志,然后添加log4j2的日志: <dependency><grou ...
- pom env.java home_关于java:aven:POM.xml中缺少工件com.sun:tools:jar:1.6.0编译时异常...
本问题已经有最佳答案,请猛点这里访问. 当我尝试增加对工具的依赖时,我遇到了一个奇怪的问题,并且在pom.xml中遇到了编译时异常. jar显示如下(缺少工件com.sun:tools:jar:1.6 ...
- java @around_正确理解Spring AOP中的Around advice
Spring AOP中,有Before advice和After advice,这两个advice从字面上就可以很容易理解,但是Around advice就有点麻烦了. 乍一看好像是Before ad ...
- java调用r实例,Spring Boot中使用RSocket的示例代码
1. 概述 RSocket 应用层协议支持Reactive Streams 语义, 例如:用RSocket作为HTTP的一种替代方案.在本教程中, 我们将看到RSocket 用在spring boot ...
- Spring Boot中使用模板引擎引用资源
目录 概念 演示 概念 在Spring Boot中可以通过模板引擎去引用资源,如果项目改变了,也是可以跑的,不需要自己手动一个个去改链接. 演示 运行截图如下: 但修改properties文件: se ...
- java controller 继承_java – Spring RestController中的继承
我有一个Spring RestController,它处理API调用的第1版. package rest.v1; @RestController @RequestMapping("v1/so ...
最新文章
- 汇编语言随笔(10)-内中断及实验12(返回到dos的中断处理程序)
- pypthon3精要(11)-try,except,else异常处理
- 尽管普通的sql语句代码可以实现数据插入的操作,但是更好的代码应该是参数的方式:...
- 09_分类算法--k近邻算法(KNN)、案例、欧氏距离、k-近邻算法API、KNeighborsClassifier、及其里面的案例(网络资料+学习资料整理笔记)
- SAP Spartacus My Company list focus事件触发后,控件border的默认效果
- 钉钉产品介绍_钉钉正式推出智能OA:免费开放、一站解决“人财物事”管理难题...
- 低效程序员的7个坏习惯
- 网易云易盾牵手百视通 助力广电领域新媒体内容安全
- 腾讯面试官这样问我二叉树,我刚好都会 | 原力计划
- 内网DNS地址自己定
- C++位运算:求 int 绝对值
- PARDISO 5.0.0 Solver Project
- onContextItemSelected 与 onMenuItemSelected 的那些事
- cmder中设置code page为utf8的方法
- Windows Server 2003 R2标准版 SP2 64位 (简体中文)官方原版ISO镜像
- 北京汽车加速海外业务发展
- regl vao 三角形交替
- 用D触发器设计三位同步二进制减法计数器逻辑状态测试表_普兰店QQT/A-HT2A1B1S0V1液位表 - 普兰仪器仪表...
- 十年前只想混一个Apache邮箱装逼,十年后却成了顶级项目创始人
- 计算机大一新生打字速度,大一新生,21修炼成打字高手
热门文章
- 为什么极度随机树比随机森林更随机?这个极度随机的特性有什么好处?在训练阶段、极度随机数比随机森林快还是慢?
- tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
- 一例IBM服务器Raid磁盘阵列故障
- 深度学习之梯度下降法
- android实现分类搜索功能,android应用中的搜索功能怎么实现的
- mysql约束日期_MySQL的约束、事务、字符串、日期、数学相关及其他补充
- STM32-超级终端显示日历
- python 获取程序运行时间
- 图像拼接--Construction and Refinement of Panoramic Mosaics with Global and Local Alignment
- 报错解决:RequestsDependencyWarning: urllib3 (1.24.3) or chardet (3.0.4) doesn't match a supported versio