import application context xml from another project

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]

  • I start "ServicesProject"
  • 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
    
    链接地址: http://www.djcxy.com/p/65356.html

    上一篇: 当屏幕方向改变时,OnLoadFinished不会从LoaderManager接收到

    下一篇: 从另一个项目导入应用程序上下文xml