ssm框架整合配置文件
Posted 每天一点积累
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm框架整合配置文件相关的知识,希望对你有一定的参考价值。
<?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> <!-- 自动扫描包,以便注册带有@Service、@repository、@Component等注解的类成为spring的bean --> <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"> <!-- 自动扫描包,以便注册带有@Controller注解的java类成spring的bean --> <!-- 为了让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框架整合配置文件的主要内容,如果未能解决你的问题,请参考以下文章