在被依赖的@Service中使用@Autowired或@Inject注解时,如果被依赖的@Service没有通过@Component或@Service等注解被Spring扫描到,则会抛出BeanInstantiationException异常。解决方法是在被依赖的@Service上添加@Component或@Service注解,将其纳入Spring容器管理。示例代码如下:
@Service public class DependentService { @Autowired private NotScannedService notScannedService; //... }
@Service @Component //或者@Service public class NotScannedService { //... }