springMVC通过getBean()获取context.xml文件中定义的service接口类
来源:互联网 发布:淘宝网针织连衣裙 编辑:程序博客网 时间:2024/05/29 20:01
以我做的一个项目为例;\
第一步
将springMVC框架搭建好后,在web工程下建一个test测试包,再建一个junit测试类
public class PreQueryProductControllerTest {private ApplicationContext applicationContext;@Beforepublic void setUp() throws Exception {applicationContext = new FileSystemXmlApplicationContext("classpath:spring-context.xml");}@Testpublic void testCallQuery() {PrdReq prdReq = new PrdReq();CreditPrdService creditPrdService = (CreditPrdService)applicationContext.getBean("creditPrdService");creditPrdService.call(prdReq);//fail("Not yet implemented");System.out.println("success");}}
根据程序运行步骤
applicationContext = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
中可以看出,目的是要加载spring-context.xml文件的内容
第二步在spring-context.xml加入以下信息,
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true"> <description>Spring 容器 配置</description><!-- 占位符 --><bean id="env" class="com.allinfinance.ares.facility.config.AresEnvironmentFactoryBean"><property name="locations" value="#{systemProperties['env.files'] ?: 'classpath:/conf/*.properties'}"/></bean><import resource="classpath*:spring-cis-dao.xml"/><import resource="classpath*:spring-cis-biz.xml"/><import resource="classpath*:spring-cis-credit.xml"/><import resource="classpath*:dubbox-credit-service.xml"/></beans>
在spring-context.xml文件中,共import了四个xml文件,这四个文件分别在四个子工程里面,因此,进入下一步
第三步
根据第一步的代码我们现以context.xml中引入的spring-cis-credit.xml为例.在cisCredit子工程下,建一个spring-cis-credit.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="true"> <!-- 开启自动注入 --> <context:annotation-config /> <context:component-scan base-package="com.allinfinance.cis.credit.service"/> <context:component-scan base-package="com.allinfinance.cis.credit.api.service"/> <bean id="SpringContextUtil" class="com.allinfinance.cis.credit.util.SpringContextUtil" lazy-init="false"/></beans
上面的文件目的是注入包,也就是注入service和api.service这两个包中的所有类.再返回第一步的java代码,进入到这一步
CreditPrdService creditPrdService = (CreditPrdService)applicationContext.getBean("creditPrdService");在service包中有CreditPreService这样一个接口类,我们为了调用其中的方法才做了以上的所有步骤,加载的所有类放在了applicationContext中,通过getBean("接口类名");即可注入所需要的接口,然后就可以调用其中的方法了
阅读全文
0 0
- springMVC通过getBean()获取context.xml文件中定义的service接口类
- springMVC通过getBean()获取context.xml文件中定义的service接口类
- 在springmvc中非controller类中调用service接口,以获取springmvc容器方法实现
- Spring 通过Autowired 和 Context.getBean()方式拿到的不是同一个Bean
- asp.net通过反射获取类中定义的枚举
- vs2008中定义dll,通过def文件导出接口
- 通过xml文件定义菜单
- 如何替换Application context定义文件中自定义的property
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- Android中通过xml资源文件定义数组
- springMVC的配置文件名在web.xml中如何定义
- 用django filebrowser上传中文文件遇到UnicodeEncodeError
- 邮箱验证 ——ACM
- 神无月排位赛(模拟 金马五校赛-上海大学)
- 二、Spring知识汇总
- 机器学习路线
- springMVC通过getBean()获取context.xml文件中定义的service接口类
- php流程控制的goto
- webpack配置文件详解(二)
- C++实现一个复数类
- MFC整型和字符串互相转换
- opencv
- 添加好友(快速幂 金马五校赛-上海大学)
- extern "c"{}
- Ubuntu遇到的错误总结