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">
&lt;context:component-scan base-package="com.ichaoying.**.web" /&gt;

&lt;mvc:annotation-driven /&gt;

&lt;mvc:resources mapping="/static/**" location="/static/" /&gt;
&lt;mvc:resources mapping="/js/**" location="/js/" /&gt;
&lt;mvc:resources mapping="/css/**" location="/css/" /&gt;
&lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;

&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
	&lt;property name="messageConverters"&gt;
		&lt;list&gt;
			&lt;bean class="org.springframework.http.converter.StringHttpMessageConverter"&gt;
				&lt;property name="supportedMediaTypes"&gt;
					&lt;list&gt;
						&lt;value&gt;text/plain;charset=UTF-8&lt;/value&gt;
					&lt;/list&gt;
				&lt;/property&gt;
			&lt;/bean&gt;
			&lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt;&lt;/bean&gt; 
		&lt;/list&gt;
	&lt;/property&gt;
&lt;/bean&gt;

<!–
<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>

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

&lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /&gt;

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

&lt;logger name="com.ichaoying.web" additivity="false"&gt;
	&lt;priority value="warn" /&gt;
	&lt;appender-ref ref="console" /&gt;
	&lt;appender-ref ref="errfile" /&gt;
&lt;/logger&gt;

&lt;!-- Application Loggers --&gt;
&lt;logger name="com.ichaoying"&gt;
	&lt;level value="debug" /&gt;
&lt;/logger&gt;

&lt;!-- 3rdparty Loggers --&gt;
&lt;logger name="org.springframework.core"&gt;
	&lt;level value="info" /&gt;
&lt;/logger&gt;

&lt;logger name="org.springframework.beans"&gt;
	&lt;level value="info" /&gt;
&lt;/logger&gt;

&lt;logger name="org.springframework.context"&gt;
	&lt;level value="info" /&gt;
&lt;/logger&gt;

&lt;logger name="org.springframework.web"&gt;
	&lt;level value="info" /&gt;
&lt;/logger&gt;

&lt;!-- Root Logger --&gt;
&lt;root&gt;
	&lt;priority value="warn" /&gt;
	&lt;appender-ref ref="console" /&gt;
	&lt;appender-ref ref="logfile" /&gt;
&lt;/root&gt;

</log4j:configuration>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值