krm*_*007 10 java spring spring-boot
我有两个模块网络和业务.我已将业务纳入网络.但是,当我尝试将业务中的服务接口包含在web中时@autowired
,它正在给予org.springframework.beans.factory.NoSuchBeanDefinitionException
.
所以,基本上@SpringBootApplication
无法扫描@Service
来自业务模块.
它是简单的,我想念?
如果我@Bean
在@SpringBootApplication
课堂上添加该服务,它工作正常.
码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
模块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;
....
}
Run Code Online (Sandbox Code Playgroud)
第2单元:
package com.manish.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SampleServiceImpl implements SampleService {
}
Run Code Online (Sandbox Code Playgroud)
谢谢,
dun*_*nni 23
@SpringBootApplication
仅使用注释本身和下面的所有包扫描类的包.
示例:如果包含SpringBootApplication批注的类在com.project.web
此包中,则扫描此包及其下面的所有包.
但是,如果您在包中有服务,com.project.business
则不会扫描bean.
在这种情况下,您必须将注释添加@ComponentScan()
到应用程序类,并将要扫描的所有包作为值添加到该注释中,例如@ComponentScan({"com.project.web", "com.project.business"})
.