applicationContext.xml配置文件详解

本文详细介绍了如何使用Spring框架进行组件扫描,并通过PropertyPlaceholderConfigurer配置文件加载数据库连接参数,利用ComboPooledDataSource实现数据库连接池管理,进一步通过LocalSessionFactoryBean创建SessionFactory,配置Hibernate属性以实现与数据库的交互,同时整合事务管理器并启用AspectJ自动代理,以实现面向切面编程。
<?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"
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">


<!-- Auto scan base packet 自动扫描所有包-->
<context:component-scan base-package="com" />


<!-- JDBC Config 导入配置有连接数据库参数值配置文件-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>


<!-- DataSource Configuration 配置连接数据库的数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass">
<value>${hbm.driver}</value>
</property>
<property name="jdbcUrl">
<value>${hbm.url}</value>
</property>
<property name="user">
<value>${hbm.user}</value>
</property>
<property name="password">
<value>${hbm.password}</value>
</property>
</bean>


<!-- SessionFactory 创建session工厂-->
<bean id="sessionFacoty"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

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

<!-- 自动建立实体类与数据表之间的映射-->

<property name="packagesToScan">
<list>
<value>com.dns.dto.</value>
</list>

</property>

<!-- 配置方言,控制台输出格式-->

<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${hbm.showsql}</prop>
<prop key="hibernate.format_sql">${hbm.formatsql}</prop>

<prop key="hibernate.dialect">${hbm.dialect}</prop>

<!-- 将当前的session上下文交给spring来控制管理-->

<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate4.SpringSessionContxt

</prop>

<!-- 是否向数据库元数据设置默认值-->

<prop key="hibernate.temp_use_jdbc_metadata_defaults">false</prop>
</props>
</property>
</bean>


<!-- Transaction Configuration 配置事务管理器-->
<bean id="transactinManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFacoty"></property>
</bean>
<!-- 将配置有@Transactional的方法纳入事务管理-->
<tx:annotation-driver transaction-manager="transactionManager"></tx>
<!-- 自动为spring容器中那些配置@aspectj切面的bean创建代理,植入切面-->
<aop:aspectj-autoproxy />
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值