Spring从项目中导入扩展和实现类

时间:2014-09-17 20:59:52

标签: java eclipse spring maven spring-mvc

我有两个(web)项目,第二个使用第一个扩展类。我甚至不知道我的项目设计是否正确,但事实是我在服务器启动时遇到错误,我不知道是否存在设计错误:

- ProjectBird

package com.mycompany.dao;

public interface BirdDao{
     public void saveFly(Fly fly);
}

package com.mycompany.dao.impl;

public class BirdDaoImpl implements BirdDao{
   public BirdDaoImpl(){
   }
   public void saveFly(Fly fly){
       System.out.println("I'm saving flight data...");
   }
}

- ProjectFalcon

package com.mycompany.myunit.dao;

public interface FalconDao {
  public void saveHunt(Hunt hunt);
}

package com.mycompany.myunit.dao.impl;

import com.mycompany.dao.impl.BirdDaoImpl;

public class FalconDaoImpl extends BirdDaoImpl implements FalconDao{
  public FalconDaoImpl(){
  }

  public void saveHunt(Hunt hunt){
     System.out.println("I'm saving hunt data... poor prey!");
  }
}

当我尝试在服务器上运行此模式时,Spring引发了一个异常:

平台:Eclipse Luna 4.4 Spring Tool Suite 3.6.1

Severe: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.mycompany.myunit.dao.impl.FalconDaoImpl; nested exception is java.io.FileNotFoundException: class path resource [com/mycompany/dao/impl/BirdDaoImpl.class] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:162)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:305)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:241)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1079)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:1002)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:506)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.io.FileNotFoundException: class path resource [com/mycompany/dao/impl/BirdDaoImpl.class] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:50)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:82)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:77)
    at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:564)
    at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:740)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:287)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:218)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:176)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:158)
    ... 39 more

是否存在设计错误或者是Eclipse上的配置错误?

  • ProjectBird列于POM
  • 在设计期间Eclipse上没有错误或问题

谢谢!

1 个答案:

答案 0 :(得分:0)

事实上,这种设计效果不佳......

如果你没有在某些配置中指出项目之间的依赖关系,它将无法工作,即便如此,建立两个web项目之间的依赖声音也不好......

如果您需要重复使用一个类或扩展其他项目使用的类,则此方案非常适合将该类/类作为依赖项包含在一个jar中,并在两个项目中导入该唯一依赖项。

更明显的例子是当你有服务层和web层时,我们可以将web层(http控制器和视图)放在一个项目中,并将服务层(服务接口和实现)放在另一个项目中,并建立一个Web项目和服务项目之间的依赖关系,可以通过将jar作为依赖项导入到Web项目中,或者如果我们使用maven作为依赖项将其声明为依赖关系。