Spring web开发配置模板

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:context="http://www.springframework.org/schema/context" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/aop 
       	    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>${spring.conf.path}/database.properties</value>
                <value>${spring.conf.path}/mail.properties</value>
            </list>
        </property>
    </bean>
    
    <context:component-scan base-package="com.ichaoying"/>

    <context:annotation-config/>
    
    <aop:aspectj-autoproxy/>
    
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    <bean id="mainDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${database.driverClassName}"/>
        <property name="jdbcUrl" value="${database.org.url}"/>
        <property name="user" value="${database.org.username}"/>
        <property name="password" value="${database.org.password}"/>
        <property name="initialPoolSize" value="${pool.initialPoolSize}"/>
        <property name="minPoolSize" value="${pool.minPoolSize}"/>
        <property name="maxPoolSize" value="${pool.maxPoolSize}"/>
        <property name="maxIdleTime" value="${pool.maxIdleTime}"/>
        <property name="acquireIncrement" value="${pool.acquireIncrement}"/>
    </bean>

	<!--hibernate -->
	<bean id="mainSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="mainDataSource" />
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.ichaoying.*.domain</value>
			</list>
		</property>
		<property name="configLocation">
			<value>/WEB-INF/conf/hibernate-mysql.xml</value>
		</property>
	</bean>
	
	<!-- hibernate template -->
	<bean id="mainHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="mainSessionFactory" />
	</bean>
	


	<!-- transaction -->
	<bean id="mainTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="mainHibernateTemplate" />
		</property>
		<property name="nestedTransactionAllowed">
			<value>true</value>
		</property>
	</bean>
	

	<tx:annotation-driven transaction-manager="mainTransactionManager" />

	<aop:config>
		<aop:pointcut id="asPointcut" expression="execution(* com.ichaoying.xxx.service.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="asPointcut" />
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="mainTransactionManager">
		<tx:attributes>
			<tx:method name="*" read-only="true" />
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="create*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="remove*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="accept*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="reject*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="execute*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="sync*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
</beans>


  • mysql配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
		<property name="show_sql">false</property>
		<property name="format_sql">false</property>
		<property name="jdbc.batch_size">50</property>
		<property name="hibernate.connection.SetBigStringTryClob">true</property>
		<property name="hibernate.archive.autodetection">class</property>
		<property name="hibernate.connection.charSet">UTF-8</property>
		<property name="hibernate.cache.use_query_cache">false</property>
		<property name="hibernate.cache.use_second_level_cache">false</property>
		<property name="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</property>
	</session-factory>
</hibernate-configuration>
  • 数据库配置
database.driverClassName=com.mysql.jdbc.Driver

database.url=jdbc:mysql://localhost:3306/dbname?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
database.username=q3boy
database.password=123

pool.initialPoolSize=0
pool.minPoolSize=0
pool.maxPoolSize=5
pool.maxIdleTime=120
pool.acquireIncrement=1
  • web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>xxxxx</display-name>
	<description>xxxx</description>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml,classpath:applicationContext-mail.xml,classpath:applicationContext-core.xml,classpath:applicationContext-hr.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/conf/log4j.xml</param-value>
	</context-param>
	<!-- 项目使用的配置文件位置.项目启动自动读取 -->
	<context-param>
		<param-name>propertiesConfigLocation</param-name>
		<param-value>/WEB-INF/conf/config.properties</param-value>
	</context-param>
	<listener>
		<listener-class>com.ichaoying.web.WebContextListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>com.ichaoying.hr.web.util.HrWebContextListener</listener-class>
	</listener>
	<filter>
		<filter-name>sitemesh</filter-name>
		<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
	</filter>
	<filter>
		<filter-name>SetCharacterEncoding</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>
		<filter-name>entityFilter</filter-name>
		<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>sitemesh</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>
	<filter-mapping>
		<filter-name>SetCharacterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>entityFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>com.ichaoying.web.MtDispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:webmvc-config-core.xml,classpath:webmvc-config.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet>
		<servlet-name>sitemesh-freemarker</servlet-name>
		<servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
		<init-param>
			<param-name>TemplatePath</param-name>
			<param-value>/</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>sitemesh-freemarker</servlet-name>
		<url-pattern>*.ftl</url-pattern>
	</servlet-mapping>
