0

I’m setting up a Spring Boot maven multi-module project where I have Domain layer, Persistence layer and Web layer.

ive been looking up similar questions,but none seem to be my case,i suspect its due to the fact the project is divided in individual maven modules Spring cant find the bean.

tried some posible solutions but they dont seem to be solving the problem.

Main

 @SpringBootApplication(scanBasePackages = { "com.imricki.movie.domain", "com.imricki.movie.persistence",
        "com.imricki.movie.web" })
public class SpringBootAplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootAplication.class, args);
    }
}

RestController

@RequestMapping(value = "/api")
@RestController
class MovieRestController {

    @Autowired
    private MovieService service;

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    ResponseEntity<Void> delete(@PathVariable Long Id) {

        this.service.delete(Id);
        return new ResponseEntity<>(HttpStatus.OK);

    }

Service

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    MovieRepository repo;

    /** The model mapper to convert. */
    private final ModelMapper modelMapper = new ModelMapper();

    @Override
    @Transactional
    public void delete(Long id) {

        this.repo.deleteById(id);

    }

Persistence

@Repository
public interface MovieRepository extends JpaRepository<Movie, Long> {

}

this is what the structure looks like

structure Im getting a excepcion wen starting the embebed tomcat,as i said before i think ists because it cant find the service so it cant inject the bean

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-05-18 21:42:40.598  INFO 1696 --- [           main] c.i.movie.web.SpringBootAplication       : Starting SpringBootAplication on DESKTOP-Q7NDFBT with PID 1696 (C:\Desarollo\wks\Parent\Web\target\classes started by Usuario in C:\Desarollo\wks\Parent\Web)
2019-05-18 21:42:40.598  INFO 1696 --- [           main] c.i.movie.web.SpringBootAplication       : No active profile set, falling back to default profiles: default
2019-05-18 21:42:43.114  INFO 1696 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-05-18 21:42:43.134  INFO 1696 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-18 21:42:43.134  INFO 1696 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-05-18 21:42:43.165  INFO 1696 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_202\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_202/bin/server;C:/Program Files/Java/jre1.8.0_202/bin;C:/Program Files/Java/jre1.8.0_202/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Calibre2\;C:\Program Files\Git\cmd;C:\Users\Usuario\AppData\Local\Microsoft\WindowsApps;;C:\Microsoft VS Code\bin;C:\eclipse\eclipse-jee-2019-03-R-win32-x86_64\eclipse;;.]
2019-05-18 21:42:43.745  INFO 1696 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-18 21:42:43.745  INFO 1696 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3100 ms
2019-05-18 21:42:44.276  WARN 1696 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'movieService' defined in file [C:\Desarollo\wks\Parent\Domain\target\classes\com\imricki\movie\domain\impl\MovieServiceImpl.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.imricki.movie.domain.impl.MovieServiceImpl] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]
2019-05-18 21:42:44.286  INFO 1696 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-05-18 21:42:44.348  INFO 1696 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-05-18 21:42:44.442 ERROR 1696 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'movieService' defined in file [C:\Desarollo\wks\Parent\Domain\target\classes\com\imricki\movie\domain\impl\MovieServiceImpl.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.imricki.movie.domain.impl.MovieServiceImpl] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:570) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at com.imricki.movie.web.SpringBootAplication.main(SpringBootAplication.java:11) [classes/:na]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.imricki.movie.domain.impl.MovieServiceImpl] from ClassLoader [sun.misc.Launcher$AppClassLoader@764c12b6]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:785) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:717) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:365) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:350) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:298) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1061) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:567) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 15 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Lcom/imricki/movie/persistence/repository/MovieRepository;
    at java.lang.Class.getDeclaredFields0(Native Method) ~[na:1.8.0_202]
    at java.lang.Class.privateGetDeclaredFields(Class.java:2583) ~[na:1.8.0_202]
    at java.lang.Class.getDeclaredFields(Class.java:1916) ~[na:1.8.0_202]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:780) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.imricki.movie.persistence.repository.MovieRepository
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_202]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_202]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_202]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_202]
    ... 25 common frames omitted


Łukasz Gawron
  • 897
  • 10
  • 20
Ricki
  • 141
  • 3
  • 16

3 Answers3

1

I solved the problem by adding component scan in the main class

@SpringBootApplication(scanBasePackages = { "com" })
@EntityScan(basePackages = { "com" })
public class SpringBootAplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootAplication.class, args);
    }

}

this way spring has a common base package in all the maven modules in witch it scans for the components.

Ricki
  • 141
  • 3
  • 16
1

You need to scan the packages. By adding @componentScan annotation

Joyson Rego
  • 968
  • 2
  • 11
  • 29
0

Seems that main issue is lack of MovieRepository implementation

Caused by: java.lang.ClassNotFoundException: com.imricki.movie.persistence.repository.MovieRepository

you didn't provide pom.xml so it's not possible to verify that all dependencies are correct, but I suspect you may lack of @EnableJpaRepositories annotation

more about this annotation here and in this SO question

Example:

@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

  @Bean
  EntityManagerFactory entityManagerFactory() {
    // …
  }
}
Łukasz Gawron
  • 897
  • 10
  • 20