在有些情况下需要使用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);
}

  

最新文章

  1. js中查找相同的几种函数
  2. js zTree的用法
  3. marathon参考(11):ports端口设置(转)
  4. NodeJS V8 GC概览
  5. QQ高仿版
  6. ML学习分享系列(2)_计算广告小窥[中]
  7. PostgreSQL基础知识分享
  8. mybatis映射文件#与$的使用,及参数传入规则
  9. ionic3 打包报错[ERROR] An error occurred while running cordova prepare (exit code 1):
  10. windows共享文件夹权限设置
  11. C++基础算法学习——N皇后问题
  12. 树莓派.Qt.Creator安装方法
  13. android 开发 对话框Dialog详解
  14. J2EE完全手册(一)
  15. P4811 C’s problem(c)
  16. Educational Codeforces Round 14 B. s-palindrome 水题
  17. VC设置代理方法
  18. 【Unity】7.6 自定义输入
  19. Druid连接池基本配置及监控配置
  20. pandas 语句

热门文章

  1. HDU 1724:Ellipse(自适应辛普森积分)
  2. 花5分钟时间来了解一下高性能网关Kong会有意外收获
  3. Maven打包成Jar文件时依赖包的问题
  4. Python选修第00次作业:缘分
  5. 看MySQL的参数调优及数据库锁实践有这一篇足够了
  6. vs2010编译zapline-zapline.systemoptimization 注释工程中的//#define abs(value) (value >= 0 ? value : -(value))即可
  7. 一些学习js的算法题目
  8. TensorFlow笔记-线程和队列
  9. sessionID是如何在客户端和服务器端传递的?
  10. python面向对象的继承-组合-02