application-context.xml

212 阅读1分钟

application-context.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:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<context:component-scan base-package="xxx.xxx.xxx(your package)" use-default-filters="false"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  </context:component-scan>
<context:annotation-config />
<!-- 用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
     <list>
    <value>classpath:conf/jdbc.properties</value>
      </list>
   </property>
</bean> 
<!-- dataSource读取数据库的配置信息 ,这里用的是dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" init-method="init" destroy-method="close">
  <property name="driverClassName" value="${jdbc_driverClassName}"></property>
  <property name="url" value="${jdbc_url}"></property>
  <property name="username" value="${jdbc_username}"></property>
  <property name="password" value="${jdbc_password}"></property>
  <!-- 连接池最大使用连接数 -->
  <property name="maxActive" value="${jdbc_maxActive}"></property>
  <!-- 初始化连接大小 -->
  <property name="initialSize" value="${jdbc_initialSize}"></property>
  <!-- 获取连接最大等待时间 -->
  <property name="maxWait" value="${jdbc_maxWait}"></property>
  <!-- 连接池最大空闲 -->
  <property name="maxIdle" value="${jdbc_maxIdle}"></property>
  <!-- 连接池最小空闲 -->
  <property name="minIdle" value="${jdbc_minIdle}"></property>
  <!-- 自动清除无用连接 -->
  <property name="removeAbandoned" value="${jdbc_removeAbandoned}"></property>
  <!-- 清除无用连接的等待时间 -->
  <property name="removeAbandonedTimeout" value="${jdbc_removeAbandonedTimeout}"></property>
  <!-- 事物是否自动提交-->
  <property name="defaultAutoCommit" value="${jdbc_defaultAutoCommit}"></property>
  <!-- 连接属性 -->
  <property name="connectionProperties" value="clientEncoding=UTF-8"></property>
</bean> 

<!-- 配置SessionFactory,这里用的是hibernate4,mybatis目前还不太熟 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.byan.entity" />
  <property name="hibernateProperties">
  <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
  </props>
  </property>
</bean> 

<!-- 定义事务管理器,用了spring管理的hibernate4的事物,再也不用担心事物。。。-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
</bean> 

<!-- 声明事务管理器,此时在ServiceImpl或者DaoImpl类上需加上@Transactional注解-->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>