在Eclipse中调试时,Spring无法从共享的Gradle库(autowired bean中的依赖关系)中找到类 [英] Spring can't find class from shared Gradle library (dependency in autowired bean) when debugging in Eclipse

查看:385
本文介绍了在Eclipse中调试时,Spring无法从共享的Gradle库(autowired bean中的依赖关系)中找到类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Gradle的Spring MVC项目。我有一个网站项目( zeario-web ),网络爬虫( zeario-webcrawler )和共享类( zeario-sharedclientserverclasses )。网页和网页抓取工具共享一些类(例如DTO或VO)。

I have a Spring MVC project which is Gradle-based. I have a website project (zeario-web), web crawler (zeario-webcrawler), and shared classes (zeario-sharedclientserverclasses). The web and web crawler share some classes (something like DTOs or VOs).

在我的网络应用程序中,我有一个 CrawlerController 一个 getWorkUnit 方法;这调用了一个方法,一个 CrawlerService 实例( @Autowired depdency),反过来又使用一些类共享库。

In my web app, I have a CrawlerController with a getWorkUnit method; this calls a method on a CrawlerService instance (@Autowired depdency), which, in turn, uses some classes from the shared library.

我的问题是,我可以通过Gradle构建和部署我的Web应用程序的工作版本,但不能通过Eclipse。 Eclipse给我一个Spring运行时异常关于没有找到一个共享类,因此无法创建/注入服务实例。

My problem is that I can build and deploy a working version of my web app via Gradle, but not via Eclipse. Eclipse gives me a spring runtime exception about not finding one of the shared classes, and being therefore unable to create/inject the service instance.

从Tomcat日志中:

From the Tomcat logs:


org.springframework.beans.factory.BeanCreationException:创建名为crawlerController的bean时出错:自动连线依赖关系的注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:private com.zeario.service.CrawlerService com.zeario.api.v1.controller.CrawlerController.service;嵌套异常是java.lang.NoClassDefFoundError:com / zeario / api / v1 / model / workunit / PageDiscoveryWorkUnit

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zeario.service.CrawlerService com.zeario.api.v1.controller.CrawlerController.service; nested exception is java.lang.NoClassDefFoundError: com/zeario/api/v1/model/workunit/PageDiscoveryWorkUnit



代码片段



控制器定义(简化):

Code Snippets

Controller definition (simplified):

@Controller
public class CrawlerController {


    @Autowired
    private CrawlerService service;

    // ... methods with @RequestMapping ...
}

服务类(简化):

@Service
public class CrawlerService {

    public PageDiscoveryWorkUnit getPageDiscoveryWorkUnit() {
        return new PageDiscoveryWorkUnit();
    }

PageDiscoveryWorkUnit 是一个简单的POJO / Bean类。我有在 spring-servlet.xml 文件中声明的服务bean:

PageDiscoveryWorkUnit is a simple POJO/Bean class. I have my service beans declared in the spring-servlet.xml file:

<bean id="crawlerService" class="com.zeario.service.CrawlerService" />



Gradle配置



1)根项目(空,无源)定义 settings.gradle 中的子项目。依赖关系在 build.gradle 中:

Gradle Configuration

1) Root project (empty, no source) defines sub-projects in settings.gradle. Dependencies are in build.gradle:

project(':zeario-web') {
    dependencies {
        compile project(':zeario-sharedclientserver')
    }
}

project(':zeario-webcrawler') {
    dependencies {
        compile project(':zeario-sharedclientserver')
    }
}

2)项目在适用的情况下定义其他项目的依赖关系。例如,web项目构建文件包含:

2) Projects define dependencies on other projects where applicable. For example, the web project build file contains:

dependencies {
    // Guava, Spring MVC, etc. ...
    packed project(':zeario-sharedclientserver') // Shared code
    // ...
}



Eclipse配置



当我检查构建路径时,Eclipse正确显示项目依赖关系;例如,它显示 web 项目依赖于共享客户端/服务器项目。

推荐答案

非常感谢@Vidya谁帮助我意识到问题是 Eclipse ,而不是。问题是Eclipse无法将文件复制到它构建的JAR中。

Many thanks to @Vidya who helped me realize that the issue is Eclipse, not Gradle. The problem is that Eclipse failed to copy the files into the JAR that it built.

解决此问题的步骤:


  • 右键单击Web项目并选择属性

  • 点击部署程序集,然后单击添加

  • 选择项目,并选择共享库

  • 清理,重建和部署

  • Right-click the Web project and pick Properties
  • Click on Deployment Assembly and click Add
  • Select Project, and pick the shared library
  • Clean, rebuild, and deploy

Et中提琴,问题解决了!

Et viola, problem solved!

注意:Eclipse有时会有一个脑屁,要求您删除并重新添加项目依赖关系。我碰到了这个。

Note: Eclipse sometimes has a brain fart, and requires you to remove and re-add the project dependency. I ran into this, too.

这篇关于在Eclipse中调试时,Spring无法从共享的Gradle库(autowired bean中的依赖关系)中找到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