zoukankan      html  css  js  c++  java
  • Spring中引入其他配置文件

    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} 获取。

    Doing is better than nothing
  • 相关阅读:
    7.12-7.19 id、w、who、last、lastb、lastlog
    查询登录信息 w, who*, id, tty, last, finger
    [rhel-media] :Yum软件仓库唯一标识符,避免与其他仓库冲突。
    :整数 跳转到该行 Vim中常用的命令
    spec2006与spec2000的对比简要说明
    Linux性能监控与分析之--- CPU
    Android实现登录小demo
    python学习笔记——旧类与新类继承中的构造函数
    tair ldb存储引擎性能測试方案
    串口通讯方式1编程
  • 原文地址:https://www.cnblogs.com/handsome1013/p/7472824.html
Copyright © 2011-2022 走看看