我正在开发一个spring boot应用程序,我在这里遇到了一个问题.我正在尝试注入一个@Repository注释接口,它似乎根本不起作用.我收到了这个错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springBootRunner': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.pharmacy.persistence.users.dao.UserEntityDao com.pharmacy.config.SpringBootRunner.userEntityDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.pharmacy.persistence.users.dao.UserEntityDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at …Run Code Online (Sandbox Code Playgroud) 在SpringBoot应用程序中,我想对存储库层进行一些测试.
@RunWith(SpringRunner.class)
@DataJpaTest
public class VisitRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private VisitRepository visitRepository;
...
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行测试时VisitRepositoryTest,我收到一个错误DefaultConfigService
com.norc.Application中的字段defaultConfigService需要一个无法找到的类型为"com.norc.service.DefaultConfigService"的bean.
那么这需要运行Application吗?
我试着把豆子放进 DefaultConfigService去VisitRepositoryTest,但是不允许这样做.
这个类在我的应用程序中使用
@EntityScan(basePackageClasses = {Application.class, Jsr310JpaConverters.class})
@SpringBootApplication
@EnableScheduling
public class Application implements SchedulingConfigurer {
@Autowired
private DefaultConfigService defaultConfigService;
...
}
Run Code Online (Sandbox Code Playgroud)
如何管理?
在我的应用程序中,我在cron选项卡中使用此类:
@Service
public class DefaultConfigServiceImpl implements DefaultConfigService {
private final DefaultConfigRepository defaultConfigRepository;
@Autowired
public DefaultConfigServiceImpl(final DefaultConfigRepository defaultConfigRepository) {
this.defaultConfigRepository = defaultConfigRepository;
}
}
Run Code Online (Sandbox Code Playgroud)