Spring中引入其他配置文件

一、引入其他 模块XML  

在Spring的配置文件,有时候为了分模块的更加清晰的进行相关实体类的配置。

比如现在有一个job-timer.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="testQuartz" class="com.mc.bsframe.job.TestJob"></bean>

    <!-- 将需要执行的定时任务注入JOB中。 -->
    <bean id="testJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="testQuartz"></property>
        <!-- 任务类中需要执行的方法 -->
        <property name="targetMethod" value="doSomething"></property>
        <!-- 上一次未执行完成的,要等待有再执行。 -->
        <property name="concurrent" value="false"></property>
    </bean>

    <!-- 基本的定时器,会绑定具体的任务。 -->
    <bean id="testTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
        <property name="jobDetail" ref="testJob"></property>
        <property name="startDelay" value="3000"></property>
        <property name="repeatInterval" value="200000"></property>
    </bean>

    <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="testTrigger"></ref>
            </list>
        </property>
    </bean>
</beans>

在Spring的整体的配置文件中使用 <import resource="classpath*:/spring/job-timer.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:scpan="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">

    <!-- 会自动扫描com.mc.bsframe下的所有包,包括子包下除了@Controller的类。 -->
    <scpan:component-scan base-package="com.mc.bsframe">
        <scpan:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <scpan:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </scpan:component-scan>

    <!-- Spring中引入其他配置文件 -->
    <import resource="classpath*:/spring/job-timer.xml" />

</beans>

二、引入properties文件。

方法1:

    <!--引入数据库配置信息 -->
    <context:property-placeholder location="classpath*:properties/db.properties" />

方法2:

情况1配置一个:

    <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>

这些properties中就是key-value的键值对,使用的时候可以使用${xxx} 获取。

时间: 2024-08-24 15:20:19

Spring中引入其他配置文件的相关文章

模拟Spring中applicationContext.xml配置文件初始化bean的过程

package com.xiaohao.action; import java.io.File; import java.lang.reflect.Method; import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** *

spring加载jar包中多个配置文件(转)

转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:beanconfigs/applicationContext_1.xml, classpath*:

spring中scope(作用越)理解

今天总结了一下spring中作用域scope的用法.在spring中作用域通过配置文件形式的用法如下. <bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton"/> 一. 在spring 中常用的作用域有单例模式(singleton),和多例模式(prototype) 1.当一个bean的 作用域设置为singleton, 那么Spring IOC容

Spring中常用的配置和注解详解

一.  Spring中常用的配置文件详解 Spring中的配置文件详解 1.<!-- 配置注解bean的扫描路径 该配置表示从cn包下开始扫描--> <context:component-scan base-package="cn"></context:component-scan> 2.<!-- 加载资源文件 其中Location表示从哪个路径加载配置文件properties--> <context:property-placeh

Spring中Bean的作用域、Spring的自动注入、在spring配置文件中引入属性文件

1. Bean的作用域 Bean的作用域默认为单例模式. 2. 自动注入 3. 在spring配置文件中引入属性文件 Bean的作用域默认为单例模式. 原文地址:https://www.cnblogs.com/mcl2238973568/p/11478426.html

SSH——Struts2中引入Spring

一,为什么要使用Spring 1,装配JavaBean 摒弃老旧的new方式,spring为我们提供了一种机制,使得创建javaBean以及设置javaBean属性的工作可以通过配置文件以及Spring框架本身来完成.这样,当某些地方需要改变时,修改Spring的配置文件即可.这个过程实际上就是Spring框架通过读取相应的配置文件中的内容,并根据这些配置自动装在javaBean对象,设置JavaBean的属性. 2,整合第三方框架. spring的设计理念就是尽可能整合第三方框架,使得这些被整

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释 2016-04-14 23:40 13030人阅读 评论(2) 收藏 举报 分类: SSM(7) 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿出来一起分享一下,希望有不足的地方大家批评指正~~~ 首先   这篇文章暂时只对框架中所要用到的配置文件进行解

spring mvc框架中引入handlebars插件

本篇介绍引入spring mvc框架中引入handlebars.js插件最基本步骤 1.下载handlebars.js插件,并添加到项目中 2.下载handlebars依赖的jar包,添加到工程 红框中的是handlebars核心包,其他是handlebars依赖的工具包 3.在spring mvc配置文件springMvc-servlet.xml中添加handlebars视图解析器配置 1 <!-- VIEW RESOLVER --> 2 <bean id="handleba

关于spring中使用多个quatz配置文件配置计划任务时,只有其中一个文件的任务有效的问题

关于spring中使用多个quatz配置文件配置计划任务时,只有其中一个文件的任务有效的问题 问题描述 工作中用到了spring与quatz来实现定时器触发一些计划任务,原本都写在一个配置文件里.后来将不同业务逻辑拆分成两个文件后,发现每次spring容器启动后,只有其中一个文件中配置的计划任务有再执行,另一个则没有. 解决方案 通过google在stackoverflow上找到了解决方案,其实原因很简单.在spring容器中配置了多个Scheduler的情况下,必须给每个Scheduler指定