Spring Web框架搭建配置

配置web.xml文件:
在tomcat启动时加载web.xml文件。

 

<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>spring-web-demodisplay-name>

	
	<filter>
		<description>字符集编码过滤器description>
		<filter-name>encodingFilterfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		<init-param>
			<description>字符集编码description>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
	filter>
	<filter-mapping>
		<filter-name>encodingFilterfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
  	

	
	<context-param>
		<description>配置spring监听description>
		<param-name>contextConfigLocationparam-name>
		<param-value>
               classpath:spring-common.xmlparam-value>
	context-param>
    
	<listener>
		<description>spring监听器description>
		<listener-class>
            org.springframework.web.context.ContextLoaderListener
        listener-class>
	listener>
	<listener>
		<description>防止spring内存溢出的监听器description>
		<listener-class>
            org.springframework.web.util.IntrospectorCleanupListener
        listener-class>
	listener>
	
	
	
	<servlet>
		<description>SpringMVC分发器  DispatcherServletdescription>
		<servlet-name>SpringMVCservlet-name>
		<servlet-class>
             org.springframework.web.servlet.DispatcherServlet
        servlet-class>
		<init-param>
			<description>SpringMVC配置文件description>
			<param-name>contextConfigLocationparam-name>
			<param-value>
				classpath:spring-mvc.xml
			param-value>
		init-param>
		<load-on-startup>1load-on-startup>
	servlet>

	<servlet-mapping>
		<servlet-name>SpringMVCservlet-name>
		<url-pattern>*.dourl-pattern>
	servlet-mapping>
	

	<welcome-file-list>
		<welcome-file>index.jspwelcome-file>
	welcome-file-list>
	 
web-app>

配置spring-common.xml文件:
配置了properties资源文件的加载路径以及自动注入的扫描路径。


<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"
	xsi:schemaLocation="
		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">

	
	<bean id="propertyConfigurer" class="com.giser.config.CustomizedPropertyPlaceholderConfigurer">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath:datasource.propertiesvalue>
				<value>classpath:mysql-connection.propertiesvalue>
			list>
		property>
	bean> 

	
	<context:component-scan base-package="com.giser" />

beans>

配置datasource.properties文件:
主要用于配置数据库的连接信息,如驱动、方言、url、用户名、密码、心跳检测语句等。

jdbc_username=gis
jdbc_password=gis
jdbc_url=jdbc:oracle:thin:@localhost:1521:giser
jdbc_driver=oracle.jdbc.driver.OracleDriver
jdbc_dialect=org.hibernate.dialect.Oracle9Dialect
validationQuery=select 1 from dual

配置spring-mybatis.xml文件:
配置了数据源、事务,接口扫描路径等。


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />
		
		<property name="initialSize" value="3" />
		
		<property name="maxActive" value="20" />
		
		<property name="maxIdle" value="20" />
		
		<property name="minIdle" value="0" />
		
		<property name="maxWait" value="60000" />
		
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />
		
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		
		<property name="minEvictableIdleTimeMillis" value="25200000" />

		
		<property name="removeAbandoned" value="true" />
		
		<property name="removeAbandonedTimeout" value="1800" />
		
		<property name="logAbandoned" value="true" />

		
		
		<property name="filters" value="mergeStat" />
	bean>

	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		
		<property name="mapperLocations" value="classpath:mapper/*.xml" />
	bean>

	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.giser.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	bean>

	
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	bean>

	
	

	
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			
			<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="append*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		tx:attributes>
	tx:advice>
	
	<aop:config>
		<aop:pointcut id="transactionPointcut" expression="execution(* com.giser.service..*.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut"
			advice-ref="transactionAdvice" />
	aop:config>

beans>

配置spring-mvc.xml文件
主要配置了控制层组件扫描、视图解析器等。


<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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
		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 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	
	<context:component-scan base-package="com.giser" />
	
	
	<mvc:annotation-driven />
 
	
	<bean 	class="org.springframework.web.servlet.view.InternalResourceViewResolver"
			p:prefix="/WEB-INF/view/" 
			p:suffix=".jsp" />
			
	
	<bean id="mappingJacksonHttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8value>
			list>
		property>
	bean>

	
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" />
			list>
		property>
	bean>

	
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
			<value>UTF-8value>
		property>
		<property name="maxUploadSize">
			
			<value>32505856value>
		property>
		<property name="maxInMemorySize">
			<value>4096value>
		property>
	bean>

beans>

至此就OK了。

你可能感兴趣的:(#,Spring)