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


相关推荐

  • c#的传输组件dotnetty

    c#的传输组件dotnetty牛皮不多了,继续吹起。。。。最近一直看大家写的东西,了解的内容不少,我的牛皮也差不多吹完了。。。。最后在说说最近测试的dotnetty.去年弄下来试了,不行,最近又弄下来了看看,可以了。哇哈哈哈哈哈哈。我用过java的netty,就不多说它的使用和原理了,网上有书,有无数的博文,随便找。当然dotnetty比较少,毕竟才出来不久,而且还是.netcore的。这个是翻译的使用原理可以看…

    2025年7月4日
    0
  • java多线程编程面试题_linux多线程面试题

    java多线程编程面试题_linux多线程面试题一、多线程的几种实现方式,什么是线程安全。四种:继承Thread类,实现Runnable接口,实现Callable接口,使用线程池。线程安全:当多个线程访问某个类时,这个类始终都能表现出正确的行为,那么就称这个类是线程安全的。(Java并发编程实战)最核心的概念是正确性。正确性:某个类的行为与其规范完全一致。二、volatile的原理,作用,能代替锁么。volatile的理解三、画一个…

    2022年8月27日
    2
  • html中div加滚动条

    html中div加滚动条div加滚动条的两种方法:一、<divstyle=”overflow:scroll;width:400px;height:400px;”></div>记住宽和高一定要设置噢,否则不成的不过在不超出时,会有下面的滚动条,所以不是最好的选择二、<divstyle=”overflow-y:auto;overflow-x:auto;…

    2022年7月12日
    41
  • lamp环境下phpwind,wordpress,discuz论坛的搭建全过程

    lamp环境下phpwind,wordpress,discuz论坛的搭建全过程phpwind,wordpress,discuz3大论坛群英聚会目前世界最流行的企业建站方式是LAMP(Linux+Apache+MySQL+PHP),即使用Linux作为操作系统,Apache作为Web服务器,MySQL作为数据库,PHP作为服务器端脚本解释器。这四个软件都是遵循GPL的开放源码软件,它们安全、稳定、快速、功能强大…

    2022年9月18日
    0
  • RGB和YUV之比较【转】

    RGB和YUV之比较【转】

    2022年3月1日
    38
  • ORACLE RAC--裸设备的创建配置

    ORACLE RAC--裸设备的创建配置

    2022年3月11日
    87

发表回复

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

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