</web-app>
  • spring mvc 配置
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="com.ichaoying.**.web" />

	<mvc:annotation-driven />

	<mvc:resources mapping="/static/**" location="/static/" />
	<mvc:resources mapping="/js/**" location="/js/" />
	<mvc:resources mapping="/css/**" location="/css/" />
	<mvc:resources mapping="/resources/**" location="/resources/" />

	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/plain;charset=UTF-8</value>
						</list>
					</property>
				</bean>
				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> 
			</list>
		</property>
	</bean>
<!--
    <bean id="exceptionResolver" class="com.ichaoying.web.MtMappingExceptionResolver">
        <property name="defaultErrorView" value="error/uncaughtException"/>
        <property name="exceptionMappings">
            <props>
                <prop key=".NoURIFoundException">error/pageNotFound</prop>
                <prop key=".DataNotFoundException">error/dataNotFound</prop>
                <prop key=".FileNotFoundException">error/fileNotFound</prop>
                <prop key=".AccessDeniedException">error/accessDenied</prop>
                <prop key="java.lang.Exception">error/uncaughtException</prop>
                <prop key=".ConstraintViolationException">error/validateException</prop>
                <prop key=".MOMAException">error/MOMAException</prop>
            </props>
        </property>
    </bean>
-->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
		<property name="templateLoaderPath" value="/WEB-INF/views/" />
		<property name="freemarkerSettings">
			<props>
				<prop key="template_update_delay">0</prop>
				<prop key="default_encoding">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
				<prop key="url_escaping_charset">UTF-8</prop>
				<prop key="whitespace_stripping">true</prop>
			</props>
		</property>
	</bean>

	<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="contentType" value="text/html; charset=UTF-8" />
		<property name="cache" value="true" />
		<property name="prefix" value="" />
		<property name="suffix" value=".ftl" />
		<property name="exposeSpringMacroHelpers" value="true" />
		<property name="exposeRequestAttributes" value="true" />
		<property name="exposeSessionAttributes" value="true" />
		<property name="requestContextAttribute" value="request" />
	</bean>

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
  • log4j配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

	<!-- Appenders -->
	<appender name="console" class="org.apache.log4j.ConsoleAppender">
		<param name="Target" value="System.out" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %5p (%F:%L) - %m%n" />
		</layout>
	</appender>
	<appender name="logfile" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="logs/web.log" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p (%F:%L) - %m%n" />
		</layout>
	</appender>
	<appender name="errfile" class="org.apache.log4j.DailyRollingFileAppender">
		<param name="File" value="logs/weberr.log" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d %-5p (%F:%L) - %m%n" />
		</layout>
	</appender>

	<logger name="com.ichaoying.web" additivity="false">
		<priority value="warn" />
		<appender-ref ref="console" />
		<appender-ref ref="errfile" />
	</logger>

	<!-- Application Loggers -->
	<logger name="com.ichaoying">
		<level value="debug" />
	</logger>

	<!-- 3rdparty Loggers -->
	<logger name="org.springframework.core">
		<level value="info" />
	</logger>

	<logger name="org.springframework.beans">
		<level value="info" />
	</logger>

	<logger name="org.springframework.context">
		<level value="info" />
	</logger>

	<logger name="org.springframework.web">
		<level value="info" />
	</logger>

	<!-- Root Logger -->
	<root>
		<priority value="warn" />
		<appender-ref ref="console" />
		<appender-ref ref="logfile" />
	</root>

</log4j:configuration>

你可能感兴趣的:(Spring web开发配置模板)