基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置
2016-06-15 10:11
609 查看
我们用的数据库为postgresql 9.4, 连接池用的是proxool。
先全局看看文件分布位置:
先配置web.xml,在里面定义Spring的编码CharacterEncodingFilter,设置为utf8,proxool初始化类,和spring 的初始化类DispatcherServlet, spring容器初始化、web mvc这些的入口都靠它完成,在这个类里面指定容器的指定配置参数为classpath下的applicationContext.xml。
打开applicationContext.xml,里面最先导入数据库配置文件spring-db.xml,再导入hibernate配置,然后自动扫描controller和dao的包并根据包里面类的依赖关键字进行加载;这里还配置了一个contextHolder的类,可以用它直接获得容器里面的类实例(ContextHolder.getBean("beanid"));最后加载spring-config/下面所有的xml配置文件
下面是spring-db.xml数据源的配置,里面除了配置dataSource,还有一个jdbcTemplate:
下面是hibernate插件的配置文件,spring-hibernate.xml,里面配置了sessionfactory和transactionmanager,个人比较抗拒为每个pojo配置一个xml(简直多此一举),所以用在pojo里面注解的方式作为hibernate的entity,所以指定了packagesToScan属性,传入entity所在的包,hibernate会在适当的时候使用这些entities:
先全局看看文件分布位置:
先配置web.xml,在里面定义Spring的编码CharacterEncodingFilter,设置为utf8,proxool初始化类,和spring 的初始化类DispatcherServlet, spring容器初始化、web mvc这些的入口都靠它完成,在这个类里面指定容器的指定配置参数为classpath下的applicationContext.xml。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>gpchat</display-name> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--管理proxool配置文件--> <servlet> <servlet-name>proxoolServletConfigurator</servlet-name> <servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator </servlet-class> <init-param> <param-name>xmlFile</param-name> <param-value>WEB-INF/proxool.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>2</load-on-startup> </servlet> --> <servlet> <servlet-name>gpchat</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>gpchat</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>因为spring作为容器,管理的类别很多,为了方便管理,我将它们分开存放(看图中黄色部分),然后在applicationContext.xml里面一一引用。
打开applicationContext.xml,里面最先导入数据库配置文件spring-db.xml,再导入hibernate配置,然后自动扫描controller和dao的包并根据包里面类的依赖关键字进行加载;这里还配置了一个contextHolder的类,可以用它直接获得容器里面的类实例(ContextHolder.getBean("beanid"));最后加载spring-config/下面所有的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:mvc="http://www.springframework.org/schema/mvc" 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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- <context:component-scan base-package="com.freestyle.test"/> --> <import resource="classpath*:/spring-db-config/spring-db.xml"/> <import resource="classpath*:/hibernate-config/spring-hibernate.xml"/> <context:component-scan base-package="com.freestyle.spring;com.freestyle.test.controller;com.freestyle.test.hibernate.dao;com.freestyle.test.hibernate.controller"/> <mvc:annotation-driven></mvc:annotation-driven> <!-- 解决@ResponseBody 的实现类其默认的编码是 iso-8859-1的问题 --> <mvc:annotation-driven> <mvc:message-converters> <!-- default StringHttpMessageConverter, solve encoding problem --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> <property name="writeAcceptCharset" value="false" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views" /> <property name="suffix" value=".jsp" /> </bean> <!-- 通过配置contextHolder来获得ApplicationContext --> <bean id="contextHolder" class="com.freestyle.spring.ContextHolder"> </bean> <import resource="classpath*:/spring-config/*.xml"/> <!-- ss <bean name="/hello" class="com.freestyle.test.controller.HelloController"/> --> </beans>
下面是spring-db.xml数据源的配置,里面除了配置dataSource,还有一个jdbcTemplate:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.bmsdbv2</value> </property> </bean> <!-- 配置Jdbc Template --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource"> </constructor-arg> </bean> </beans>
下面是hibernate插件的配置文件,spring-hibernate.xml,里面配置了sessionfactory和transactionmanager,个人比较抗拒为每个pojo配置一个xml(简直多此一举),所以用在pojo里面注解的方式作为hibernate的entity,所以指定了packagesToScan属性,传入entity所在的包,hibernate会在适当的时候使用这些entities:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" <span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" <span style="white-space:pre"> </span>xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" <span style="white-space:pre"> </span>xmlns:tx="http://www.springframework.org/schema/tx" <span style="white-space:pre"> </span>xsi:schemaLocation="http://www.springframework.org/schema/beans <span style="white-space:pre"> </span>http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 配置hibernate的session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy"> <property name="dataSource" ref="dataSource"></property> <property name="packagesToScan"> <!-- hibernate sessionFactory管理的类存放位置,自动装载 --> <list> <value>com.freestyle.test.hibernate.entities</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <!-- 设置自动创建|更新|验证数据库表结构 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <!-- 是否在控制台显示sql --> <prop key="hibernate.show_sql">true</prop> <!-- 是否格式化sql,优化显示 --> <prop key="hibernate.format_sql">true</prop> <!-- 是否开启二级缓存 --> <prop key="hibernate.cache.use_second_level_cache">false</prop> <!-- 是否开启查询缓存 --> <prop key="hibernate.cache.use_query_cache">false</prop> <!-- 数据库批量查询最大数 --> <prop key="hibernate.jdbc.fetch_size">100</prop> <!-- 数据库批量更新、添加、删除操作最大数 --> <prop key="hibernate.jdbc.batch_size">50</prop> <!-- 是否自动提交事务 --> <prop key="hibernate.connection.autocommit">false</prop> <!-- 指定hibernate在何时释放JDBC连接 --> <prop key="hibernate.connection.release_mode">auto</prop> </props> </property> </bean> <!-- 定义Hibernate事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- Hibernate Oracle sequence的使用技巧
- 模拟Spring的简单实现
- jsp Hibernate批量更新和批量删除处理代码
- Spring整合WebSocket应用示例(上)
- jsp hibernate的分页代码第1/3页
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- springmvc 发送ajax出现中文乱码的解决方法汇总
- Struts2+Hibernate实现数据分页的方法
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的