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:aop="http://www.springframework.org/schema/aop"

xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

作用一:<!-- 加载外部的properties配置文件 -->配数据库

<!-- maven下不能用这种方式,只能用第二种方式 -->

<!-- <context:property-placeholder location="classpath:jdbc.properties"

/> -->

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:jdbc.properties</value>

</list>

</property>

</bean>

 
<!-- 自动扫描与装配bean -->

<context:component-scan base-package="*.*.*" />

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

init-method="init" destroy-method="close">

<!-- 基本属性 url、user、password -->

<property name="url" value="${*}" />

<property name="username" value="${username}" />

<property name="password" value="${password}" />

<!-- 配置初始化大小、最小、最大 -->

<property name="initialSize" value="1" />

<property name="minIdle" value="1" />

<property name="maxActive" value="20" />

<!-- 配置获取连接等待超时的时间 -->

<property name="maxWait" value="60000" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

<property name="minEvictableIdleTimeMillis" value="300000" />

<property name="validationQuery" value="SELECT 'x'" />

<property name="testWhileIdle" value="true" />

<property name="testOnBorrow" value="false" />

<property name="testOnReturn" value="false" />

<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->

<property name="poolPreparedStatements" value="true" />

<property name="maxPoolPreparedStatementPerConnectionSize"

value="20" />

 
<!-- 配置监控统计拦截的filters -->

<property name="filters" value="mergeStat" />

</bean>


<!-- 配置druid监控spring jdbc -->

<bean id="druid-stat-interceptor"

class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />

<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"

scope="prototype">

<property name="patterns">

<list>

<value>org.bigdatacn.sfgk.wlzbs.service.*</value>

</list>

</property>

</bean>

<aop:config>

<aop:advisor advice-ref="druid-stat-interceptor"

pointcut-ref="druid-stat-pointcut" />

</aop:config>


作用二:<!-- 开启spring缓存 -->

<cache:annotation-driven cache-manager="cacheManager" />

<bean id="cacheManagerFactory"

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"

p:configLocation="classpath:ehcache.xml" p:shared="false" />

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"

p:cacheManager-ref="cacheManagerFactory" />

 
 作用三:性能优化

<!-- 监控dao和service的执行情况 -->

<bean id="facadeMonitoringAdvisor" class="net.bull.javamelody.MonitoringSpringAdvisor">

<property name="pointcut">

<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">

<property name="patterns">

<array>                          

<value>org\.bigdatacn\.sfgk\.wlzbs\.dao\..*</value>

<value>org\.bigdatacn\.sfgk\.wlzbs\.daoImpl\..*</value>

<value>org\.bigdatacn\.sfgk\.wlzbs\.service\..*</value>

<value>org\.bigdatacn\.sfgk\.wlzbs\.view\.service\..*</value>

<value>org\.bigdatacn\.sfgk\.wlzbs\.view\.serviceImpl\..*</value>

</array>

</property>

</bean>

</property>

</bean>

 
作用四:<!-- 配置SessionFactory -->,导入hibranate.cfg.xlm

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

</bean>


作用五: <!-- 配置声明式的事务管理(采用基于注解的方式) -->

<bean id="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

</beans>

