Spring整合Hibernate的XML文件配置,以及web.xml文件配置

利用Spring整合Hibernate时的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"> <!-- 向 Spring 容器注册
AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor
识别注解 -->
<context:annotation-config /> <!-- 自动扫描base-pack子包下面Java文件,如果扫描到有@Component @Controller @Service等这些注解的类,则把这些类注册为bean -->
<context:component-scan base-package="cqvie.yjq" /> <!-- ************************************************************************************************ -->
<!-- 定义.properties文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> <!-- 如果有多个.properties文件,则可以透过 locations属性来设定:
<property name="locations">
<list>
<value>classpath:mailsender1.properties</value>
<value>classpath:mailsender2.properties</value>
</list>
</property>
-->
</bean> <!-- ************************************************************************************************ -->
<!-- 配置数据源 -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <!-- ************************************************************************************************ -->
<!-- 自动创建 SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 等价于下面的方法
<property name="annotatedClasses">
<list>
<value>cqvie.yjq.model.User</value>
<value>cqvie.yjq.model.Log</value>
</list>
</property>
-->
<!-- 扫描包下面的java文件 -->
<property name="packagesToScan">
<list>
<value>cqvie.yjq.model</value>
</list>
</property> <!-- 配置 hibernate.cfg.xml 中的信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean> <!-- ************************************************************************************************ -->
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 注解方式
<tx:annotation-driven transaction-manager="txManager"/>
-->
<!-- XML方式 -->
<aop:config>
<aop:pointcut expression="execution(public * cqvie.yjq.service..*.*(..))" id="bussinessService"/>
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice"/>
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="is*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- ************************************************************************************************ -->
<!-- Spring 调用 Hibernate 的持久化操作 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> </beans>

web.xml文件配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSH_170324</display-name> <welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- 默认路径:/WEB-INF/applicationContext.xml -->
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext*.xml</param-value> -->
<param-value>classpath:beans.xml</param-value>
</context-param> <!-- Spring 解决中文乱码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 增加session的使用范围 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<!-- 默认寻找beans.xml中的sessionFactory -->
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>
上一篇:机器学习实战 - 读书笔记(05) - Logistic回归


下一篇:5. JavaScript 正则表达式