web.xml加载顺序及Spring包扫描注意

来源:互联网 发布:中国四大财阀 知乎 编辑:程序博客网 时间:2024/05/29 18:04

 

 一、web.xml文件中配置文件加载顺序web.xml文件中,我们一般会配置一些工程启动时需加载的配置文件.比如:SpringMVC工程开发时,  我们一般是会有两个xml的配置文件。一个上下文配置文件applicationContext.xml,另一个就是springMVC的配置文件servlet-context.xml文件.加载顺序: 1. 服务器启动时,首先会找web.xml文件,加载web.xml文件中配置文件; 2.找到 web.xml后,首先加载上下文配置文件;也就是<context-param></context- param>标签中初始化文件.其可用通配符的方式指定路径加载多个文件;比如:application*.xml. 3.加载监听器;<listener>...</listener> 4.加载过滤器;<filter>...</filter> 5.加载Servlet;<servlet></servlet>。比如SpringMVC的配置文件servlet-context.xml。 二. SpringMVC配置事务管理时,@Service,@Controller包文件扫描时配置注意事项: 1. 当我们在applicationContext.xml中添加了Spring的事务配置,而在servlet-context.xml中添加扫描@service包路径<context:component-scan base-package="**.*.service" />时,则当我们在Service中方法添加事务注解时,会发现事务没有起作用.而把<context:component-scan base-package="**.*.service" />放在和事务配置的同一个xml配置文件时, 就可以了.总的来说就是Service层要在Controller层先被扫描. 2. 当在applicationContext.xml文件中添加了扫描Service包的路径<context:component-scan base-package="com.cn.service.*" />时,          又同时在servlet-context.xml文件中添加扫描<context:component-scan base-package="com.cn.*" />时,Spring事务也不会起作用。因为SpringMVC中配置文件中配置会覆盖applicationContext.xml中内容.
原创粉丝点击