1. 程式人生 > >ssm框架整合配置文件

ssm框架整合配置文件

web-inf qwidget apps 連接 client undle style med ide

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd        
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd  
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

	<!-- 引入properties配置文件 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>

	<!-- 數據源:druid連接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

	</bean>

	<!-- 
      配置sqlSessionFactory 後面會用,就是創建一個sqlSessionFactory實例,其中property是set get方法後面跟的字段
      這部分配置可以看
      http://www.mybatis.org/spring/zh/factorybean.html    
     --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 指定要用的數據源 --> <property name="dataSource" ref="dataSource" /> <!-- 加載Mapper xml配置 --> <property name="mapperLocations"> <list> <value>classpath:com/xmal/dms/dao/mapper/*Mapper.xml</value> </list> </property> </bean> <!--
        配置要掃描的Mapper接口類

        mybatis中org.mybatis.spring.mapper.MapperScannerConfigurer去查找指定包下的mapper
        然後為每一個映射器創建一個MapperFactoryBean

        http://www.mybatis.org/spring/zh/mappers.html        

       仔細看spring bean註入方法結合autowired自動連接

     -->
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.xmal.dms.dao.mapper" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- 配置事務管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 采用註解方式來使用事務 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- 註意多機部署時,緩存同步問題 -->
	<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
		<property name="caches">
			<set>
				<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default" />
			</set>
		</property>
	</bean>
	<cache:annotation-driven cache-manager="cacheManager" />

	<!-- spring提供的訪問rest服務的接口 -->
	<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
	</bean>

	<!-- 自動掃描包,[email protected]
/* */@[email protected] --> <context:component-scan base-package="com.xmal.dms" annotation-config="true" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <!-- <context:exclude-filter type="regex" expression=".temp.*"/> --> </context:component-scan> </beans>

以上是applicationcontext.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

	<!-- 自動掃描包,[email protected] -->
	<!-- 為了讓spring掃描不到Controller -->
	<context:component-scan base-package="com.xmal.dms"
		annotation-config="true" use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

     <-- 
      這篇文章很好:
      https://my.oschina.net/lichhao/blog/172562
      http://www.baeldung.com/spring-httpmessageconverter-rest

      消息轉換器是為了將Http超文本傳輸字符串轉換成為Java對象,將Java對象轉換為超文本傳輸協議的字符串
      
      ByteArrayHttpMessageConverter,將字符串轉化為ByteArray,具體可以看API文檔

      http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/StringHttpMessageConverter.html


     --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <bean class="org.springframework.http.MediaType"> <constructor-arg index="0" value="text" /> <constructor-arg index="1" value="plain" /> <constructor-arg index="2" value="UTF-8" /> </bean> </list> </property> </bean> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /> </list> </property> <property name="webBindingInitializer"> <bean class="com.xmal.dms.controller.util.DateConverter" /> </property> </bean> <!-- 默認的註解映射的支持,註冊請求url和controller類方法的映射 會自動註冊 DefaultAnnotationHandlerMapping(order屬性值為0)與AnnotationMethodHandlerAdapter兩個bean --> <!--<mvc:annotation-driven/> --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> </bean> <!-- 攔截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.xmal.dms.interceptor.AuthenticationInterceptor" /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**" /> <bean class="com.xmal.dms.interceptor.AuthorityInterceptor" /> </mvc:interceptor> </mvc:interceptors> <!-- 集成Hibernate Validator --> <mvc:annotation-driven validator="validator" /> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator" /> <!--不設置則默認為classpath下的ValidationMessages.properties --> <property name="validationMessageSource" ref="validatemessageSource" /> </bean> <bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:ValidationMessages_zh_CN</value> <value>classpath:validation-messages</value> </list> </property> <property name="fileEncodings" value="utf-8" /> <property name="cacheSeconds" value="120" /> </bean> <!-- 集成Hibernate Validator end--> <!-- 處理靜態資源方法2 自動註冊 SimpleUrlHandlerMapping的order屬性值是: 2147483646 <mvc:resources mapping="/jqwidgets/**" location="/resources/jqwidgets/" /> <mvc:resources mapping="/bootstrap/**" location="/resources/bootstrap/" /> <mvc:resources mapping="/js/**" location="/resources/js/" /> <mvc:resources mapping="/css/**" location="/resources/css/" /> <mvc:resources mapping="/images/**" location="/resources/images/" /> <mvc:resources mapping="/plugins/**" location="/resources/plugins/" /> //用戶上傳的圖片,文件等均放於該文件夾 <mvc:resources mapping="/file/**" location="/resources/uploads/" /> --> <!-- 處理靜態資源方法3 自動註冊 SimpleUrlHandlerMapping的order屬性值是: 2147483647 --> <!--<mvc:default-servlet-handler/> --> <!-- ViewResolver InternalResourceViewResolver:用於支持Servlet、JSP視圖解析 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>

  以上是spring配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<!-- 順序不能錯,第一部分是context-param -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:applicationContext*.xml</param-value>
	</context-param>
	<!-- 第二部分是filter -->
	<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>
	</filter>
	<filter>
		<filter-name>DruidWebStatFilter</filter-name>
		<filter-class>com.alibaba.druid.support.http.WebStatFilter
		</filter-class>
		<init-param>
			<param-name>exclusions</param-name>
			<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
			</param-value>
		</init-param>
		<init-param>
			<param-name>principalCookieName</param-name>
			<param-value>account</param-value>
		</init-param>
		<init-param>
			<param-name>profileEnable</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- 第三部分是filter-mapping -->
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter-mapping>
		<filter-name>DruidWebStatFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 第四部分是listener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<!-- 第五部分是servlet -->
	<!-- Spring MVC核心Servlet -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:springmvc-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Druid 監控Servlet -->
	<servlet>
		<servlet-name>DruidStatView</servlet-name>
		<servlet-class>com.alibaba.druid.support.http.StatViewServlet
		</servlet-class>
		<init-param>
			<param-name>allow</param-name>
			<param-value>127.0.0.1,127.0.0.2</param-value>
		</init-param>
		<init-param>
			<param-name>deny</param-name>
			<param-value>127.0.0.3,127.0.0.4</param-value>
		</init-param>
		<init-param>
			<param-name>resetEnable</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	<!-- CXF WebService核心Servlet -->
	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
	</servlet>	

	<servlet>
		<description></description>
		<display-name>TestTask</display-name>
		<servlet-name>TestTask</servlet-name>
		<servlet-class>com.xmal.javafw.task.TestTask</servlet-class>
	</servlet>
	
	<!-- 第六部分是servlet-mapping -->
	<!-- 處理靜態資源方法1  靜態資源不被spring mvc攔截  http://blog.csdn.net/hello5orld/article/details/9407905    -->
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.jpg</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.png</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.gif</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.ttf</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.woff</url-pattern>
	</servlet-mapping>

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/index</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>DruidStatView</servlet-name>
		<url-pattern>/druid/*</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/soap/*</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>TestTask</servlet-name>
		<url-pattern>/TestTask</url-pattern>
	</servlet-mapping>	
	
	<!-- 第七部分是welcome-file-list -->
	<welcome-file-list>
		<welcome-file>index</welcome-file>
	</welcome-file-list>
	<!-- 第八部分是 error-page-->
	<error-page>
		<error-code>404</error-code>
		<location>/WEB-INF/views/error/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/WEB-INF/views/error/error.jsp</location>
	</error-page>
	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/WEB-INF/views/error/error.jsp</location>
	</error-page>
</web-app>

  

ssm框架整合配置文件