您的位置:首页 > 编程语言 > Java开发

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,这里的配置文件位置一定要写正确,否则读取不到配置文件,当然就拿不到类了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: