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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring applicationContext.xml和hibernate.cfg.xml设置相关的知识,希望对你有一定的参考价值。

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设置的主要内容,如果未能解决你的问题,请参考以下文章

Spring -- Spring相关API (ApplicationContext getBean)

spring BeanFactory 与ApplicationContext

BeanFactory and ApplicationContext in Spring

Spring获取ApplicationContext

Spring 学习笔记——IoC容器(ApplicationContext)

Spring ApplicationContext下的refresh()方法