4

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

buræquete
  • 14,226
  • 4
  • 44
  • 89
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • I see that you are using the '@Service' annotation correctly but are you using the '@Repository' annotation for your repository class? Without that annotation, you'll see an error when trying to '@Autowire'. This might be helpful as well as I don't see where your main SpringBootApplication class is located at: https://stackoverflow.com/questions/29221645/cant-autowire-repository-annotated-interface-in-spring-boot – Brandon Jan 24 '19 at 20:56
  • Problem solved by this https://stackoverflow.com/a/42025703/6690200 – xingbin Feb 14 '19 at 03:48

6 Answers6

7

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
Lino
  • 19,604
  • 6
  • 47
  • 65
  • Thanks, I'll try it. I can not figure out why IntelliJ mistake it as an error. – xingbin Jan 22 '19 at 12:48
  • No luck, it still warns... – xingbin Jan 24 '19 at 13:09
  • @竹杖芒鞋轻胜马 does the `[alt]` + `[enter]` menu provide any solutions? You can also ignore those warnings by going to *Settings* > *Editor* > *Inspections* and turning it off – Lino Jan 24 '19 at 13:29
  • Yes, it let me add `@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")`, and the warnings disappear. I do not know why... – xingbin Jan 28 '19 at 13:24
  • Adding @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") solved the problem. But when I google `SpringJavaInjectionPointsAutowiringInspection`, I did not find any doc or official documents... – xingbin Jan 28 '19 at 13:41
  • @竹杖芒鞋轻胜马 I also found this question: https://stackoverflow.com/questions/21323309/intellij-idea-shows-errors-when-using-springs-autowired-annotation, what happens when you remove the spring facet in your project configuration and let IntelliJ detect it? – Lino Jan 31 '19 at 07:35
  • The warning disappears after I removing the Spring facet. While, I'm afraid this brings another problem, even if I remove the annotations on the other classes, such as `@Service`, IntelliJ now can not detect it, so this might disable the whole detect function of IntelliJ... – xingbin Jan 31 '19 at 07:40
  • @竹杖芒鞋轻胜马 Try to click on [Build] and then [Rebuild Project] in the top bar, this sometimes triggers intelliJ to scan everything, and as it says just completely rebuild your project – Lino Jan 31 '19 at 07:42
  • Seems there is a solution here https://stackoverflow.com/a/42025703/6690200 – xingbin Jan 31 '19 at 07:52
  • The solution I take before, use `@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")`, was suggested by IntelliJ after I enter `[alt] + [enter]`. It does work but I will not use it because I do not want to import this annotation. I will enable `spring data` plugin, like another answer said. – xingbin Jan 31 '19 at 08:12
2

It is possible that Intellij has trouble with your package name in @EnableJpaRepositories annotation. Can you try putting that annotation in your main Spring class without any basePackages property defined?

@EnableJpaRepositories(entityManagerFactoryRef = "xxx", transactionManagerRef = "xxx")
@SpringBootApplication
public class FooApplication {

    public static void main(String[] args) {
        SpringApplication.run(FooApplication.class, args);
    }
}

Your main Spring class should be in the root of course;

root
  ServicePackage
    MyService
  RepositoryPackage
    MyRepository
  FooApplication <---- main class here
buræquete
  • 14,226
  • 4
  • 44
  • 89
  • 1
    Thanks for your reply. Use `@Repository` could elimate the warning, but `JpaRepository` is `NoRepositoryBean`. I think it should not be annotated with`@Repository`. My main class is `App`, so I suppose the repository class should be scanned. – xingbin Jan 26 '19 at 04:57
  • @竹杖芒鞋轻胜马 can you put the `@EnableJpaRepositories` on the `App` class then? Without `basePackages` property? That would resolve I'd hope! – buræquete Jan 26 '19 at 04:58
  • Seems does not help – xingbin Jan 28 '19 at 13:23
  • @竹杖芒鞋轻胜马 How about adding the facet to your project? From `Project Structure` menu, go to your project/module and add `JPA` facet? [example](https://i.stack.imgur.com/GbVS2.png) – buræquete Jan 28 '19 at 13:27
  • Thanks you sir, I already did that. But IntelliJ seems just can not recognize it. – xingbin Jan 28 '19 at 13:28
  • @竹杖芒鞋轻胜马 really interesting... It might be that your intellij has trouble, did you try following the steps [here](https://www.jetbrains.com/help/idea/enabling-jpa-support.html) ? Otherwise I have no other idea sadly :'( – buræquete Jan 28 '19 at 13:32
  • Thanks again. I will try it tomorrow. – xingbin Jan 28 '19 at 13:34
  • @竹杖芒鞋轻胜马 I have one question, when you type methods in a repository, does Intellij complete them? Like when you type `findBy`, does it give suggestions? – buræquete Jan 28 '19 at 13:34
  • Yes, it gives me auto complete. So I'm pretty sure IntelliJ can scan the repository. And the warning does no harm, I just don't like warnings. – xingbin Jan 28 '19 at 13:36
  • @竹杖芒鞋轻胜马 yeah I understand, I had the exact case, no problems but annoying warnings, not sure what stopped them though... – buræquete Jan 28 '19 at 13:37
  • @竹杖芒鞋轻胜马 use autowiring via constructors? That would make those warnings disappear I think, just follow [this](https://www.baeldung.com/spring-injection-lombok) This is what I did I think, that removed the warnings. – buræquete Jan 28 '19 at 13:40
  • Yes, I'm autowiring via constructors... While the warning shows whether filed or constructor injection.. – xingbin Jan 28 '19 at 13:43
1

Try it :

@Configuration
@ComponentScan(basePackages = {"your repository package}")
squalltrt
  • 21
  • 1
0

You can either declare:

@SuppressWarnings("SpringJavaAutowiringInspection")

On the field, or suppress the warning through Intellij's code inspection (click the red bulb and you can suppress 'Autowiring for Bean Class' either for the field or for the whole class.

0

On your database configuration you have to declare your base packages :

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "postgreEntityManager",
        transactionManagerRef = "postgreTransactionManager",
        basePackages = "fr.lab.test.dao"
)
public class PostGreDBConfig {
L. Quastana
  • 1,273
  • 1
  • 12
  • 34
0

I had the same issue once, but just changing the project structure and move your MyRepository to the same package where you have the service.This should work. I don't know why it happens.

Shubham
  • 997
  • 1
  • 9
  • 15