Spring boot考虑定义一个类型的bean

时间:2018-05-02 10:08:40

标签: java maven spring-boot

我在pom.xml

中添加了一个依赖项
<dependency>
    <groupId>com.company.location.my1</groupId>
    <artifactId>sMyWebservice-module-services</artifactId>
    <version>RELEASE</version>
    <scope>compile</scope>
</dependency>

但由于例外情况,我无法启动我的网络服务:Field myService in com.company.my.webservice.controllers.MyController required a bean of type 'com.company.location.my1.webservice.my.MyService' that could not be found.

所以我将springbootapplication改为

@SpringBootApplication
    (scanBasePackages = {
    "com.company.location.my1", //what I added to my pom
    "com.company.my.webservice" //current parent package, not necessary
    })
public class WebserviceApplication {

所以我会扫描包

并在MyController中使用它

@Autowired
private MyService myService;

@PostMapping
public MyResponse activate(){
   return myService.activate();
}

1 个答案:

答案 0 :(得分:0)

验证MyService类是否使用@Service@Configuration进行注释。

如果MyService未注释,您可以在WebserviceApplication类中使用@Bean定义一个方法,该方法将为您创建实例:

@Bean
public MyService myService() {
    return new MyService();
}