java 春豆配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:48:43  来源:igfitidea点击:

spring beans configuration

javaspring

提问by cduggan

I'm using Spring's dependency injection but I'm running into difficulty loading a resource in my Spring config file.

我正在使用 Spring 的依赖注入,但在 Spring 配置文件中加载资源时遇到了困难。

The resource is an XML file and is in a JAR file on my classpath. I try to access it as follows:

该资源是一个 XML 文件,位于我的类路径上的 JAR 文件中。我尝试按如下方式访问它:

<import resource="classpath:com/config/resources.xml" />

however I keep getting encountering the following error:

但是我不断遇到以下错误:

Failed to import bean definitions from URL location [classpath:com/config/resources.xml]

无法从 URL 位置导入 bean 定义 [classpath:com/config/resources.xml]

The JAR file is on the classpath of a Java project, which is in turn used by my web app. Should I really be doing my Spring configuration in the web project as opposed the Java project, or does that matter?

JAR 文件位于 Java 项目的类路径中,我的 Web 应用程序反过来使用该类路径。我真的应该在 Web 项目中而不是在 Java 项目中进行 Spring 配置,还是这有关系?

回答by toolkit

If it needs to be in the classpath of your webapp, then you should stick the JAR containing the config file into your WEB-INF/lib directory.

如果它需要在您的 web 应用程序的类路径中,那么您应该将包含配置文件的 JAR 粘贴到您的 WEB-INF/lib 目录中。

If you're using a webapp, then the common convention is use a ContextLoaderListener to ensure a WebApplicationContext is inserted into a standard place in the ServletContext:

如果您使用的是 web 应用程序,那么常见的约定是使用 ContextLoaderListener 来确保将 WebApplicationContext 插入到 ServletContext 中的标准位置:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/com/config/resources.xml</param-value>
</context-param>

Then use WebApplicationContextUtilsto fish the application context out of the servlet context using:

然后使用WebApplicationContextUtils使用以下方法从 servlet 上下文中获取应用程序上下文:

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

回答by Paul Gregtheitroade

I ran into a similar issue with a red5 plugin. I resolved it like so:

我在使用 red5 插件时遇到了类似的问题。我是这样解决的:

try {
  subContext = new FileSystemXmlApplicationContext(new String[] { "classpath*:/myconfig.xml" }, true, context);
} catch (Exception fnfe) {
  subContext = new FileSystemXmlApplicationContext(new String[] { "plugins/myconfig.xml" }, true, context);
}

This will look anywhere on the classpath first, including within the jar that contains my code. If an exception occurs the plugin directory is checked. It may not be the best solution but it works.

这将首先查找类路径上的任何位置,包括包含我的代码的 jar 内。如果发生异常,则检查插件目录。它可能不是最好的解决方案,但它有效。

回答by MetroidFan2002

I don't really recall why this matters, but try putting an asterisk () in front of the colon (:) classpath:/ If this doesn't work, try the asterisk after the colon (classpath:*), although I think it was before the colon.

我真的不记得为什么这很重要,但是尝试在冒号 (:) 类路径前面放一个星号 ( ):/ 如果这不起作用,请尝试在冒号 (classpath:*) 后面加星号它在冒号之前。

回答by Chochos

I've only used the <import>directive in J2SE and it works without the classpath:prefix, simply as <import resource="config/resources.xml" />. But in J2EE if all your files are inside WEB-INF, it should be similar, just import resource="bla.xml" and it should find it, although in J2EE you don't need to do this because in web.xml you can define several files in the contextConfigLocationcontext parameter inside web.xml, just separate them with spaces or newlines.

我只<import>在 J2SE 中使用过该指令,它在没有classpath:前缀的情况下工作,只是作为<import resource="config/resources.xml" />. 但是在 J2EE 中,如果您的所有文件都在 WEB-INF 中,它应该是类似的,只需导入 resource="bla.xml" 就可以找到它,尽管在 J2EE 中您不需要这样做,因为在 web.xml 中您可以contextConfigLocation在 web.xml中的context 参数中定义多个文件,只需用空格或换行符分隔它们即可。