刚开始学习spring 以及springmvc 、mybatis 框架时能把配置文件配置能起来就很高兴了,但是当时间长了我们发现如果所有的配置都写在一个配置文件中的话会很多,所以看着也痛苦,所以我们可以吧配置文件分开配置,这样方便之后查找,那分开配置的方式有几种方式那:
一、xml引入
1、将配置文件引入spring的配置文件
情况描述:简单的springmvc+spring+mybatis 框架我搭建起来了,那我想使用dubbo,还想再定义一个定时任务,你可以吧他们的这些配置配在spring的那个配置文件中但是钠盐的话太多了,也很乱怎么办。
(1)、我们新建两个配置文件dubbo的配置文件和定时任务的配置文件,这样了后我们我们在spring的配置文件中引入这两个配置文件,引入方式如下:
<import resource="classpath*:/spring/applicationContext-task.xml" />
<import resource="classpath*:/spring/applicationContext-dubbo.xml" />
上面两个是引入的配置文件,下面看看spring配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<import resource="classpath*:/spring/applicationContext-task.xml" />
taks 配置文件:
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd"
default-lazy-init="true">
<description>Spring task </description>
<!-- Enables the Spring Task @Scheduled programming model -->
<task:executor id="executor" pool-size="50" />
<task:scheduler id="scheduler" pool-size="100" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
<!-- 配置自定义任务 -->
<bean name="accessTokenTask" lazy-init="false" class="com.base.service.task.jd.AccessTokenTask"></bean>
</beans>
dubbo 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
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-2.5.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer-of-helloworld-app"></dubbo:application>
<!--zookeeper注册中心 -->
<dubbo:registry address="192.168.0.113:2181" protocol="zookeeper"></dubbo:registry>
<dubbo:protocol name="dubbo" port="20880" />
<!-- <dubbo:monitor address="127.0.0.1:7070" /> -->
<!-- 生成远程服务代理,可以和本地bean一样使用IUserService-->
<!--<dubbo:reference id="companyService" interface="com.base.userauthservice.ICompanyService"></dubbo:reference>-->
<!--<dubbo:reference id="financeService" interface="com.base.userauthservice.IFinanceService"></dubbo:reference>-->
<!--<dubbo:reference id="userService" interface="com.base.userauthservice.IUserService"></dubbo:reference>-->
</beans>
2、直接在web.xml 中引入相关的所有配置文件:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
这样所有的满足配置条件的文件都会被加载……
二、.properties 配置文件的加载
1、方法1
单个文件的加载:
<context:property-placeholder location="classpath*:properties/db.properties" />
2、方法2
单个文件的加载:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath*:db/jdbc.properties" />
</bean>
方法2
多个文件的加载:
<bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/opt/demo/config/demo-db.properties</value>
<value>classpath:/opt/demo/config/demo-db2.properties</value>
</list>
</property>
</bean>