配置文件内容过多修改起来维护起来很麻烦。Struts 2可以在总的配置文件中引入其他的配置文件。这是一种办法。第二种办法在加载配置文件的时候,里面可以一次性传多个。

<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!-- demo6Bean的集合属性的注入 ==============================================================--> <bean id="collectionBean" class="cn.itcast.spring3.demo6.CollectionBean"> <!-- 注入List集合 --> <property name="list">
<list>
<value>童童</value>
<value>小凤</value>
</list>
</property> <!-- 注入set集合 --> <property name="set">
<set>
<value>杜宏</value>
<value>如花</value>
</set>
</property> <!-- 注入map集合 --> <property name="map"> <map>
<entry key="刚刚" value="111"></entry>
<entry key="娇娇" value="333"></entry> </map> </property>
<property name="properties">
<props>
<prop key="username">root</prop> <!-- prop有key和value的形式,属性文件就是key和value的形式 --> <prop key="password">123</prop>
</props>
</property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?>
<!-- 别去schema,schema是文件,本地的文件,你得引那个头 --> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- demo1快速入门================================================= -->
<!-- 把接口和实现类在这个配置文件中配置,有了实现类的全路径之后到时候才能用工厂反射 --> <!-- 通过一个<bean>标签来设置类的信息,通过id属性为类起个标识. -->
<!-- 接口,实现类,配置文件也都有了 -->
<!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
<!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
也是这么做的,只不过工厂由Spring提供好了
-->
<bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
<!-- 使用<property>标签注入属性
value指的是普通值
ref指的是对象
-->
<property name="info" value="传智播客"></property>
</bean>
<!-- demo1快速入门 -->
<!-- demo2Bean的实例化 -->
<!-- 默认情况下使用的就是无参数的构造方法. -->
<!--
<bean id="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
-->
<!--
<bean name="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
-->
<!-- 第二种使用静态工厂实例化 不能写class了,因为现在不是由Spring直接帮你创建对象了-->
<!--
<bean id="bean2" class="cn.itcast.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>
-->
<!-- 第三种使用实例工厂实例化 -->
<!--
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
-->
<!-- 要先把Bean3Factory实例化 -->
<!--
<bean id="bean3Factory" class="cn.itcast.spring3.demo2.Bean3Factory"></bean>
-->
<!-- demo2Bean的实例化====================== end--> <!-- demo3Bean的作用范围======================= -->
<!--
<bean id="customer" class="cn.itcast.spring3.demo3.Customer" scope="prototype"></bean>
-->
<!--
<bean id="product" class="cn.itcast.spring3.demo3.Product" init-method="setup" destroy-method="teardown" scope="singleton">
<property name="name" value="空调">-->
<!-- 把Product类的属性name注入进来 -->
<!--
</property>
</bean>
--> <!-- demo4Bean的生命周期======================= -->
<!--
<bean id="customerService" class="cn.itcast.spring3.demo4.CustomerServiceImpl" init-method="setup" destroy-method="teardown"> <property name="name" value="itcast"></property>
</bean>
-->
<!-- 后处理Bean是由Spring容器自动调用不用你管,我们起个id是为了我们在程序中去获得它。但是这个类不用由我们获得, 由Spring自动调用。cn.itcast.spring3.demo4.MyBeanPostProcessor是后处理Bean-->
<!-- <bean class="cn.itcast.spring3.demo4.MyBeanPostProcessor"></bean>-->
<!-- demo5Bean的属性注入=====================================================================================================================================-->
<!-- 构造方法的注入 -->
<bean id="car" class="cn.itcast.spring3.demo5.Car">
<constructor-arg name="name" value="宝马"><!-- 通过这个标签为类注入属性 -->
</constructor-arg>
<constructor-arg name="price" value="1000000"><!-- 通过这个标签为类注入属性 -->
</constructor-arg>
<!--
<constructor-arg index="0" type="java.lang.String" value="奔驰">--><!-- 通过这个标签为类注入属性 -->
<!-- </constructor-arg>-->
<!--
<constructor-arg index="1" type="java.lang.Double" value="2000000">--><!-- 通过这个标签为类注入属性 --> <!-- </constructor-arg>-->
</bean>
<!--
<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
-->
<!-- <property>标签中name就是属性名称,value是普通属性的值,
ref:引用其他的对象
-->
<!--
<property name="name" value="保时捷奇瑞QQ"></property>
<property name="price" value="500000020000"></property>
</bean>
-->
<!-- p名称空间的写法
<bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="宝马" p:price="400000">
</bean>
-->
<!-- SpEL写法 -->
<bean id="car2" class="cn.itcast.spring3.demo5.Car2" >
<property name="name" value="#{'大众'}"></property>
<property name="price" value="#{'120000'}"></property>
</bean> <!--
<bean id="person" class="cn.itcast.spring3.demo5.Person">
<property name="name" value="任童"></property>
<property name="car2" ref="car2"></property>
</bean>
--> <!-- p名称空间的写法 -->
<!--
<bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="童童" p:car2-ref="car2">
</bean>
-->
<!-- SpEL写法 -->
<bean id="person" class="cn.itcast.spring3.demo5.Person">
<!--
<property name="name" value="#{'小边'}"></property>
-->
<!--
<property name="name" value="#{personInfo.name}"></property>
-->
<property name="name" value="#{personInfo.showName()}"></property>
<property name="car2" value="#{car2}"></property> </bean> <!--
<property name="name" value="任童"></property>
<property name="car2" ref="car2"></property>
-->
<bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">
<property name="name" value="张三"></property> </bean>
<!-- demo5Bean的属性的注入============================================================== -->
<!-- demo6Bean的集合属性的注入 ==============================================================-->
<!--
<bean id="collectionBean" class="cn.itcast.spring3.demo6.CollectionBean">
-->
<!-- 注入List集合 -->
<!--
<property name="list">
<list>
<value>童童</value>
<value>小凤</value>
</list>
</property>
-->
<!-- 注入set集合 -->
<!--
<property name="set">
<set>
<value>杜宏</value>
<value>如花</value>
</set>
</property>
-->
<!-- 注入map集合 -->
<!--
<property name="map"> <map>
<entry key="刚刚" value="111"></entry>
<entry key="娇娇" value="333"></entry> </map> </property>
<property name="properties">
<props>
<prop key="username">root</prop>
-->
<!-- prop有key和value的形式,属性文件就是key和value的形式 -->
<!--
<prop key="password">123</prop>
</props>
</property>
</bean>
--> <import resource="applicationContext2.xml"/> <!-- 配置文件引入 -->
</beans>
package cn.itcast.spring3.demo6;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest6 {
@Test
public void demo1(){//注入list
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
CollectionBean collectionBean = (CollectionBean) applicationContext.getBean("collectionBean"); System.out.println(collectionBean); }
}

