【问题标题】:Maven : Not able to autowire from service module to controller moduleMaven:无法从服务模块自动装配到控制器模块
【发布时间】:2019-12-26 22:06:01
【问题描述】:

这是我的项目结构

parent 是客户端、控制器、服务和存储库的父项目。

现在我想在控制器中添加服务的依赖,所以在控制器pom.xml中添加了相同的内容

    <dependency>
        <groupId>com.freelancing.fullstack</groupId>
        <artifactId>service</artifactId>
        <version>${project.version}</version>
        <scope>provided</scope>
    </dependency>

在控制器项目中,我有一个 MainController 类。

@RestController
public class MainController
{
    @Autowired
    ServiceClass sc;

    @GetMapping("test")
    public void testing()
    {
        sc.callService();
    }
}

现在在将控制器项目作为 springboot 应用程序运行时,我面临以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController' defined in file [E:\parent\controller\target\classes\org\controller\MainController.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.controller.MainController] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@574aa597]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.controller.ControllerClass.main(ControllerClass.java:11) [classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.controller.MainController] from ClassLoader [org.springframework.boot.devtools.restart.classloader.RestartClassLoader@574aa597]
        at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:734) ~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:666) ~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:382) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:363) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:311) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1094) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:569) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        ... 20 common frames omitted
    Caused by: java.lang.NoClassDefFoundError: Lorg/service/ServiceClass;
        at java.lang.Class.getDeclaredFields0(Native Method) ~[na:1.8.0_212]
        at java.lang.Class.privateGetDeclaredFields(Class.java:2583) ~[na:1.8.0_212]
        at java.lang.Class.getDeclaredFields(Class.java:1916) ~[na:1.8.0_212]
        at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:729) ~[spring-core-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        ... 26 common frames omitted
    Caused by: java.lang.ClassNotFoundException: org.service.ServiceClass
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_212]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_212]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_212]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_212]
        at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:144) ~[spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_212]
        ... 30 common frames omitted

seems like ServiceClass bean is not available at runtime.

添加componentscan注解后

facing this issue.

