Spring bean加载多个配置文件
2024-10-11 01:35:17
除了写很简单的加载一个xml,加载多个的情况一直没用到,在公司里也不会由自己处理这个问题,现在需要用到了,就研究验证一下。
使用的案例还是上面的例子。
只有,将原来的beans.xml分成两个部分。
1.结构
2.beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--
加载一个xml,这个作为总的xml入口
-->
<import resource="beans2.xml"/> <bean id="helloWorldService" class="com.it.bean.HelloWorldService">
<property name="helloWorld" ref="springHelloWorld"/>
</bean> </beans>
3.beans2.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="springHelloWorld" class="com.it.service.SpringHelloWorld"></bean> </beans>
4.运行类
package com.it.main; import com.it.service.HelloWorld;
import com.it.bean.HelloWorldService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloMain {
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); HelloWorld hw= service.getHelloWorld(); hw.sayHello();
}
}
最新文章
- ATM-PROGRAM 关于Proprties的问题
- ArcGIS Engine开发之地图浏览
- linux工作用到的
- HTML5触屏版多线程渲染模板技术分享
- 不挣扎了,开始学习LINQ TO XML,进而来解析网页。
- 如何判断C#的Finalizer线程有没有被阻塞
- android发送/接收Json包含中文的处理
- asp.net 服务器端缓存与客户端缓存 [转]
- ORACLE 日志 logminer 使用
- php对xml的处理
- win7 64位 Xsheel
- 【linux kernel】 中断处理-中断上半部
- 优秀的前端project如何制定一个老师--html学习路径
- Linux Centos7配置防火墙开启端口
- Python学习【第26篇】:Python系列- 多线程(threading)
- 【Django基本命令002】
- struts2之数据校验
- CentOS 6下升级Python版本
- [W3bsafe]分享一个爬SQL注入漏洞的工具
- Web开发中button与submit区别
热门文章
- Docker入门02——Dockerfile详解
- HashMap原理分析(JDK1.7.x之前)
- shiro授权及自定义realm授权(七)
- win10 + ubuntu双系统详细安装过程
- scp -r拷贝目录不会拷贝软连接
- Redis消息通知(任务队列和发布订阅模式)
- win32编程:L,_T() ,TEXT和_TEXT
- shell脚本中冒号
- zabbix3.0.4关于java服务端程序内存溢出的处理
- freeswitch用户整合(使用mysql数据库的用户表)