java 多模块组件扫描在 Spring Boot 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33080261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multi module component scanning not working in spring boot
提问by krmanish007
I am having two module web and business. I have included business in the web. But when I try to include a service interface from business into web using @autowired, it is giving org.springframework.beans.factory.NoSuchBeanDefinitionException.
我有两个模块网络和业务。我已将业务包含在网络中。但是,当我尝试将业务的服务接口包含到 Web 中时@autowired,它给出了org.springframework.beans.factory.NoSuchBeanDefinitionException.
So, basically @SpringBootApplicationis not able to scan the @Servicefrom business module.
所以,基本上@SpringBootApplication是无法扫描到@Servicefrom业务模块的。
Is it something simple, I am missing?
是不是很简单,我失踪了?
If I add @Beanfor that service in the @SpringBootApplicationclass, it is working fine.
如果我@Bean在@SpringBootApplication课堂上添加该服务,它就可以正常工作。
Code:
代码:
package com.manish;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class SpringBootConfiguration {
public static void main(String[] args) {
SpringApplication.run(SpringBootConfiguration.class, args);
}
}
Class from module 1 from which is calling class from module 2:
模块 1 中的类正在调用模块 2 中的类:
package com.manish.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import uk.co.smithnews.pmp.service.contract.UserRegistrationService;
@RestController
@RequestMapping("/testManish")
public class SampleController {
@Autowired
private SampleService sampleService;
....
}
Module 2:
模块 2:
package com.manish.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SampleServiceImpl implements SampleService {
}
Thanks,
谢谢,
回答by dunni
@SpringBootApplicationonly scans the packages of the class with the annotation itself and all packages below.
@SpringBootApplication只扫描带有注释本身的类的包和下面的所有包。
Example: If the class with the SpringBootApplication annotation is in the package com.project.web, then this packages and all below that are scanned.
示例:如果带有 SpringBootApplication 注解的类在 package 中com.project.web,那么这个包和下面的所有包都被扫描。
However, if you have your services in the package com.project.business, the beans won't be scanned.
但是,如果您在 package 中有您的服务,com.project.business则不会扫描 bean。
In that case you have to add the annotation @ComponentScan()to your application class, and add all packages you want to scan as value in that annotation, e.g. @ComponentScan({"com.project.web", "com.project.business"}).
在这种情况下,您必须将注释添加@ComponentScan()到您的应用程序类,并添加您想要扫描的所有包作为该注释中的值,例如@ComponentScan({"com.project.web", "com.project.business"}).

