Could not autowire. No beans of `Repository' type found
Solution 1
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:
- Click in the file structure on the top folder
- Press F4
- Go to "Modules"
- Select the module in which IntelliJ complains from the list
- Click on "Spring"
- Click on the plus icon
- Select the configuration which provides your
xRepository
- Save
Solution 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
Solution 3
Try it :
@Configuration
@ComponentScan(basePackages = {"your repository package}")
xingbin
Updated on June 07, 2022Comments
-
xingbin almost 2 years
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 injectMyRepository
toMyService
, but IntelliJ always complainsCould 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
-
xingbin over 5 yearsThanks for the reply. I did that, but still the warning...
-
xingbin over 5 yearsWhere should I put this annotation on?
-
xingbin over 5 yearsThanks, I'll try it. I can not figure out why IntelliJ mistake it as an error.
-
AlgorithmFromHell over 5 yearsPut it above the field. It should work just as well if you put it on top of the class, but then you're going to suppress warnings for all fields.
-
xingbin over 5 yearsI've tried both.. No luck... Still the warning.
-
xingbin over 5 yearsNo luck, it still warns...
-
Lino over 5 years@竹杖芒鞋轻胜马 does the
[alt]
+[enter]
menu provide any solutions? You can also ignore those warnings by going to Settings > Editor > Inspections and turning it off -
xingbin over 5 yearsThanks for your reply. Use
@Repository
could elimate the warning, butJpaRepository
isNoRepositoryBean
. I think it should not be annotated with@Repository
. My main class isApp
, so I suppose the repository class should be scanned. -
xingbin over 5 yearsI already did that in
MyConfig
-
buræquete over 5 years@竹杖芒鞋轻胜马 can you put the
@EnableJpaRepositories
on theApp
class then? WithoutbasePackages
property? That would resolve I'd hope! -
xingbin over 5 yearsSeems does not help
-
xingbin over 5 yearsYes, it let me add
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
, and the warnings disappear. I do not know why... -
buræquete over 5 years@竹杖芒鞋轻胜马 How about adding the facet to your project? From
Project Structure
menu, go to your project/module and addJPA
facet? example -
xingbin over 5 yearsThanks you sir, I already did that. But IntelliJ seems just can not recognize it.
-
buræquete over 5 years@竹杖芒鞋轻胜马 really interesting... It might be that your intellij has trouble, did you try following the steps here ? Otherwise I have no other idea sadly :'(
-
xingbin over 5 yearsThanks again. I will try it tomorrow.
-
buræquete over 5 years@竹杖芒鞋轻胜马 I have one question, when you type methods in a repository, does Intellij complete them? Like when you type
findBy
, does it give suggestions? -
xingbin over 5 yearsYes, 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.
-
buræquete over 5 years@竹杖芒鞋轻胜马 yeah I understand, I had the exact case, no problems but annoying warnings, not sure what stopped them though...
-
buræquete over 5 years@竹杖芒鞋轻胜马 use autowiring via constructors? That would make those warnings disappear I think, just follow this This is what I did I think, that removed the warnings.
-
xingbin over 5 yearsAdding @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") solved the problem. But when I google
SpringJavaInjectionPointsAutowiringInspection
, I did not find any doc or official documents... -
xingbin over 5 yearsYes, I'm autowiring via constructors... While the warning shows whether filed or constructor injection..
-
Lino over 5 years@竹杖芒鞋轻胜马 I also found this question: stackoverflow.com/questions/21323309/…, what happens when you remove the spring facet in your project configuration and let IntelliJ detect it?
-
xingbin over 5 yearsThe 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... -
Lino over 5 years@竹杖芒鞋轻胜马 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
-
xingbin over 5 yearsSeems there is a solution here stackoverflow.com/a/42025703/6690200
-
xingbin over 5 yearsThe 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 enablespring data
plugin, like another answer said. -
Eugene Kortov about 3 years@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")