码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC+JPA+SpringData配置

时间:2015-03-02 11:21:23      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:java   spring   spring mvc   


pom.xml

<properties>
	    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	    <jdk.version>1.7</jdk.version>
	    <junit.version>4.12</junit.version>
	    <spring.version>4.1.1.RELEASE</spring.version>
	    <jackson.version>2.4.3</jackson.version>
	    <hessian.version>4.0.38</hessian.version>
	    <log4j.version>1.2.14</log4j.version>
	    <slf4j.log4j12.version>1.7.7</slf4j.log4j12.version>
	    <aspectj.version>1.8.3</aspectj.version>
	    <httpclient.version>4.3.6</httpclient.version>
	    <spring.data.jpa.version>1.7.0.RELEASE</spring.data.jpa.version>
	    <hibernate.version>4.3.7.Final</hibernate.version>
	    <oracle.jdbc.version>11.2.0.RELEASE</oracle.jdbc.version>
	    <dubbo.version>2.5.3</dubbo.version>
	    <freemarker.version>2.3.21</freemarker.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		
		<!-- spring -->
	    <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<!-- spring mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-jpa</artifactId>
			<version>${spring.data.jpa.version}</version>
		</dependency>
		
		<!-- JackSon -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		
		<!-- c3po -->
	    <dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5-pre9</version>
		</dependency>
		
		<!-- oracle jdbc -->
		<dependency>
	  		<groupId>com.oracle</groupId>
	  		<artifactId>ojdbc6</artifactId>
	  		<version>${oracle.jdbc.version}</version>
		</dependency>
		<!-- Hibernate+JPA -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>${hibernate.version}</version>
		</dependency>
		
		<!-- aspectj -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		
		<!-- hessian -->
	    <dependency>
			<groupId>com.caucho</groupId>
			<artifactId>hessian</artifactId>
			<version>${hessian.version}</version>
		</dependency>
		
		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.5.2</version>
		</dependency>
		
		<!-- log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.log4j12.version}</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>${httpclient.version}</version>
		</dependency>
		
		<dependency>
			<groupId>com.tiamaes.gjds</groupId>
			<artifactId>gjds-util</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>
		
		<dependency>
			<groupId>com.tiamaes.gjds.dxp</groupId>
			<artifactId>gjds-dxp-api</artifactId>
			<version>1.0.0-SNAPSHOT</version>
		</dependency>
		
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.21</version>
		</dependency>
	</dependencies>
	
	<build>
		<finalName>gjds-dxp</finalName>
		<plugins>  
	        <plugin>  
	            <groupId>org.apache.maven.plugins</groupId>  
	            <artifactId>maven-compiler-plugin</artifactId>  
	            <version>2.0.2</version>  
	            <configuration>  
	                <source>${jdk.version}</source>  
	                <target>${jdk.version}</target>  
	            </configuration>  
	        </plugin>  
    	</plugins>
	</build>

web.xml

<!-- 载入log4j配置文件 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	
	<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	
	<!--Spring log4j Config loader-->
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	
	<!-- Spring 刷新Introspector防止内存泄露 -->  
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext*.xml
		</param-value>
	</context-param>
  
	<!-- springMVC配置 -->
	<servlet>
		<servlet-name>springMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!--Spring的ApplicationContext 载入 -->
	<listener>
	  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- spring字符过滤器 -->
	<filter>
	  <filter-name>characterEncodingFilter</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>
	  <init-param>
	    <param-name>forceEncoding</param-name>
	    <param-value>true</param-value>
	  </init-param>
	</filter>
	<filter-mapping>
	  <filter-name>characterEncodingFilter</filter-name>
	  <url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<context-param>
	  <param-name>webAppRootKey</param-name>
	  <param-value>gjds-dxp.root</param-value>
	</context-param>
	
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
		
	<mvc:annotation-driven conversion-service="conversionService" >
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json; charset=UTF-8</value>  
                   		<value>application/x-www-form-urlencoded; charset=UTF-8</value>
					</list>
				</property>
				<property name="prettyPrint" value="true"/>
				<property name="objectMapper">  
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper"
                    	p:serializationInclusion="NON_NULL">
                    	
                        <property name="dateFormat">
                            <bean class="java.text.SimpleDateFormat">  
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                            </bean>  
                        </property>
                    </bean>
                </property>
			</bean>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/plain;charset=UTF-8</value>
						<value>text/html;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>	
	</mvc:annotation-driven>
	
	<!-- <mvc:view-controller path="/" view-name="redirect:/login"/> -->
	
	<context:annotation-config />
	
	<context:component-scan base-package="com.tiamaes.**.controller" >
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>
	
	<!-- 控制日期格式的 -->
	<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<!--<property name="converters">
			<list>
				<bean class="com.zrhis.base.converter.DateConverter" ></bean>
			</list>
		</property>
	    -->
	</bean>
	
	<!-- p:suffix=".jsp" -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
		p:prefix="/WEB-INF/" p:suffix=".jsp">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
	</bean>

    <mvc:default-servlet-handler/>
	
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
    <import resource="classpath:hessian.xml"/>
