- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想覆盖上下文configLocation
web.xml如下
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>com.mypackage.MyDispacherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:default-ctx.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
然后是MyDispacherServlet
public class MyDispacherServlet extends org.springframework.web.servlet.DispatcherServlet {
@Override
public void init(ServletConfig config) throws ServletException {
// here will be code to find dynamically other-ctx.xml
String correctSpringXml = "classpath*:other-ctx.xml";
setContextConfigLocation(correctSpringXml) ;
super.init(config);
}
@Override
protected WebApplicationContext initWebApplicationContext() throws BeansException {
WebApplicationContext wac = super.initWebApplicationContext();
return wac;
}
但是这段代码不起作用。如何正确覆盖 contextConfigLocation?
最佳答案
看起来您需要仔细查看您尝试覆盖的 init
方法(在 HttpServletBean
中定义)。
//unimportent parts removed
@Override
public final void init() throws ServletException {
...
try {
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, this.environment));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {...}
...
// Let subclasses do whatever initialization they like.
initServletBean();
...
}
看起来contextConfigLocation
参数是由bw.setPropertyValues(pvs, true);
设置的
不同的解决思路:
您需要完全覆盖 init 方法(无需调用 super.init()
)。然后在调用 bw.setPropertyValues(pvs, true);
之前修改 pvs
(您如何执行此操作)。
或者在调用 super.initServletBean()
之前覆盖 initServletBean()
并修改那里的属性。
这是我首先要尝试的:或者您尝试覆盖 getServletConfig()
以便它返回您修改后的配置。
关于spring - 如何覆盖 dispatcherServlet 中的 contextConfigLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18098348/
在 web.xml 中,我们有名为 contextConfigLocation 的上下文参数集,定义如下面的代码所示: contextConfigLocation /WEB-INF/
我想覆盖上下文configLocation web.xml如下 appServlet com.mypackage.MyDispacherServlet co
我正在阅读 Spring MVC 的文档,我有一个关于 init 参数的问题。如果重要的话,我正在使用 Spring 3.2。 contextConfigLocation 和命名空间有什么区别? co
我无法理解我的项目中应用程序上下文实际发生的情况。我已经创建了使用 spring-mvc 的项目的新部分 有RestConfig类 @Configuration @EnableWebMvc @Comp
当我将占位符放在 web.xml 中时,它会被 catalina.properties 中定义的值或 vm 选项中的 -D 参数正确替换。但是,当我将占位符放入由 ContextLoaderListe
我有一个带有 Spring 的项目和一些服务。我正在尝试在其中包含一些休息服务。问题是嵌入式 jetty 服务器位于 @Service 实例内,由于多种原因我无法触及它。因此,我尝试根据我的新要求配置
这里的想法是从数据库加载一些 xml 来使用,但我需要它是一个在启动时加载一次的 bean。 xml 绝对必须位于数据库中。 如果这是重复的,我深表歉意,但我找不到任何详细说明此问题的信息。 最佳答案
检查了一些参数后,我需要修改项目中的web.xml。我在 web.xml 中的 contextConfigLocation 参数如下: contextConfigLocation cl
这是我的 web.xml 的一部分: contextConfigLocation classpath:application-config.
我们正在使用 JavaConfig,我正在尝试设置一个单独的应用程序上下文来解决 Jersey 1.7 中的一些问题(即它包含所有路径中用 @Controller 注释的所有 Spring beans
我正在通过 Spring MVC 中的 mongodb java 驱动程序连接到 MongoDB 分片服务器。我正在使用以下版本: spring-webmvc-3.2.1.RELEASE mongo-
假设我有一个 Spring Java 项目,并且我正在尝试将其配置为 Web 服务器 servlet。这是 web.xml 文件的精简版: contextConfigLocation
我有一个 web.xml 文件,其中包含一个定义 的 servlet(除其他外)。初始化参数 指定 上下文配置位置 ,但 参数值 是 空白 ? 开发商为什么要这么做。我一生都无法在 Spring 3.
我有一个像这样设置的 spring mvc 项目: appServlet org.springframework.web.servlet.DispatcherServlet
我是一名优秀的程序员,十分优秀!