java - BeanCreationException,Spring 启动

标签 java maven spring-boot spring-mvc

我正在设置一个 Spring Boot maven 多模块项目,其中包含域层、持久层和 Web 层。

我一直在查找类似的问题,但似乎没有一个是我的情况,我怀疑这是因为该项目被划分为单独的maven模块,Spring找不到bean。

尝试了一些可能的解决方案,但它们似乎并没有解决问题。

主要

 @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);
    }
}

休息 Controller

@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
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);

    }

持久性

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

}

这就是结构的样子

structure 我在启动嵌入 tomcat 时遇到异常,正如我之前所说,我认为是因为它无法找到服务,因此无法注入(inject) 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


最佳答案

我通过在主类中添加组件扫描解决了这个问题

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

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

}

这样spring在所有maven模块中都有一个通用的基础包,它可以扫描组件。

关于java - BeanCreationException,Spring 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202859/

相关文章:

java - Tomcat不要运行sakai

java - mvn install 依赖项忽略 destFileName :copy-dependencies

java - Spring REST获取多个映射端点的url路径

java - 在 JAXB 中自动添加根元素

spring-boot - Kotlin - 数据类实体抛出 StackOverflowError

java - Java中设置多种格式的系统剪贴板

java - 给jvm添加内存

java - Spring MVC : PropertyEditor throwing exception, 无法设置值

Java Swing : Button's specific id. 如何在不使用 .getActionCommand 的监听器中捕获按下的按钮?

java - 用于 Spring Boot 的 GraphQL Java 客户端