day38 15-Spring的配置文件引入的问题的更多相关文章

  1. spring 配置文件 引入外部的property文件的两种方法

    spring  的配置文件 引入外部的property文件的两种方法 <!-- 引入jdbc配置文件    方法一 --> <bean id="propertyConfig ...

  2. Spring配置文件引入xml文件: <import resource=" " />标签使用总结

    引入其他模块XML 在Spring的配置文件,有时候为了分模块的更加清晰的进行相关实体类的配置. 比如现在有一个job-timer.xml的配置 <?xml version="1.0& ...

  3. spring配置文件引入properties文件:<context:property-placeholder>标签使用总结

    一.问题描述: 1.有些参数在某些阶段中是常量,比如: (1)在开发阶段我们连接数据库时的连接url.username.password.driverClass等 (2)分布式应用中client端访问 ...

  4. Spring Boot 配置文件密码加密两种方案

    Spring Boot 配置文件密码加密两种方案 jasypt 加解密 jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中.可以快速集成到 Spring Boot 项 ...

  5. Spring Boot 配置文件详解

    Spring Boot配置文件详解 Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们的作用都是修改Spring Boot自动配置的默认值.相对于prop ...

  6. Spring boot 配置文件详解 (properties 和yml )

    从其他框架来看 我们都有自己的配置文件, hibernate有hbm,mybatis 有properties, 同样, Spring boot 也有全局配置文件. Springboot使用一个全局的配 ...

  7. Springboot 系列(二)Spring Boot 配置文件

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 不管是通过官方提供的方式获取 Spring ...

  8. 史上最全的Spring Boot配置文件详解

    Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门和基础,经常会用到,所以写下做个总结以便日 ...

  9. spring boot 配置文件

    spring boot使用一个全局配置文件:主要是以下两种类型 application.properties  :例:server.port=9998 application.yml(YAML)  : ...

随机推荐

  1. iOS汇编系列-汇编入门

    概述 汇编语言(Assembly Language)用符号代替了0和1,比机器语言更便于阅读和记忆. 但是同样汇编语言同样指令太多不便于记忆,就出现了高级语言.C\C++\Java\Swift等,更接 ...

  2. 2014-VGG-《Very deep convolutional networks for large-scale image recognition》翻译

    2014-VGG-<Very deep convolutional networks for large-scale image recognition>翻译 原文:http://xues ...

  3. 巧用 position:absolute

    1.跟随性 下面这种方法更加简便以及更方便维护, 例如“西部世界”,由于不用将父元素设为position:relative,position:absolute的位置也就不用根据文字多少而重新进行top ...

  4. PAT甲级——A1053 Path of Equal Weight

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  5. 010-利用Selenium+python自动输入博客账号密码登录

    from selenium import webdriver import time def OpenUrl(): # 访问网址 driver.get(url) def Login(): # 查询登录 ...

  6. os.path.join 的用法

    Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...

  7. Kotlin 委托(2)变量委托是什么、自定义变量委托

    1.委托是什么? 1.1 官网示例 在每个变量委托的实现的背后,Kotlin 编译器都会生成辅助对象并委托给它. 假设委托如下, class C { var prop: Type by MyDeleg ...

  8. Python 原生2种 邮件发送(发送验证码) 的方法

    import smtplib from email.mime.text import MIMEText # 第三方 SMTP 服务 mail_host = "smtp.sina.cn&quo ...

  9. tyvj 1423 GF和猫咪的玩具

    传送门 解题思路 题目比较水,floyd求出最短路取个最小值即可.结果joyoi时限写错了..好像只有0ms才能过??突然发现加了快读就T不加就A,数据在10000以下的还是scanf快啊. 代码 # ...

  10. vmware的Linux虚拟机ping不通外网的解决办法

    转载自:https://blog.csdn.net/l_l_b_/article/details/79409843 1.点击此处编辑 2.选择虚拟网络编辑器 3.点击更改设置 4.选择v8 并将使用本 ...