Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not autowire. No beans of `Repository' type found [duplicate]

I'm using Spring Data Jpa, this is my project structure:

App
  ConfigPackage
    MyConfig
  ServicePackage
    MyService
  RepositoryPackage
    MyRepository

Here is the MyRepository:

public interface MyRepository extends JpaRepository<MyEntity, Long> {
}

Here is the MyService:

@Service
public class MyService {

    @Autowired
    private MyRepository myRepository; <---here

    ...
}

Here is the MyConfig:

@Configuration
@EnableJpaRepositories(
        basePackages = "RepositoryPackage",
        entityManagerFactoryRef = "xxx",
        transactionManagerRef = "xxx"
)
public class MyConfig {
}

I use @Autowired to inject MyRepository to MyService, but IntelliJ always complains

Could not autowire. No beans of 'MyRepository' type found

even if the code can compile and run successfully.

Why can not IntelliJ recognize this is not an error? How to remove the warning of IntelliJ?

IntelliJ Version: 2018.2.6

like image 385
xingbin Avatar asked Jan 01 '23 09:01

xingbin


1 Answers

Note this is for IntelliJ IDEA 2018.3.3 Ultimate Edition (But should work for other versions too)

I've noticed that this errors occurs (at least in my projects) when a configuration uses @ComponentScan and thus loads another class annotated with @Configuration. IntelliJ seems to not fully recognize it which leads to the error/warning:

  1. Click in the file structure on the top folder
  2. Press F4
  3. Go to "Modules"
  4. Select the module in which IntelliJ complains from the list
  5. Click on "Spring"
  6. Click on the plus icon
  7. Select the configuration which provides your xRepository
  8. Save
like image 180
Lino Avatar answered Jan 04 '23 17:01

Lino