简体   繁体   English

春豆配置

[英]spring beans configuration

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. 资源是XML文件,位于类路径的JAR文件中。 I try to access it as follows: 我尝试按如下方式访问它:

<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. JAR文件位于Java项目的类路径中,而该项目又由我的Web应用程序使用。 Should I really be doing my Spring configuration in the web project as opposed the Java project, or does that matter? 我应该真的在Web项目中进行Spring配置而不是Java项目,还是重要?

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. 如果它需要在您的webapp的类路径中,那么您应该将包含配置文件的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: 如果您使用的是webapp,那么常见的约定是使用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 WebApplicationContextUtils to fish the application context out of the servlet context using: 然后使用WebApplicationContextUtils使用以下命令从servlet上下文中捕获应用程序上下文:

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

I ran into a similar issue with a red5 plugin. 我用red5插件遇到了类似的问题。 I resolved it like so: 我这样解决了:

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. 这将首先查看类路径中的任何位置,包括在包含我的代码的jar中。 If an exception occurs the plugin directory is checked. 如果发生异常,则检查插件目录。 It may not be the best solution but it works. 它可能不是最好的解决方案,但它有效。

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

I've only used the <import> directive in J2SE and it works without the classpath: prefix, simply as <import resource="config/resources.xml" /> . 我只在J2SE中使用了<import>指令,它没有classpath:前缀,只是作为<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 contextConfigLocation context parameter inside web.xml, just separate them with spaces or newlines. 但是在J2EE中,如果所有文件都在WEB-INF中,它应该是相似的,只需导入resource =“bla.xml”它应该找到它,虽然在J2EE中你不需要这样做,因为在web.xml中你可以在web.xml中的contextConfigLocation上下文参数中定义多个文件,只需用空格或换行符分隔它们即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM