基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之二: Spring/proxool数据源/Hibernate的配置

      我们用的数据库为postgresql 9.4, 连接池用的是proxool。

      先全局看看文件分布位置:


      先配置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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值