我试图理解为什么我不能自动装配类库,但我可以在相同的包中自动装配一个接口存储库以进行相同的测试.当我启动应用程序时,相同的存储库按预期工作.
一,错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.app.person.repository.PersonRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.raiseNoMatchingBeanFound(DefaultPersonbleBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.doResolveDependency(DefaultPersonbleBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.resolveDependency(DefaultPersonbleBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 28 more
Run Code Online (Sandbox Code Playgroud)
我有一个非常简单的例子.考试:
@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryTest {
@Autowired
private PersonRepository personRepository; // fail...
@Autowired
private PersonCrudRepository personCrudRepository; // works!
@Test
public void findOne() {
}
}
Run Code Online (Sandbox Code Playgroud)
存储库类:
@Repository
public class PersonRepository {
//code
}
Run Code Online (Sandbox Code Playgroud)
存储库界面:
@Repository
public interface PersonCrudRepository extends …Run Code Online (Sandbox Code Playgroud) 我不确定我的配置有什么问题,但是据我所知它应该是正确的,尽管那个春天宣布没有这样的bean定义。我对以下几点表示赞同:
这是我完整的调试日志: spring启动日志pastebin
没有调试标志的日志:
2017-06-12 09:34:36.395 INFO 12880 --- [ restartedMain]
d.a.c.newworlds.NewWorldsApplication : No active profile set, falling back to default profiles: default
2017-06-12 09:34:36.727 INFO 12880 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@10057b9b: startup date [Mon Jun 12 09:34:36 CEST 2017]; root of context hierarchy
2017-06-12 09:34:38.090 INFO 12880 --- [ restartedMain] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'playerRepository' with a different definition: replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Data Jpa,这是我的项目结构:
App
ConfigPackage
MyConfig
ServicePackage
MyService
RepositoryPackage
MyRepository
Run Code Online (Sandbox Code Playgroud)
这是MyRepository:
public interface MyRepository extends JpaRepository<MyEntity, Long> {
}
Run Code Online (Sandbox Code Playgroud)
这是MyService:
@Service
public class MyService {
@Autowired
private MyRepository myRepository; <---here
...
}
Run Code Online (Sandbox Code Playgroud)
这是MyConfig:
@Configuration
@EnableJpaRepositories(
basePackages = "RepositoryPackage",
entityManagerFactoryRef = "xxx",
transactionManagerRef = "xxx"
)
public class MyConfig {
}
Run Code Online (Sandbox Code Playgroud)
我用@Autowired注入MyRepository到MyService,但的IntelliJ总是抱怨
无法自动接线。找不到“ MyRepository”类型的bean
即使代码可以编译并成功运行。
为什么IntelliJ无法识别这不是错误?如何清除IntelliJ的警告?
IntelliJ版本:2018.2.6
I am building a Spring-boot application where I am using Spring data jpa feature.
Please find below my dao layer code
package com.adv.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CustomerDao extends JpaRepository<Customer, String> {
}
Run Code Online (Sandbox Code Playgroud)
I am using a DaoProvider class as follows:
package com.adv.dao;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class DaoProvider implements Serializable {
private static final long serialVersionUID = 1L;
@Autowired
private CustomerDao customerDao;
public CustomerDao getCustomerDao() {
return customerDao;
}
}
Run Code Online (Sandbox Code Playgroud)
My spring …