Недоступный квалифицирующий компонент типа «mypackage.repository»: ожидается как минимум один компонент, который считается кандидатом на автоматическое подключение - PullRequest
0 голосов
/ 04 апреля 2019

У меня есть тестовый класс в Junit4, который должен использовать NutrientListService.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {

    private NutrientListService nutrientService;

    @Test
    public void someTest()
        Result re = Calculator.calculate(response, nutrientService)
}

Я получаю нулевой нутриентный сервис, поэтому попытался настроить ApplicationContext.

@Configuration
@ComponentScan("myservice")
@ComponentScan("myrepository")
public class ApplicationContext {

    @Autowired
    NutrientListService nutrientService;
}

Однако я получаю

Error creating bean with name 'nutrientListService': Unsatisfied dependency expressed through field 'nutrientListRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repositories.NutrientListRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Это услуга:

@Service
@Component
public class NutrientListService {
    @Autowired
    private NutrientListRepository repo;
}

И хранилище:

@Repository
public interface NutrientListRepository extends MongoRepository<MyClass, String> {

    MyClass findByID(String ID);
}

Есть идеи, как правильно подключить услугу? Мне нужно передать его для расчета, так как это один из параметров. Нужно ли использовать класс контекста приложения или application-context.xml (который я не смог найти)? Каков был бы наименее неясный способ сделать это? Я благодарю вас.

1 Ответ

0 голосов
/ 04 апреля 2019
@Configuration
@ComponentScan("myservice")
@ComponentScan("myrepository")
public class ApplicationContext {
    @Bean
    NutrientListService nutrientService(){
      new NutrientListService()
    }
}

А затем вызовите Боб с @ Autowired

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {

    @Autowired
    NutrientListService nutrientService

    @Test
    public void someTest()
        Result re = Calculator.calculate(response, nutrientService)
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...