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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • (others)ICMP报文详解系列「建议收藏」

    (others)ICMP报文详解系列「建议收藏」Linuxicmp学习笔记之一icmp协议相关的格式分类: linux网络2014-04-1723:45 487人阅读 评论(0) 收藏 举报Linuxicmp功能分析之一 icmp协议相关的格式 ICMP协议是网络层中一个非常重要的协议,其全称为Internet Control Message Protocol(因特网控制报文协议

    2022年5月24日
    32
  • c语言流水灯程序详细讲解,用c语言编写单片机流水灯程序详解[通俗易懂]

    c语言流水灯程序详细讲解,用c语言编写单片机流水灯程序详解[通俗易懂]用C语言编写的单片机流水灯程序一、硬件电路因为电路用单片机控制,所以电路非常简洁。其电路原理图见下图,印制板图如下图所示。?电路的核心部分是AT89C2051单片机,前面提到它有Pl和P3两组I/O口,我们这里只用到Pl口,共8个引脚。图中Cl、R9组成典型的上电复位(即在加电时单片机复位)电路,XTAL、C2、C3与AT89C2051片内振荡电路组成时钟振荡器。值得注意的是,C2、C3的容量不能…

    2022年5月1日
    109
  • 冯诺依曼体系结构「建议收藏」

    冯诺依曼体系结构「建议收藏」目录冯诺依曼体系结构简介数据流向存储分级举例说明数据的流动过程冯诺依曼体系结构简介我们常见的计算机,如笔记本。我们不常见的计算机,如服务器,大部分都遵守冯诺依曼体系。计算机本质上是有输入,并且经过计算机的计算,将结果显示到某种显示输出上,就可以称为计算机。输入单元:键盘,网卡,磁盘,话筒…输出单元:显示器,网卡,磁盘,音响…存储器没有特殊说明一般指的是物理内存。中央处理器(CPU):含有运算器和控制器等运算器在进行运算的时候无外乎两种情况,一种是算术运算,一种逻辑运算。控制器主要能够用来

    2025年8月12日
    3
  • Linux 内核版本号命名规则[通俗易懂]

    Linux 内核版本号命名规则[通俗易懂]Linux内核版本有两种:稳定版和开发版,Linux内核版本号由3个数字组成:r.x.yr:目前发布的内核主版本。x:偶数表示稳定版本;奇数表示开发中版本。y:错误修补的次数。内核版本号每位都代表什么?以版本号为例:2.6.9-5.ELsmp,r:2,主版本号x:6,次版本号,表示稳定版本y:9,修订版本号,表示修改的次数头两个数字合在一齐可…

    2025年9月6日
    7
  • 无名汉化组官网_什么是并查集

    无名汉化组官网_什么是并查集永无乡包含 n 座岛,编号从 1 到 n ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 n 座岛排名,名次用 1 到 n 来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛到达另一个岛。如果从岛 a 出发经过若干座(含 0 座)桥可以到达岛 b ,则称岛 a 和岛 b 是连通的。现在有两种操作:B x y 表示在岛 x 与岛 y 之间修建一座新桥。Q x k 表示询问当前与岛 x 连通的所有岛中第 k 重要的是哪座岛,即所有与岛 x 连通的岛中重要度排名第 k 小的岛是哪座,请你输出那

    2022年8月10日
    4
  • 測试加入多级文件夹篇[通俗易懂]

    測试加入多级文件夹篇

    2022年1月26日
    39

发表回复

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

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