springMVC通过getBean()获取context.xml文件中定义的service接口类

springMVC通过getBean()获取context.xml文件中定义的service接口类

以我做的一个项目为例;\

第一步

将springMVC框架搭建好后,在web工程下建一个test测试包,再建一个junit测试类

public class PreQueryProductControllerTest {
	private ApplicationContext applicationContext;
	@Before
	public void setUp() throws Exception {
		applicationContext = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
	}
	@Test
	public 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(“接口类名”);即可注入所需要的接口,然后就可以调用其中的方法了

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106349.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 精美的液晶数字字体素材[通俗易懂]

    精美的液晶数字字体素材[通俗易懂]液晶数字应该比较常见,那么液晶数字字体的应用也是相对广泛了,可以运用于一切需要液晶显示屏上的数字字体显示。对于这样一种有着广泛的应用数字字体,选择使用哪款液晶数字字体也是一个很重要的问题啦!为此,特意为大家收集了几款液晶数字字体供大家选择,喜欢的朋友赶紧收藏起来吧!  DS-Digital字体是一款比较常规的液晶数字字体,这款字体的仅支持数字和大写字母输入,字体端正,结构完整,整体视觉呈现效果…

    2025年7月27日
    4
  • ubuntu svn命令 tab补全(linux安装svn客户端)

    ubuntuSVN命令大全1、将文件checkout到本地目录svncheckout path(path是服务器上的目录)例如:svncheckoutsvn://192.168.1.1/pro/domain简写:svnco2、往版本库中添加新的文件svn add file例如:svn add test.php(添加te

    2022年4月11日
    57
  • webmin 安装后如何登录 用户名和密码

    webmin 安装后如何登录 用户名和密码

    2022年2月21日
    47
  • python控制运算的优先顺序(python控制电脑桌面软件)

    Python控制odrive紧接着上一篇文章,来分享一下我的应用过程。先说结论,python编程odrive其实只要写上你之前在odrivetool下的调试命令就行了目前我还只是一个菜鸡,对很多技术都不熟悉或者只知道表面。我也是边做变成长,有哪些不到位的地方可以友善交流。最初的疑惑python也是我最近才开始学习的语言,它可以是一门交互式的语言,如果你知道这一点,那么就知道odrivetool其实就是这样子,你输入命令行然后执行。所以,如果你想控制odrive,只要在python代码中写上诸如o

    2022年4月17日
    60
  • 如何获取相应tableview中的touchesBegan事件[通俗易懂]

    如何获取相应tableview中的touchesBegan事件[通俗易懂]我

    2022年7月25日
    8
  • Using LockWorkstation in Windows XP

    Using LockWorkstation in Windows XPUsingLockWorkstationinWindowsXPBYDelphiscn(cnBlaster#hotmail.com)http://blog.csdn.net/delphiscn问题提交:花落孤独相关连接:http://blog.csdn.net/delphiscn/archive/2005/06/25/403157.aspx提交日期:2005-12-063:53…

    2022年7月21日
    14

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号