> [INFO] Scanning for projects... [INFO]  [INFO] ----------------<
> com.freelancing.fullstack:controller >---------------- [INFO] Building
> controller 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar
> ]--------------------------------- [INFO]  [INFO] >>>
> spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) >
> test-compile @ controller >>> [INFO]  [INFO] ---
> maven-resources-plugin:3.1.0:copy-resources (copy-resources) @
> controller --- [INFO] Using 'UTF-8' encoding to copy filtered
> resources. [INFO] Copying 22 resources [INFO]  [INFO] ---
> maven-resources-plugin:3.1.0:resources (default-resources) @
> controller --- [INFO] Using 'UTF-8' encoding to copy filtered
> resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] 
> [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @
> controller --- [INFO] Nothing to compile - all classes are up to date
> [INFO]  [INFO] --- maven-resources-plugin:3.1.0:testResources
> (default-testResources) @ controller --- [INFO] Using 'UTF-8' encoding
> to copy filtered resources. [INFO] skip non existing resourceDirectory
> E:\parent\controller\src\test\resources [INFO]  [INFO] ---
> maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @
> controller --- [INFO] Nothing to compile - all classes are up to date
> [INFO]  [INFO] <<< spring-boot-maven-plugin:2.2.2.RELEASE:run
> (default-cli) < test-compile @ controller <<< [INFO]  [INFO]  [INFO]
> --- spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) @ controller --- [INFO] Attaching agents: []
> 
>   .   ____          _            __ _ _  /\\ / ___'_ __ _ _(_)_ __  __
> _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )   '  |____| .__|_| |_|_| |_\__, | / / / / 
> =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v2.2.2.RELEASE)
> 
> 2019-12-27 10:51:29.859  INFO 6184 --- [  restartedMain]
> org.controller.ControllerClass           : Starting ControllerClass on
> DESKTOP-551C51M with PID 6184 (E:\parent\controller\target\classes
> started by sparsh in E:\parent\controller) 2019-12-27 10:51:29.864 
> INFO 6184 --- [  restartedMain] org.controller.ControllerClass        
> : No active profile set, falling back to default profiles: default
> 2019-12-27 10:51:29.927  INFO 6184 --- [  restartedMain]
> .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults
> active! Set 'spring.devtools.add-properties' to 'false' to disable
> 2019-12-27 10:51:29.927  INFO 6184 --- [  restartedMain]
> .e.DevToolsPropertyDefaultsPostProcessor : For additional web related
> logging consider setting the 'logging.level.web' property to 'DEBUG'
> 2019-12-27 10:51:31.375  INFO 6184 --- [  restartedMain]
> o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with
> port(s): 9091 (http) 2019-12-27 10:51:31.384  INFO 6184 --- [ 
> restartedMain] o.apache.catalina.core.StandardService   : Starting
> service [Tomcat] 2019-12-27 10:51:31.384  INFO 6184 --- [ 
> restartedMain] org.apache.catalina.core.StandardEngine  : Starting
> Servlet engine: [Apache Tomcat/9.0.29] 2019-12-27 10:51:31.456  INFO
> 6184 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       :
> Initializing Spring embedded WebApplicationContext 2019-12-27
> 10:51:31.457  INFO 6184 --- [  restartedMain]
> o.s.web.context.ContextLoader            : Root WebApplicationContext:
> initialization completed in 1529 ms 2019-12-27 10:51:31.753  INFO 6184
> --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor' 2019-12-27
> 10:51:31.805  INFO 6184 --- [  restartedMain]
> o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class
> path resource [resources/index.html] 2019-12-27 10:51:31.944  WARN
> 6184 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       :
> Unable to start LiveReload server 2019-12-27 10:51:31.950  INFO 6184
> --- [  restartedMain] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator' 2019-12-27
> 10:51:32.009  INFO 6184 --- [  restartedMain]
> o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
> 2019-12-27 10:51:32.019  INFO 6184 --- [  restartedMain]
> ConditionEvaluationReportLoggingListener : 
> 
> Error starting ApplicationContext. To display the conditions report
> re-run your application with 'debug' enabled. 2019-12-27 10:51:32.027
> ERROR 6184 --- [  restartedMain]
> o.s.b.d.LoggingFailureAnalysisReporter   : 
> 
> *************************** APPLICATION FAILED TO START
> ***************************
> 
> Description:
> 
> Web server failed to start. Port 9091 was already in use.
> 
> Action:
> 
> Identify and stop the process that's listening on port 9091 or
> configure this application to listen on another port.
> 
> 2019-12-27 10:51:32.029  INFO 6184 --- [  restartedMain]
> o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down
> ExecutorService 'applicationTaskExecutor' [INFO]
> ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO]
> ------------------------------------------------------------------------ [INFO] Total time: 7.045 s [INFO] Finished at:
> 2019-12-27T10:51:32+05:30 [INFO]
> ------------------------------------------------------------------------

请帮忙

【问题讨论】:

标签: spring-boot maven


【解决方案1】:

删除服务依赖项中的范围provided。在构建和测试项目期间使用Maven 依赖项范围provided。它们也需要运行,但不会导出(因为期望依赖项将由运行时提供,例如,由 servlet 容器或应用程序服务器提供)。像这样使用它:

<dependency>
    <groupId>com.freelancing.fullstack</groupId>
    <artifactId>service</artifactId>
    <version>${project.version}</version>
</dependency>

在 springboot 应用程序中,通常将基本包作为主应用程序所在的包级别。由于您有一个多模块项目,因此您的其他包可能没有被扫描以创建 bean。因此,请尝试使用以下配置,例如指定要用于自动配置的类路径扫描的基本包:

@ComponentScan(basePackages = {"com.yourBasePackage"})

【讨论】:

  • 感谢您的回复,虽然添加此构建成功但对于任何随机端口,端口已被使用。在它上面添加错误消息可以帮助你
  • @sparsh610 我已经更新了我的答案。请尝试更新的答案。
  • 找不到 bean 的问题得到了解决,但现在它说端口已在使用中。对于每个值,我也在 application.properties 中更改了它
  • 使用命令netstat -ano 查找所有正在使用您希望springboot 应用程序使用的端口的pid 杀死所有的pid 并重试。在启动您的应用程序之前,请注意停止使用特定端口的任何进程。
猜你喜欢
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多