applicationContext.xml简单笔记

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

    xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      ">

 <!--    <bean id="exceptionHandler" class="net.crm.base.exception.MyExceptionHandler"/> 

    <mvc:annotation-driven />
    <aop:aspectj-autoproxy />
    <context:component-scan base-package="net.crm.*" ></context:component-scan>-->

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

     <bean id="dataSources" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
      <!-- 基本属性 url、user、password -->
      <property name="url" value="${jdbcUrl}" />
      <property name="username" value="${user}" />
      <property name="password" value="${password}" />

      <!-- 配置初始化大小、最小、最大 -->
      <property name="initialSize" value="1" />
      <property name="minIdle" value="1" />
      <property name="maxActive" value="100" />

      <!-- 配置获取连接等待超时的时间 -->
      <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="stat" />
  </bean>

        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="initialPoolSize" value="10" />
        <property name="maxIdleTime" value="100" />
        <property name="maxPoolSize" value="200" />
        <property name="minPoolSize" value="10" />
    </bean>

     <!-- 配置Jdbc模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

  <!--
    <aop:config>
        <aop:pointcut id="servicePointcut"   expression="execution(* *..service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
    </aop:config>

    <bean id="userAdvice" class="com.niuren.advice.UserAdvice"></bean>
    <aop:config>
        <aop:aspect id="userAop" ref="userAdvice">
            <aop:pointcut id="target" expression="execution(* com.niuren.service.*.*(..))"/>
            <aop:before method="doBefore" pointcut-ref="target"/>
        </aop:aspect>
    </aop:config>
  -->

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:net/crm/**/mapper/*.xml" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>ApplicationResources</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="true" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass" value="org.springframework.stereotype.Repository" />
        <property name="basePackage" value="net.crm" />
    </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>

<!-- 配置那些类的方法进行事务管理 -->
    <aop:config>
        <aop:pointcut id="executeService" expression="execution(* net.crm.*.service..*(..))"/>
        <aop:advisor pointcut-ref="executeService" advice-ref="txAdvice"/>
    </aop:config>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="load*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="read*" read-only="true"/>
            <tx:method name="sync*"/>
            <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
        </tx:attributes>
    </tx:advice>

    <!-- 事务注解支持
    <tx:annotation-driven transaction-manager="transactionManager" /> -->
    <!-- 扫描注解文件 -->
    <mvc:annotation-driven />
    <context:component-scan base-package="net.crm.*" />
    <aop:aspectj-autoproxy /> 

</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:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <!-- 自动扫描的包名,如果有多个包,请使用逗号隔开 -->
    <context:component-scan base-package="com.jieyou.*" />

    <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />
       <!-- 启动对aspectj的支持 -->
       <aop:aspectj-autoproxy/>

    <!-- 自动搜索指定包及其子包下的所有Bean类 -->
    <context:component-scan base-package="com.jieyou.*" />

    <!-- 读取属性文件信息,将这些信息设置成Spring配置文件的数据 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
        <!-- 定义数据源Bean,使用C3P0数据源实现 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="initialPoolSize" value="10" />
        <property name="maxIdleTime" value="100" />
        <property name="maxPoolSize" value="200" />
        <property name="minPoolSize" value="10" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--dataSource属性指定要用到的连接池 -->
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="annotationClass" value="org.springframework.stereotype.Repository" />
        <property name="basePackage" value="com.jieyou" />
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

<!-- 配置JDBC数据源的局部事务管理器 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置事务增强处理Bean,指定事务管理器 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!-- 所有已get开头的方法是只读的 -->
        <!--     <tx:method name="get*" read-only="true" propagation="REQUIRED" /> -->
            <!-- 其他方法使用默认的事务管理器 -->
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

    <!-- AOP配置元素
    <aop:config>
        <aop:pointcut expression="execution(* com.jieyou.login_register.service.impl.*.*(..))"
            id="txPoint" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
    </aop:config>
    -->
</beans>
时间: 2024-08-05 07:54:11

applicationContext.xml简单笔记的相关文章

mybatis-config.xml简单笔记

mybatis-config.xml简单笔记 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <

【JAVA错误笔记】 - 【Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法】

错误描述: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 原因分析: 问题主要由于加载spring的默认配置文件位置一般是在/WEB-INF/下找applicationContext.xml文件. 而Myeclipse文件自动生成的applicationContext文件是放在/WEB-INF/classes/applicationContext.xml. 所以在默认加载中是找到applicati

SpringMVC之application-context.xml,了解数据库相关配置

上一篇SpringMVC之web.xml让我们了解到配置一个web项目的时候,如何做基础的DispatcherServlet相关配置,作为SpringMVC上手的第一步,而application-context.xml则让我们了解到如何将数据库信息加载到项目中,包含关键的数据库连接信息.sqlSessionFactory.事务等关键因素. ①.xml内容 <?xml version="1.0" encoding="UTF-8"?> <beans x

applicationContext.xml文件如何调用外部properties等配置文件

只需要在applicationContext.xml文件中添加一行: <!-- 导入外部的properties文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> 其中:location属性是指该文件的位置. 如果是在src目录下的话,该位置为:classpath:文件名.后缀 如果是在/WEB-INF/目录下的话,该位置为: /WEB-INF/文件名.后缀 但是要

applicationContext.xml中的使用${}是代表什么意思?

在applicationContext.xml文件中,使用 ${xxx} 表示的是:调用某个变量,该变量的名称就是{xxx}里面的"xxx". 例如:在applicationContext.xml文件中配置数据连接池 <!-- 配置数据链接池 --> <property name="dataSource"> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"

web.xml简单配置

<?xml version="1.0" encoding="UTF-8"?> <!-- 基本表头 --> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation

@Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml

项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" /> 这样在程序代码中直接用@Value("${name}")就能直接取到properties文件中定义的变量

XML学习笔记(五):使用 jdom和dom4j 解析XML

XML解析的详细分析与jaxp解析XML详见:XML学习笔记(四):使用 DOM和SAX 解析XML 一.JDom 1.创建XML文件: 1)Document类即代表整个XML文档,把生成的 Document 利用 XMLOutputter 类输出即可. 2)映射关系:元素:Element:属性:Attribute:注解:Comment:文本信息:Text: 3)注意:addContent()是追加,setContent()会覆盖. /** * 创建XML * * @throws IOExcep

spring 找不到applicationContext.xml解决方法

初学Spring在用Resource rs=new ClassPathResource("applicationContext.xml");时老是遇到这个错误.后来发现用ApplicationContext ctx=new FileSystemXmlApplicationContext("WebContent/WEB-INF/applicationContext.xml"):可以解决这个问题.仔细研究了下:之所以我用ClassPathResource中找不到appl