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)
上一篇 2021年7月16日 下午7:00
下一篇 2021年7月16日 下午8:00


相关推荐

  • android 自定义progressdialog,自定义ProgressDialog

    android 自定义progressdialog,自定义ProgressDialog最近工作中需要用到progressDialog,可是系统自带的黑色progressDialog又是其丑无比,无奈只能自己自定义了,在网上查看别人的例子,并自己整理了一份Demo:先上图:MyProgressDialog:packagecom.example.myprogressdialog_zzw;importandroid.app.Dialog;importandroid.content….

    2022年7月14日
    25
  • CSS三角形

    CSS三角形一 css 三角形的原理将一个 div 的宽度和高度设置为 0 然后设置边框样式 triangle width 0 height 0 border top 100pxsolidre border right 100pxsolidbl border bottom 100pxsolidor border left 100pxsolidgr 得到一个由四个三角形组合形成的正方形将对应位置的边框颜色设

    2026年3月19日
    2
  • OpenClaw 接入硅基流动 API 全流程

    OpenClaw 接入硅基流动 API 全流程

    2026年3月13日
    6
  • 使用Ubuntu搭建Web服务器

    使用Ubuntu搭建Web服务器Docker是一个开源的应用容器引擎,基于Go语言并遵从Apache2.0协议开源。Docker可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似iPhone的app),更重要的是容器性能开销极低。总而言之,Docker相当于在你的电脑上建了一个虚拟机…

    2022年5月28日
    32
  • java web文件上传——FileUpload

    java web文件上传——FileUpload文件上传分析1.普通表单提交默认enctype="application/x-www-form-urlencoded";但是当表单中存在文件类型时,需要设置enctype="multipart/form-data",它不对字符进行编码,用于发送二进制的文件(即所有文件类型,如视频、图片、音乐、文档都可以用此类型entype);还有一种enctype="text/plain"用于发送纯文本内容。…

    2022年5月14日
    86
  • 孟小峰:大数据管理与数据思维

    孟小峰:大数据管理与数据思维孟小峰:大数据管理与数据思维大数据的规模效应给数据存储、管理以及数据分析带来了极大的挑战,数据管理方式上的变革正在酝酿和发生。本文对大数据的基本概念进行剖析,归纳大数据时代所面临的新挑战,并对大数据的主要应用做简单对比。在此基础上,阐述大数据处理的基本框架,并就云计算技术对于大数据时代数据管理所产生的作用进阅读全文和小伙伴们一起来吐槽

    2022年6月7日
    50

发表回复

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

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