美文网首页
Spring MVC 配置文件路径问题

Spring MVC 配置文件路径问题

作者: ChanHsu | 来源:发表于2017-04-28 18:33 被阅读122次

1.classpath 和 classpath* 区别

1.classpath:只会到你的class路径中查找找文件;
2.classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.

2.applicationcontext.xml 和 dispatcher-servlet.xml区别

1.ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的、比如:aop,sessionFactory,对应系统级别的配置,作用范围是系统上下文;

2.dispatcher-servlet.xml是spring mvc里面的,控制器、拦截uri转发view,对应的是 controller 级别的配置,作用范围是控制层上下文。

使用@Value注解获取*.property,在applicationContext.xml中引入配置文件取到的值${mysql.username}而不是真正的值,需要在dispatcherServlet中引入:
在使用spring mvc时,实际上是两个spring容器:
1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件
org.springframework.web.servlet.DispatcherServlet
这里最终是使用WebApplicationContext parent =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 创建spring容器,代码在FrameworkServlet中

2,applicationContext.xml 是另外一个,也需要注入属性文件
org.springframework.web.context.ContextLoaderListener

在我们的service中可以拿到@Value注入的值,那是因为我们通常都会把获取属性文件定义在applicationContext.xml中,这样在 Controller中是取不到的,必须在dispatcher-servlet.xml 中把获取属性文件再定义一下

相关文章

网友评论

      本文标题:Spring MVC 配置文件路径问题

      本文链接:https://www.haomeiwen.com/subject/wojazttx.html