</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/task
		http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://www.springframework.org/schema/data/jpa
		http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
	
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<context:annotation-config />
	<context:component-scan base-package="com.tiamaes.**">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="driverClass" value="${dataSource.className}" />
		<property name="jdbcUrl" value="${dataSource.url}" />
		<property name="user" value="${dataSource.username}" />
		<property name="password" value="${dataSource.password}" />
		
		<!-- 连接关闭时默认将所有未提交的操作回滚。Default: false -->
		<property name="autoCommitOnClose" value="true" />
		<!-- 当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒。Default: 0--> 
		<property name="checkoutTimeout" value="${cpool.checkoutTimeout}" />
		<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
		<property name="initialPoolSize" value="${cpool.minPoolSize}" />
		<!-- 连接池中保留的最小连接数 -->
		<property name="minPoolSize" value="${cpool.minPoolSize}" />
		<!-- 连接池中保留的最大连接数。Default: 15 -->
		<property name="maxPoolSize" value="${cpool.maxPoolSize}" />
		<!-- 最大空闲时间,36000秒(10小时)内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
		<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
		<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}" />
		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
		<property name="acquireIncrement" value="${cpool.acquireIncrement}" />
		<!-- 定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
        <property name="acquireRetryAttempts" value="${cpool.acquireRetryAttempts}" />
        <!-- 两次连接中间隔时间,单位毫秒。Default: 1000 -->
        <property name="acquireRetryDelay" value="${cpool.acquireRetryDelay}" />
	</bean>
	
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="persistenceUnitName" value="Oracle" />
		<!-- <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" /> -->
		<property name="persistenceProvider" ref="persistenceProvider" />
		<property name="jpaDialect" ref="jpaDialect" />
		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
		<property name="packagesToScan" value="com.tiamaes.gjds.dxp.bean"/>
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.temp.use_jdbc_metadata_defaults">${hibernate.temp.use_jdbc_metadata_defaults}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
				<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
				<prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
				<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
			</props>
		</property>
	</bean>
	
	<!-- <bean class="org.hibernate.jpa.HibernatePersistenceProvider" /> -->
	<bean id="persistenceProvider" class="org.hibernate.jpa.HibernatePersistenceProvider" />
	<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
	<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="database" value="ORACLE"></property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>
	
	<!-- 事务装配 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="query*" propagation="NOT_SUPPORTED"/>
			<tx:method name="get*" propagation="NOT_SUPPORTED"/>
			<tx:method name="find*" propagation="NOT_SUPPORTED"/>
			<tx:method name="*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut expression="execution(* com.tiamaes.**.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>
	
	<tx:annotation-driven transaction-manager="transactionManager" />

	<aop:aspectj-autoproxy />
	
	<jpa:repositories base-package="com.tiamaes.gjds.dxp.repository" 
		entity-manager-factory-ref="entityManagerFactory"
		transaction-manager-ref="transactionManager"/>
	
	<task:annotation-driven />
	
	<bean id="javascriptProcesser" class="com.tiamaes.gjds.script.JavascriptProcesser" />
	
	<bean id="tiamaesMailSender" class="com.tiamaes.gjds.dxp.sender.mail.TiamaesMailSender" />
	
</beans>

jdbc.properties

dataSource.className=oracle.jdbc.OracleDriver
dataSource.url=jdbc\:oracle\:thin\:@//192.168.13.111\:1521/gongjiaodushi
dataSource.username=GJDS
dataSource.password=GJDS

# Time to wait for an open connection before timing out
# (in milliseconds)
cpool.checkoutTimeout=5000

# Connection pool size
cpool.minPoolSize=5
cpool.maxPoolSize=20

# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8 hours(28,800 seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=36000

# How long to hang on to excess unused connections after traffic spike
# (in seconds)
cpool.maxIdleTimeExcessConnections=1800

# Acquiring new connections is slow, so eagerly retrieve extra connections
# when current pool size is reached
cpool.acquireIncrement=5

cpool.acquireRetryAttempts=0
cpool.acquireRetryDelay=1000


hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.temp.use_jdbc_metadata_defaults=false
hibernate.show_sql=false
hibernate.format_sql=false
hibernate.max_fetch_depth=3
hibernate.jdbc.fetch_size=18
hibernate.jdbc.batch_size=10



SpringMVC+JPA+SpringData配置

标签:java   spring   spring mvc   

原文地址:http://blog.csdn.net/jaune161/article/details/44014937

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!