0

In my 'ServicesProject' I have:

  • serv-applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <import resource="classpath:repo-applicationContext.xml" />
        <context:annotation-config />
        <bean id="myAppService" class="com.myapp.services.MyServiceImpl"/>
    </beans>
    
  • In pom.xml, import dependency from RepositoryProject

  • web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/serv-applicationContext.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>
           org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
          org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    
    <servlet>
        <servlet-name>RestServlet</servlet-name>
        <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/serv-applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

In my 'RepositoryProject':

  • repo-applicationContext.xml is in: src/main/webapp/META-INF/spring
  • web.xml of RepositoryProject:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/META-INF/spring/repo-applicationContext.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>
          org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <listener>
        <listener-class>
           org.springframework.web.context.request.RequestContextListener
        </listener-class>
     </listener>
    

EDIT: RepositoryProject is a library:

<packaging>jar</packaging>

When I started Tomcat I have this error:

Configuration problem: Failed to import bean definitions from URL location [classpath:repo-applicationContext.xml]

  1. I start "ServicesProject"
  2. repo-applicationContext.xml is in: target/ServicesProject/WEB-INF/lib/repoproject.jar/repo-applicationContext.xml. The pom.xml of RepositoryProject:
<groupId>com.myapp.repository</groupId>
  <artifactId>RepositoryProject</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RepositoryProject</name>
  <url>http://maven.apache.org</url>

  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <springframework.version>5.0.1.RELEASE</springframework.version>
      <java.version>1.8</java.version>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
    <dependencies> <!--SPRING, MYSQL, HIBERNATE --> </dependencies>
    <build>
...
        <resource>
              <directory>src/main/resources/spring/</directory>
              <filtering>true</filtering>
          </resource>
          <resource>
              <directory>src/main/resources/hibernate/</directory>
              <filtering>true</filtering>
          </resource>
...
</build>

I tried: a) with classpath*:repo-applicationContext.xml I have:

Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myAppService': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.myapp.services.MyServiceImpl] from ClassLoader [ParallelWebappClassLoader context: ServicesProject delegate: false." MyServiceImpl uses a bean defined in repo-applicationContext.

b) with classpath:repo-applicationContext.xml

java.io.FileNotFoundException

EDIT 2: Now I have this error:

Exception encountered during context initialization - 
cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myAppService': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: 
Failed to introspect Class [com.myapp.services.MyServiceImpl] from ClassLoader [ParallelWebappClassLoader
  context: ServicesProject
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@6979e8cb
]
nov 20, 2017 10:54:55 PM org.springframework.cache.ehcache.EhCacheManagerFactoryBean destroy
INFORMAZIONI: Shutting down EhCache CacheManager
nov 20, 2017 10:54:55 PM org.springframework.web.context.ContextLoader initWebApplicationContext
GRAVE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'myAppService': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: 
Failed to introspect Class [com.myapp.services.MyServiceImpl] from ClassLoader [ParallelWebappClassLoader
  context: ServicesProject
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@6979e8cb
]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:262)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:409)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:291)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4745)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Failed to introspect Class 
[com.myapp.services.MyServiceImpl] from ClassLoader [ParallelWebappClassLoader
  context: ServicesProject
  delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@6979e8cb
]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:541)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:245)
    ... 23 more
Caused by: java.lang.NoClassDefFoundError: com/myapp/repository/exception/RepositoryException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641)
    ... 26 more
Caused by: java.lang.ClassNotFoundException: com.myapp.repository.exception.RepositoryException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
    ... 30 more
fuerteVentura22
  • 93
  • 1
  • 2
  • 13
  • Try `classpath*:...` – Stewart Nov 20 '17 at 10:56
  • @Stewart why `classpath*` helps? – michaldo Nov 20 '17 at 11:10
  • https://stackoverflow.com/questions/3294423/spring-classpath-prefix-difference – Stewart Nov 20 '17 at 11:11
  • Please clarify your issue: 1. Which project do you start. 2. Why import failed 3. Where in target application is located `repo-ApplicationContext.xml` – michaldo Nov 20 '17 at 11:11
  • https://stackoverflow.com/questions/13994840/what-is-the-difference-between-classpath-and-classpath-in-spring-xml – Stewart Nov 20 '17 at 11:11
  • @michaldo I've edited the post – fuerteVentura22 Nov 20 '17 at 18:43
  • I don't see any reason why it happems: your config looks similar to https://stackoverflow.com/questions/5113579/how-to-import-spring-config-xml-of-one-project-into-spring-config-xml-of-another. I think issue will be easy to solve if you share minimal project to reproduce – michaldo Nov 20 '17 at 20:07
  • @michaldo now I have another error, see the updated post – fuerteVentura22 Nov 20 '17 at 22:01
  • `ClassNotFoundException` is usually easy to solve; add missing class to application – michaldo Nov 21 '17 at 08:05
  • That class (com.myapp.repository.exception.RepositoryException) is in RepositoryProject (imported in pom.xml). Where should I add it? – fuerteVentura22 Nov 21 '17 at 08:51
  • Spring configuration seems be OK. It looks you have a problem with assembly WAR - RepositoryProject content cannot be found. In my opinion assembly WAR is rather trivial. If you need help, please provide minimal example project – michaldo Nov 22 '17 at 08:11

0 Answers0