#二、样例二

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!--使Spring支持自动检测组件,如注解的@Controller -->
    <context:component-scan base-package="com.parry.test.*" />
    <bean
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <!-- 数据库配置 -->
    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
         <!-- 测试数据库 -->
        <property name="jdbcUrl"
            value="jdbc:mysql://127.0.0.1:3066/TESTDB?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        
        <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
        <property name="idleConnectionTestPeriod" value="240" />
        <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:30,如果要永远存活设置为0 -->
        <!-- 数据库连接池过期时间应小于等于mysql的过期时间和mycat的过期时间 -->
        <property name="idleMaxAge" value="20" />
        <!-- 每个分区最大的连接数 -->
        <property name="maxConnectionsPerPartition" value="100" />
        <!-- 每个分区最小的连接数 -->
        <property name="minConnectionsPerPartition" value="20" />
        <!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定 -->
        <property name="partitionCount" value="1" />
        <!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 -->
        <property name="acquireIncrement" value="2" />
        <!-- 缓存prepared statements的大小,默认值:0 -->
        <property name="statementsCacheSize" value="0" />
        <property name="connectionTimeoutInMs" value="100" />
        <!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 -->
        <property name="releaseHelperThreads" value="3" />
    </bean>
    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis.xml" />
        <!-- mapper和resultmap配置路径 -->
        <property name="mapperLocations">
            <list>
                <!-- 表示在com.sfpay.mapper包或以下所有目录中,以-resultmap.xml结尾所有文件 -->
                <value>classpath:com/parry/test/dao/config/*.xml</value>
            </list>
        </property>
    </bean>
    <!-- 配置mapper接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cn21.calendar.dao" />
    </bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>
    <!-- 事务配置 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean('xxxx')的静态方法得到spring 
        bean对象 -->
    <bean class="com.parry.test.springcontext.SpringContextHolder"
        lazy-init="false" />

    <!-- 定时器 begin -->
    <!-- 赛程 调度业务对象 -->
    <bean id="deletePastOrderJob" class="com.parry.test.function.PublicTypeFunction" />
    <!-- 赛程 调度业务 -->
    <bean id="deletePastOrderTask"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="deletePastOrderJob" />
        <property name="targetMethod" value="deletePastOrder" />
    </bean>
    <!-- 赛程 调度器触发器 每天早上07:00执行一次 -->
    <bean id="deletePastOrderTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="deletePastOrderTask" />
        <property name="cronExpression" value="0 13 09 * * ? *" />
    </bean>
    <!-- 设置调度 -->
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <!-- <ref bean="deletePastOrderTaskTrigger" /> -->
            </list>
        </property>
    </bean>
    <!-- 定时器 end -->
    <!-- 服务器启动,初始化项目配置参数 -->
    <bean name="InitalizeBean" class="com.parry.test.configure.impl.InitalizeBean" />
</beans>

#三、样例三

<?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: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.1.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
    " default-autowire="byName">  
  
      
    <!-- 配置数据源 -->  
    <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:jdbc.properties</value>  
            </list>  
        </property>  
    </bean>  
      
    <!-- dataSource -->  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="url" value="${jdbc.url}"></property>  
        <property name="driverClassName" value="${jdbc.driver}"></property>  
        <property name="username" value="${jdbc.username}"></property>  
        <property name="password" value="${jdbc.password}"></property>  
    </bean>  
    <!-- sessionFaction -->  
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource"></property>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
            </props>  
        </property>  
        <property name="mappingDirectoryLocations">  
            <list>  
                <value>classpath:org/entity</value>  
            </list>  
        </property>  
    </bean>  
      
    <!-- 事务 -->  
     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory"></property>  
    </bean>  
      
    <!-- 增强 -->  
    <tx:advice id="txAdvice" transaction-manager="txManager">  
        <tx:attributes>  
            <tx:method name="add*" propagation="REQUIRED"/>  
            <tx:method name="save*" propagation="REQUIRED"/>  
            <tx:method name="update*" propagation="REQUIRED"/>  
            <tx:method name="del*" propagation="REQUIRED"/>  
            <tx:method name="get*" read-only="true"/>  
            <tx:method name="find*" read-only="true"/>  
            <tx:method name="query*" read-only="true"/>  
            <tx:method name="*"/>  
        </tx:attributes>  
    </tx:advice>  
    <aop:config>  
        <aop:pointcut id="mycut" expression="execution(* org.service..*.*(..))" />  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="mycut"/>  
    </aop:config>   
      
    <!-- 引入dao层 -->  
    <import resource="applicationContext-dao.xml"/>  
    <!-- 引入service层 -->  
    <import resource="applicationContext-service.xml"/>  
    <!-- 引入action层 -->  
    <import resource="applicationContext-action.xml"/>  
    </beans>  

参考:
https://blog.youkuaiyun.com/qq_27093465/article/details/52566506
https://blog.youkuaiyun.com/qq_32364027/article/details/51354462
https://blog.youkuaiyun.com/qq_34137397/article/details/72823031
https://blog.youkuaiyun.com/heng_ji/article/details/7022171
https://www.cnblogs.com/kaiwen1/p/6864458.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值