Spring beans configuration

I am using Spring dependency injection, but I am having difficulty loading the resource in the Spring configuration file.

A resource is an XML file and is located in a JAR file in my class path. I am trying to access it as follows:

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

however, I continue to encounter the following error:

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

The JAR file is on the way to the classes of the Java project, which in turn is used by my web application. Do I really have to do my Spring configuration in a web project, as opposed to a Java project, or does it matter?

+5
source share
4 answers

-, JAR, , WEB-INF/lib.

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>

WebApplicationContextUtils, , :

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

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);
}

, , . , . , .

+1

, , () (:) classpath:/ , (classpath: *), , .

0
source

I used the directive <import>in J2SE and it works without a prefix classpath:, just like <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 do not need to do this, because in web.xml you can define several files in the context parameter contextConfigLocationinside web.xml, just separate them with spaces or newlines.

0
source

All Articles