简体   繁体   中英

Spring importing extending and implementing class from a project

I have two (web) projects in ways that the second one uses the first extending a class. I don't even know if my project's design is right, but the fact is that I'm facing an error during server start, and I don't know if there's a design mistake:

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

When I try to run this schema on server, Spring raises an exception:

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

Is there a design error or would it be an configuration mistake on Eclipse?

  • ProjectBird is listed in POM
  • There's no errors or issues on Eclipse during design time

Thanks!

In fact the this design will not work well ...

If you have not indicated in some configuration the dependency between projects, it will not work, even so, establish dependency between two web projects sounds not good..

If you need re-use one class or extend one used by other project this scenario is perfect to enclose that class/classes into one jar as dependency and import that unique dependency in both projects.

The more clear example is when you have service layer and web layer, we can put the web layer (http controller and views) in one project and put the service layer (services interfaces and implementations) in other one, and establish a dependency relation between web project and the service project, it could be done by importing the jar as dependency in the web project or declaring it as dependency if we are using maven for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM