码迷,mamicode.com
首页 > 移动开发 > 详细

spring applicationContext.xml和hibernate.cfg.xml设置

时间:2017-01-05 20:15:00      阅读:383      评论:0      收藏:0      [点我收藏+]

标签:bsp   led   .config   home   next   教程   oid   public   ref   

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"
       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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:property-placeholder location="classpath:database.properties"></context:property-placeholder>

    <!--
    使用Javabean读取资源文件
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:system.properties</value>
        </property>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>-->
    

    <!-- 指定IOC 自动扫描的包 -->
    <context:component-scan base-package="com.lingdong"></context:component-scan>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="useCodeAsDefaultMessage" value="true"/>
    </bean>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass">
            <value>${jdbc.driverClass}</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.userName}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="initialPoolSize">
            <value>${jdbc.initialPoolSize}</value>
        </property>
        <property name="maxPoolSize">
            <value>${jdbc.maxPoolSize}</value>
        </property>
    </bean>

    <!-- 配置 Hibernate的 sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation">
            <value>${hibernate.cfg.xml}</value>
        </property>
        <property name="mappingLocations">
            <value>${hibernate.hbm.xml}</value>
        </property>
    </bean>
    <!--<bean id="loggingAspect" class="com.lingdong.aop.LoggingAspect"/>-->
    <!--Spring Aop 启用自动代理注解 -->
    <!--<aop:aspectj-autoproxy proxy-target-class="true"/>-->
    <!--配置Aop 切入点,切面-->
    <!--<aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.lingdong.aop.*.*(..))"></aop:pointcut>
        <aop:aspect ref="loggingAspect">
            <aop:before method="before" pointcut-ref="pointcut"></aop:before>
        </aop:aspect>
    </aop:config>-->

    <!-- 配置 Spring 声明式事务 -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!--启用事务注解 驱动,使用注解配置事务时使用-->
    <!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>-->

    <!-- 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务切入点,与事务属性关联  -->
    <aop:config>
        <aop:pointcut expression="execution(* com.lingdong.service.*.*(..))" id="pointcut"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>


</beans>

 

hibernate.cfg.xml配置

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
    </session-factory>
</hibernate-configuration>

 

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
    </session-factory>
</hibernate-configuration>

 

本文出自 “Java技术博客” 博客,请务必保留此出处http://lingdong.blog.51cto.com/3572216/1889446

    技术分享
技术分享
0人
了这篇文章
类别:spring┆阅读(2)┆评论(0) ┆ 编辑删除 ┆ 返回博主首页返回博客首页
技术分享

文章评论

 
 

发表评论            

昵  称:
主  页:
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容:
 

同时赞一个

返回顶部

spring applicationContext.xml和hibernate.cfg.xml设置

标签:bsp   led   .config   home   next   教程   oid   public   ref   

原文地址:http://www.cnblogs.com/dongblogs/p/6253546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!