applicationContext.xml的复用(import resource)

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

比如:现在有mybatis、tx事务、redis等 的配置

如果全放在applicationContext.xml中,会特别的长,不方便查看与修改

使用 import resource 标签

将每个模块的配置抽离:

applicationContext-tx.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 5、配置事务  使用的tx前缀的标签, 导入tx的命名空间-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入DataSource -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 事务的策略 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- 声明事务的规则 : 针对业务层的不同的方法,采用不同的规则-->
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS" />
            <tx:method name="query*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="del*" propagation="REQUIRED"  rollback-for="java.lang.Exception"  timeout="2"/>
            <tx:method name="update*" propagation="REQUIRED"  rollback-for="java.lang.Exception"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 事务的织入 -->
    <aop:config>
        <aop:pointcut expression="execution( *  com.zl.housePlus.service..*.*(..) )" id="pointcut1"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
    </aop:config>

</beans>
View Code

applicationContext-redis.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--redis哨兵 -->
    <bean id="redisSentinelConfiguration"
        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean
                class="org.springframework.data.redis.connection.RedisNode">
                <property name="name" value="mymaster" />
            </bean>
        </property>
        <!-- 哨兵 -->
        <property name="sentinels">
            <set>
                <bean
                    class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="127.0.0.1" />
                    <constructor-arg name="port" value="10001" />
                </bean>
                <bean
                    class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="127.0.0.1" />
                    <constructor-arg name="port" value="10002" />
                </bean>
                <bean
                    class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="127.0.0.1" />
                    <constructor-arg name="port" value="10003" />
                </bean>
            </set>
        </property>
    </bean>


    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="jedisPoolConfig"></property>
        <property name="hostName" value="${redis.host}"></property>
        <property name="port" value="${redis.port}"></property>
        <property name="password" value="${redis.password}" />
        <!-- 哨兵 -->
        <constructor-arg name="sentinelConfig"
            ref="redisSentinelConfiguration" />
    </bean>

    <bean id="jedisPoolConfig"
        class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxActive}" /><!-- 最大连接数 -->
        <property name="maxIdle" value="${redis.maxIdle}" /><!-- 最大闲置 -->
        <property name="minIdle" value="${redis.minIdle}" /><!-- 最小闲置 -->
        <property name="maxWaitMillis" value="${redis.maxWait}" /><!-- 
            最大等待 -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" /><!-- 
            可以获取 -->
    </bean>


    <bean id="redisTemplate"
        class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"
            ref="jedisConnectionFactory" />

        <!-- 序列化方式 建议key/hashKey采用StringRedisSerializer -->
        <property name="keySerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <!-- 开启REIDS事务支持 -->
        <property name="enableTransactionSupport" value="false" />
    </bean>

    <bean id="redisHttpSessionConfiguration"
        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <!-- 设置session存活时间 -->
        <property name="maxInactiveIntervalInSeconds" value="300000"></property>
    </bean>

    <!-- 对string操作的封装 -->
    <!-- <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 
        <constructor-arg ref="jedisConnectionFactory" /> 开启REIDS事务支持 <property name="enableTransactionSupport" 
        value="false" /> </bean> -->

</beans>
View Code

applicationContext-mybatis.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 2、配置一个数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 配置相应的数据 -->
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
    </bean>
    
    <!-- 3、创建一个SqlSessionFactorybean ,用于产生SessionFactory , 需要注入数据源-->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath*:mapper/*Mapper.xml"/>
        <property name="typeAliasesPackage" value="com.zl.houseplus.entity"/>
        <!-- 添加全局配置 -->
        <property name="globalConfig" ref="globalConfig" />
        <!-- 配置分页插件 -->
        <property name="plugins">
            <array>
                <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
                    <!-- <property name="sqlParser" ref="自定义解析类、可以没有" />
                    <property name="dialectClazz" value="自定义方言类、可以没有" />
                    COUNT SQL 解析.可以没有-->
                    <property name="countSqlParser" ref="countSqlParser" /> 
                </bean>
            </array>
        </property>
    </bean>
    
    <bean id="countSqlParser" class="com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize">
        <!-- 设置为 true 可以优化部分 left join 的sql -->
            <property name="optimizeJoin" value="true"/>
    </bean>
    
    <!-- 4、mapper扫描器:通过mapper代理,创建mapper代理对象,保存到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="basePackage" value="com.zl.housePlus.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean> 
    
    <!-- 序列生成策略 -->
    <bean id="globalConfig"  class="com.baomidou.mybatisplus.core.config.GlobalConfig">
        <property name="dbConfig" ref="dbConfig" />
    </bean>
    <bean id="dbConfig"  class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
        <property name="keyGenerator" ref="keyGenerator" />
    </bean>
    <bean id="keyGenerator"  class="com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator" />
        

    
</beans>
View Code

最后的主 applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
    <!-- 加载db.properties 文件-->
    <context:property-placeholder location="classpath:db.properties,classpath:redis.properties" 
      file-encoding
="UTF-8"/> <!-- 扫描其他类中的注解 --> <context:component-scan base-package="com.zl.housePlus"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 加载其他配置文件 --> <import resource="applicationContext-tx.xml"/> <import resource="applicationContext-mybatis.xml"/> <import resource="applicationContext-redis.xml"/> </beans>

其他类中加载配置文件只需要加载applicationContext.xml即可(比如测试类)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class JedisTest {
。。。
原文地址:https://www.cnblogs.com/64Byte/p/13217417.html