spring 上下文获取bean,service等类

来源:互联网 发布:时间序列算法 rsi 编辑:程序博客网 时间:2024/06/15 15:19

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,这里的配置文件位置一定要写正确,否则读取不到配置文件,当然就拿不到类了。

0 0
原创粉丝点击