spring 整合 struts2 + Hibernate application配置文件(基于注解)

下面是 application.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        ">
        <!-- 上面的开启了
                    aop命名空间
                    tx命名空间
                    context命名空间
        -->
        
    <!-- 开启注解开发spring模式  -->
    <context:component-scan base-package="cn.itcast"/>
    
    <!-- hibernateTemplate,用于提供dao层的各种操作 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <!-- 
            注入SessionFactory,虽然HibernateTemplate类中没有对应的sessionFactory属性,
            但是其父类HibernateAccessor有 
        -->
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 
        sessionFactory的bean,这里使用的sessionFactory不再是SessionFactoryBean了,
        而是它的子类AnnotationSessionFactoryBean,专门用于注解的sessionFactorybean    
     -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <!-- 注入dataSource数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 注入Hibernate的各种属性 -->
        <property name="hibernateProperties">
            <props>
                <!-- 数据库方言的设置,必须设置,用于生成正确的sql语句 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop><!-- 展示sql,非必须,开发使用 -->
                <prop key="hibernate.format_sql">true</prop><!-- 格式化sql,非必须,开发使用 -->
            </props>
        </property>
        <!-- 
            资源注册,和之前的不一样了,因为采用了注解模式,没有了xxxhbm.xml文件了,不能像之前那样注册了。
            采用的是包扫描    
        -->
        <property name="packagesToScan">
            <list>
                <!-- 这里使用的package,不是路径,所以使用的是点,而不是斜杠 -->
                <value>cn.itcast.ssh.annotation.user.vo</value>
            </list>
        </property>    
    </bean>
    
    <!-- dataSource,配置数据源,包括驱动,url,用户名,密码 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/DB_Demo1"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
</beans>
原文地址:https://www.cnblogs.com/daimajun/p/7058996.html