spring配置文件详解
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring配置文件详解相关的知识,希望对你有一定的参考价值。
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 引用外部资源 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
</bean>
<!-- 配置 SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--
第一种方式: 引用这个 hibernate.cfg.xml,那么 hibernate 的基本信息就写在 hibernate.cfg.xml
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
-->
<!-- 若使用这种方式,可以省略 hibernate.cfg.xml -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.mysqlDialect</prop>
</props>
</property>
<!-- 配置 映射文件
value: 是路径
-->
<property name="mappingLocations" value="classpath:cn/hc/ssh/entities/*.hbm.xml"></property>
</bean>
<!-- 声明式事务 -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务属性 -->
<tx:advice id="tA" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="set*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="show*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置切点及应用通知 -->
<aop:config>
<aop:pointcut expression="execution(* cn.hc.ssh.service.impl.*.*(..))" id="pt"/>
<aop:advisor advice-ref="tA" pointcut-ref="pt"/>
</aop:config>
<bean id="employeeDao" class="cn.hc.ssh.dao.impl.EmployeeDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="departmentDao" class="cn.hc.ssh.dao.impl.DepartmentDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="employeeService" class="cn.hc.ssh.service.impl.EmployeeServiceImpl">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="departmentService" class="cn.hc.ssh.service.impl.DepartmentServiceImpl">
<property name="departmentDao" ref="departmentDao"></property>
</bean>
<bean id="employeeAction" class="cn.hc.ssh.actions.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
<property name="departmentService" ref="departmentService"></property>
</bean>
</beans>
以上是关于spring配置文件详解的主要内容,如果未能解决你的问题,请参考以下文章