main方法中注入Spring bean
2024-08-31 16:08:28
在有些情况下需要使用main使用Spring bean,但是main方法启动并没有托管给Spring管理,会导致bean失败,报空指针异常。
可以使用 ClassPathXmlApplicationContext 加载配置文件,获获取bean:
public static void main(String[] args) {
@SuppressWarnings("resource")
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");//spring.xml文件为spring配置文件
context.registerShutdownHook();
context.start();
CityConfig cityConfig=(CityConfig)context.getBean("cityConfig");//获取CityConfig bean
System.out.println(cityConfig);
}
最新文章
- js中查找相同的几种函数
- js zTree的用法
- marathon参考(11):ports端口设置(转)
- NodeJS V8 GC概览
- QQ高仿版
- ML学习分享系列(2)_计算广告小窥[中]
- PostgreSQL基础知识分享
- mybatis映射文件#与$的使用,及参数传入规则
- ionic3 打包报错[ERROR] An error occurred while running cordova prepare (exit code 1):
- windows共享文件夹权限设置
- C++基础算法学习——N皇后问题
- 树莓派.Qt.Creator安装方法
- android 开发 对话框Dialog详解
- J2EE完全手册(一)
- P4811 C’s problem(c)
- Educational Codeforces Round 14 B. s-palindrome 水题
- VC设置代理方法
- 【Unity】7.6 自定义输入
- Druid连接池基本配置及监控配置
- pandas 语句
热门文章
- HDU 1724:Ellipse(自适应辛普森积分)
- 花5分钟时间来了解一下高性能网关Kong会有意外收获
- Maven打包成Jar文件时依赖包的问题
- Python选修第00次作业:缘分
- 看MySQL的参数调优及数据库锁实践有这一篇足够了
- vs2010编译zapline-zapline.systemoptimization 注释工程中的//#define abs(value) (value >;= 0 ? value : -(value))即可
- 一些学习js的算法题目
- TensorFlow笔记-线程和队列
- sessionID是如何在客户端和服务器端传递的?
- python面向对象的继承-组合-02