没有可用类型 'mypackage.repository' 的符合条件的 bean:预计至少有 1 个符合自动装配候选条件的 bean
No qualifying bean of type 'mypackage.repository' available: expected at least 1 bean which qualifies as autowire candidate
我在 Junit4 中有一个测试 class 需要使用 NutrientListService。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {
private NutrientListService nutrientService;
@Test
public void someTest()
Result re = Calculator.calculate(response, nutrientService)
}
我得到一个空的 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);
}
有任何正确连接服务的想法吗?我需要传递它进行计算,因为它是参数之一。我是否必须使用应用程序上下文 class 或应用程序-context.xml(我找不到)?这样做最不晦涩的方法是什么?我谢谢你。
@Configuration
@ComponentScan("myservice")
@ComponentScan("myrepository")
public class ApplicationContext {
@Bean
NutrientListService nutrientService(){
new NutrientListService()
}
}
然后用@Autowired
调用Bean
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {
@Autowired
NutrientListService nutrientService
@Test
public void someTest()
Result re = Calculator.calculate(response, nutrientService)
}
我在 Junit4 中有一个测试 class 需要使用 NutrientListService。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {
private NutrientListService nutrientService;
@Test
public void someTest()
Result re = Calculator.calculate(response, nutrientService)
}
我得到一个空的 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);
}
有任何正确连接服务的想法吗?我需要传递它进行计算,因为它是参数之一。我是否必须使用应用程序上下文 class 或应用程序-context.xml(我找不到)?这样做最不晦涩的方法是什么?我谢谢你。
@Configuration
@ComponentScan("myservice")
@ComponentScan("myrepository")
public class ApplicationContext {
@Bean
NutrientListService nutrientService(){
new NutrientListService()
}
}
然后用@Autowired
调用Bean@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {
@Autowired
NutrientListService nutrientService
@Test
public void someTest()
Result re = Calculator.calculate(response, nutrientService)
}