springMVC中bean容器:bean.xml的配置
2016-04-06 18:51
645 查看
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 1.添加组件对注解的支持:扫描 -->
<context:component-scan base-package="com.lovo.elec"></context:component-scan>
<!-- 2.C3P0连接池 (另一种还有dbcp) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<!-- &的转义字符:& -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/elec_u16?useUnicode=true&characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="5"></property>
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="30"></property>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initPoolSize" value="10"></property>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30-->
<property name="acquireRetryAttempts" value="30" />
<!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
<property name="breakAfterAcquireFailure" value="true" />
</bean>
<!-- 3.创建sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4.创建事务管理器 -->
<bean id="transManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.事务处理:注解 -->
<tx:annotation-driven transaction-manager="transManager" />
<!-- 事务处理:配置文件 -->
<!--
<tx:advice id="adviceBean" transaction-manager="trManager">
<tx:attributes> <tx:method name="save*" isolation="DEFAULT"
propagation="REQUIRED" read-only="false"/> <tx:method name="update*"
isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false"/> <tx:method name="*" read-only="true"/>
</tx:attributes> </tx:advice> <aop:config> <aop:pointcut
expression="execution(* cn.lovo.elec.service..*.*(..))"
id="pointcutBean"/> <aop:advisor advice-ref="adviceBean"
pointcut-ref="pointcutBean"/> </aop:config>
-->
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 1.添加组件对注解的支持:扫描 -->
<context:component-scan base-package="com.lovo.elec"></context:component-scan>
<!-- 2.C3P0连接池 (另一种还有dbcp) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<!-- &的转义字符:& -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/elec_u16?useUnicode=true&characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<!-- 连接池中保留的最小连接数。 -->
<property name="minPoolSize" value="5"></property>
<!-- 连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="30"></property>
<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initPoolSize" value="10"></property>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="5" />
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30-->
<property name="acquireRetryAttempts" value="30" />
<!-- 获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
获取连接失败后该数据源将申明已断开并永久关闭。Default: false -->
<property name="breakAfterAcquireFailure" value="true" />
</bean>
<!-- 3.创建sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4.创建事务管理器 -->
<bean id="transManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5.事务处理:注解 -->
<tx:annotation-driven transaction-manager="transManager" />
<!-- 事务处理:配置文件 -->
<!--
<tx:advice id="adviceBean" transaction-manager="trManager">
<tx:attributes> <tx:method name="save*" isolation="DEFAULT"
propagation="REQUIRED" read-only="false"/> <tx:method name="update*"
isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"
read-only="false"/> <tx:method name="*" read-only="true"/>
</tx:attributes> </tx:advice> <aop:config> <aop:pointcut
expression="execution(* cn.lovo.elec.service..*.*(..))"
id="pointcutBean"/> <aop:advisor advice-ref="adviceBean"
pointcut-ref="pointcutBean"/> </aop:config>
-->
</beans>
相关文章推荐
- Spring Bean基本管理实例详解
- Spring中多配置文件及引用其他bean的方式
- Spring MVC--拦截器实现和用户登陆例子
- SpringMVC源码解读之HandlerMapping - AbstractUrlHandlerMapping系列request分发
- 解析Java中如何获取Spring中配置的bean
- Java简单实现SpringMVC+MyBatis分页插件
- Java中BEAN与EJB的区别浅析
- SpringMVC文件上传 多文件上传实例
- SpringMVC restful 注解之@RequestBody进行json与object转换
- SpringMVC源码解读之HandlerMapping
- 深入解析Java的Spring框架中bean的依赖注入
- spring mvc4的日期/数字格式化、枚举转换示例
- SpringMVC源码解读之 HandlerMapping - AbstractDetectingUrlHandlerMapping系列初始化
- 解决springmvc+mybatis+mysql中文乱码问题
- SpringMVC上传图片与访问
- Java中Spring获取bean方法小结
- 详解Spring Boot 使用Java代码创建Bean并注册到Spring中
- 详解Java的Spring框架中bean的定义以及生命周期
- 详解Java的Spring框架中bean的注入集合
- Java的Spring框架中bean的继承与内部bean的注入