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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM