spring 上下文获取bean,service等类
2015-08-27 14:17
585 查看
spring工程里,在controller,service以外的普通类里获取service,bean等类,可以通过上下文环境 ApplicationContext 做到。
见证奇迹的时刻到了:
private static ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
private ISmsService smsService = applicationContext.getBean(ISmsService.class);
注意:classpath:spring-context.xml,这里的配置文件位置一定要写正确,否则读取不到配置文件,当然就拿不到类了。
见证奇迹的时刻到了:
private static ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
private ISmsService smsService = applicationContext.getBean(ISmsService.class);
注意:classpath:spring-context.xml,这里的配置文件位置一定要写正确,否则读取不到配置文件,当然就拿不到类了。
相关文章推荐
- SpringMVC访问静态资源的三种方式
- 多数据源 atomikos 打造Java分布式事务
- 工程师薪酬分析:Java 不是过去式
- java中string转码md5
- 【转】 Java中弹出对话框中的几种方式
- Struts2的拦截器工作原理
- Java JDK内置工具的使用说明
- spring(13) spring整合hibernate
- struts2设置文件上传大小
- java实现区域内屏幕截图示例
- Java XLSTransformer生成excel文件
- 算法研究之快速排序java版
- Java计算工作小时数的方法(修正版)
- 很好的一篇设置Eclipse的文章
- Spring任务调度实战之Timer
- Spring笔记③--spring的命名空间
- Spring任务调度实战之Quartz Simple Trigger
- Spring任务调度实战之Quartz Cron Trigger
- java中判断字符串是否为数字的三种方法
- 工程师薪酬分析:Java 不是过去式