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


相关推荐

  • Mac上的抓包工具Charles

    Mac上的抓包工具Charles今天就来看一下Mac上如何进行抓包,之前有一篇文章介绍了使用Fidder进行抓包http://blog.csdn.net/jiangwei0910410003/article/details/19806999不过可惜的是,Fidder使用C#开发的,所以就不能在Mac上使用了,不过还有另外一个抓包神器,就是Charles,它是Java开发的,所以跨平台,不仅可以在Mac上使

    2022年5月9日
    53
  • Pycharm远程开发操作

    Pycharm远程开发操作Pycharm 远程开发操作可以在 Windows 中使用 Pycharm 编写代码 而代码的调试运行可以使用远程服务器中的 python 解释器 在本地创建好工程项目 或从 git 上 clone 下代码 后 用 Pycharm 打开 1 打开 Tools 设置 Deployment 2 选择 SFTP3 设置服务器名字 4 设置服务器信息 1 测试服务器连接是否可用 2 设置上传代码的目录映射 3 打开设置 设置远程 Python 解释器 4 选择已存在的服务器设置 5 选择 Create 复制服务器设置到解

    2026年3月27日
    2
  • iframe属性参数「建议收藏」

    iframe属性参数

    当点击一个子页面的链接时,如何将另一个子页面嵌入到当前iframe中
    只要给这个iframe命名就可以了。
    ,然后,网页上的超链接语句应该写为:
     
    将iframe解释成“浏览器中的浏览器”很是

    2022年4月8日
    274
  • error LNK2019: 无法解析的外部符号的几种情况探讨[通俗易懂]

    error LNK2019: 无法解析的外部符号的几种情况探讨[通俗易懂]errorLNK2019:无法解析的外部符号的几种情况探讨。

    2022年10月6日
    3
  • Linux服务器php环境搭建教程

    Linux服务器php环境搭建教程RT.以前服务器端是使用phpnow套件。今天尝试在ubuntu下独立完成apache2+php+mysql的服务器运行环境。具体的步骤如下(下面的操作都需要使用root权限):|.Apache2安装sudoapt-getinstallapache2安装的成功标志是你在浏览器中打开127.0.0.1的时候会显示“Itworks!”的字眼。apache2访

    2022年6月29日
    19
  • Linux下查看某个用户组下的所有用户

    Linux下查看某个用户组下的所有用户需要查看 jishu 分组下的所有用户 可以使用命令如下 grep jishu etc group 显示结果是 这样就可以将该分组下的所有用户显示出来了

    2026年3月26日
    1

发表回复

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

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