zoukankan      html  css  js  c++  java
  • Spring bean加载多个配置文件

      除了写很简单的加载一个xml,加载多个的情况一直没用到,在公司里也不会由自己处理这个问题,现在需要用到了,就研究验证一下。

      使用的案例还是上面的例子。

      只有,将原来的beans.xml分成两个部分。

      

    1.结构

      

    2.beans.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
     5 
     6     <!--
     7         加载一个xml,这个作为总的xml入口
     8     -->
     9     <import resource="beans2.xml"/>
    10 
    11     <bean id="helloWorldService" class="com.it.bean.HelloWorldService">
    12         <property name="helloWorld" ref="springHelloWorld"/>
    13     </bean>
    14 
    15 
    16 </beans>

    3.beans2.xml

    1 <?xml version="1.0" encoding="UTF-8"?>
    2 <beans xmlns="http://www.springframework.org/schema/beans"
    3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    5 
    6     <bean id="springHelloWorld" class="com.it.service.SpringHelloWorld"></bean>
    7     
    8 </beans>

    4.运行类

     1 package com.it.main;
     2 
     3 import com.it.service.HelloWorld;
     4 import com.it.bean.HelloWorldService;
     5 import org.springframework.context.ApplicationContext;
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 
     8 public class HelloMain {
     9     public static void main(String[] args) {
    10 
    11         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    12 
    13         HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
    14 
    15         HelloWorld hw= service.getHelloWorld();
    16 
    17         hw.sayHello();
    18     }
    19 }

      

  • 相关阅读:
    调试脚本
    if [ $? -eq 0 ]的含义
    主键和索引的区别
    Docker守护式容器
    Docker容器的基本操作
    Linux下Docker的安装
    Linux下查看占用CPU资源最多的几个进程
    报错:pymysql.err.InternalError: (1054, "Unknown column 'AType' in 'field list'")
    在webpack中使用postcss-px2rem的
    vue环境配置脚手架环境搭建vue工程目录
  • 原文地址:https://www.cnblogs.com/juncaoit/p/8660921.html
Copyright © 2011-2022 走看看