applicationcontext.xml和spring-servlet.xml怎么配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了applicationcontext.xml和spring-servlet.xml怎么配置相关的知识,希望对你有一定的参考价值。
spring-servlet.xml是说springmvc吗?
这些都需要看你具体用到了什么来进行具体配置的.
给你一个参考吧:
spring-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
default-lazy-init="true">
<!-- enable aop -->
<aop:aspectj-autoproxy />
<!-- enable autowire -->
<context:annotation-config />
<context:component-scan base-package="info.kkdo" />
<!-- 启动对@AspectJ注解的支持 -->
<!--通知spring使用cglib而不是jdk的来生成代理方法 AOP可以拦截到Controller -->
<!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
<!-- 注解支持 -->
<!-- <context:annotation-config/> -->
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/json;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven>
<!-- 处理responseBody 里面日期类型 -->
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
<!-- 这个移动到spring-application中 -->
<!-- <import resource="spring-mvc-shiro.xml" /> -->
<!-- 开启了spring的监听器,这里就关闭,否则bean会创建两次 -->
<!-- <import resource="spring-application.xml" /> -->
</beans>
application.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:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
default-lazy-init="false">
<!-- 定时器开关 开始 -->
<task:annotation-driven />
<!-- 统一异常处理方式 ,在Controller中处理了-->
<!-- <bean id="exceptionHandler" class="info.kkdo.common.exception.MyExceptionHandler"/> -->
<!-- 初始化数据 -->
<!-- 引入spring-shiro配置文件 -->
<import resource="spring-shiro.xml" />
<import resource="spring-mybatis.xml"/>
<import resource="spring-task.xml"/>
<!-- 全局application -->
<!-- <bean id="springAcaHolder" class="com.kkdo.core.tools.support.SpringAcaHolder"></bean> -->
<!-- 使用Spring组件扫描的方式来实现自动注入bean -->
<context:component-scan base-package="info.kkdo" />
<!-- 隐式地向 Spring 容器注册 -->
<!-- <context:annotation-config /> -->
<!-- 用于读取配置文件的bean -->
<!-- <bean id="configurationLoader"
class="info.kkdo.common.config.ConfigurationLoader">
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean> -->
</beans> 参考技术A <context:component-scan base-package="com.ssm.express">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:annotation-config />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="$driver" />
<property name="url" value="$url" />
<property name="username" value="$username" />
<property name="password" value="$password" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="$initialSize"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="$maxActive"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="$maxIdle"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="$minIdle"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="$maxWait"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/ssm/express/dao/mapping/*.xml"></property>
<!-- 若不保留mybatis配置文件用上面那条替换这一条 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.express.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
spring 的
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --><context:component-scan base-package="com.ssm.express.controller" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
Springmvc的
applicationContext.xml和dispatcher-servlet.xml的区别
在SpringMVC项目中我们一般会引入applicationContext.xml和dispatcher-servlet.xml两个配置文件,这两个配置文件具体的区别是什么呢?
Spring 官方文档介绍如下:
Spring lets you define multiple contexts in a parent-child hierarchy.
The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.
The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet‘s app context.
There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml
for servlet spring2).
Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.
All Spring MVC controllers must go in the spring-servlet.xml context.
In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared
between all servlets in a webapp. If you only have one servlet, then there‘s not really much point, unless you have a specific use for it.
可见, applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文。
1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。
2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。
3)servlet context可以引用application context里的实例,反之不可以。
4)多个servlet共享application context里的实例
5)在applicationContext和dispatcher-servlet定义的bean最好不要重复,dispatcher-servlet最好只扫描@controler,applicationContext扫描其它
dispatcher-servlet.xml
<context:component-scan base-package="com.test.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
applicationContext.xml
<context:component-scan base-package="com.test" use-default-filters="true"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
注:
use-default-filters用来指示是否自动扫描带有@Component、@Repository、@Service和@Controller的类。默认为true,即默认扫描
<context:include-filter/>子标签是用来添加扫描注解
<context:exclude-filter/>子标签是用来排除扫描注解
----------------------------------------------------------------------------------------------------------------------------------------------
ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的
dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
以上是关于applicationcontext.xml和spring-servlet.xml怎么配置的主要内容,如果未能解决你的问题,请参考以下文章
springMVC-servlet.xml 和 applicationContext-*.xml的区别
hibernate.cfg.xml 和 applicationContext.xml的关系,
applicationContext.xml和web.xml的一些配置
Spring Framework中applicationContext.xml和spring-servlet.xml的区别
Spring Framework中applicationContext.xml和spring-servlet.xml的区别