Spring - 从另一个项目

时间:2017-11-19 14:03:21

标签: java xml spring maven

在我的“服务项目”中,我有:

  • 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>
    
  • 在pom.xml中,从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>
    

在我的“ RepositoryProject ”中:

  • repo-applicationContext.xml位于:src / main / webapp / META-INF / spring
  • RepositoryProject的web.xml:

    <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>
    

修改: RepositoryProject是一个库:

<packaging>jar</packaging>

当我启动Tomcat时出现此错误:

  

配置问题:无法从URL导入bean定义   location [classpath:repo-applicationContext.xml]

  1. 我启动“ServicesProject”
  2. repo-applicationContext.xml位于:target / ServicesProject / WEB-INF / lib / repoproject.jar / repo-applicationContext.xml。 RepositoryProject的pom.xml:
  3. <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>
    

    我试过:a)使用classpath *:repo-applicationContext.xml我有:

      

    上下文初始化失败   org.springframework.beans.factory.BeanCreationException:错误   创建名为'myAppService'的bean:查找方法解析   失败;嵌套异常是java.lang.IllegalStateException:失败   从ClassLoader中内省Class [com.myapp.services.MyServiceImpl]   [ParallelWebappClassLoader上下文:ServicesProject委托:false。“   MyServiceImpl使用repo-applicationContext中定义的bean。

    b)使用classpath:repo-applicationContext.xml

      

    java.io.FileNotFoundException

    编辑2: 现在我有这个错误:

    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
    

0 个答案:

没有答案