Spring cannot find class from Gradle shared library (dependency in autowired bean) when debugging in Eclipse

Problem definition

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

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

My problem is that I can create and deploy a working version of my web application using Gradle, but not through Eclipse. Eclipse gives me a Spring exception at runtime without the ability to search for one of the shared classes and, therefore, are unable to create / insert an instance of the service.

From Tomcat Logs:

org.springframework.beans.factory.BeanCreationException: Error creating bean named 'crawlerController': injection of auto-notified dependencies failed; The nested exception is org.springframework.beans.factory.BeanCreationException: Autofield failed: private com.zeario.service.CrawlerService com.zeario.api.v1.controller.CrawlerController.service; The 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 ... } 

Class of service (simplified):

 @Service public class CrawlerService { public PageDiscoveryWorkUnit getPageDiscoveryWorkUnit() { return new PageDiscoveryWorkUnit(); } 

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

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

Gradle Configuration

1) The root project (empty, without source) defines the subprojects 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) Projects determine dependencies on other projects, where applicable. For example, a web project build file contains:

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

Eclipse Configuration

When I check build paths, Eclipse correctly displays project dependencies; for example, it shows that the web project is dependent on the shared client/server project.

+1
source share
2 answers

Many thanks to @Vidya, who helped me understand that the problem is Eclipse, not Gradle. The problem is that Eclipse was not able to copy the files to the JAR that it built.

Steps to fix this problem:

  • Right-click the web project and select Properties
  • Click Deployment Assembly and click Add
  • Select Project and select a shared library
  • Clear, rebuild, and deploy

Et viola, the problem is solved!

Note. Eclipse sometimes has brain fart and requires you to remove and re-add a project dependency. I also came across this.

0
source

It seems that Eclipse does not pack the jar that contains PageDiscoveryWorkUnit in the war. This allows you to customize Eclipse. Confirm this by unpacking the wars created in both directions and comparing the contents of `WEB-INF / lib. Then redo the Eclipse configuration to make sure the jar is added.

+2
source

Source: https://habr.com/ru/post/885405/


All Articles