简体   繁体   中英

Intellij @Autowired. No matching spring beans found

JAVA 1.7
Spring 4.3.7
IntelliJ IDEA 2017.3.4 (Ultimate Edition) Build #IU-173.4548.28, built on January 30, 2018 JRE: 1.8.0_152-release-1024-b11 x86_64 JVM: OpenJDK 64-Bit Server VM by JetBrains sro Mac OS X 10.13.3

why not found? the code has no problem and the test was successful.

But I'm concerned about the error mark.

useDefaultFilters false = not found.

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(
    basePackages = "org.fxb.module",
    useDefaultFilters = false,
    includeFilters = {
        @Filter(type = FilterType.ANNOTATION, classes = { Aspect.class, Mapper.class }),
    }
)
public class ModuleConfiguration {
  @Autowired
  private ModuleContextAOP moduleContextAOP;

在此处输入图片说明

useDefaultFilters = true = found.

在此处输入图片说明

AOP code

package org.fxb.module.aop;

@Aspect
@Component
public class ModuleContextAOP {

在此处输入图片说明

I think it's a code with no problems. I do not understand why IntelliJ seems to be an error.

I think it isn't able to autowire the component because the class ModuleConfiguration where you are autowiring ModuleContextAOP isn't a component. Try annotating ModuleConfiguration with component and it should work. Usually @Configuration is used for defining beans but you are trying to autowire them.

Your class ModuleConfiguration is not a Spring bean class hence dependencies on it cannot be autowired. You need to either put the bean entry for the class inside XML file (where you declare other spring beans) or annotate it with @component .

@Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

Or you can use @Configuration to scan all the spring bean into a class @Configuration is the heart of the Java-based configuration mechanism that was introduced in Spring 3. It provides an alternative to the XML-based configuration.

So the 2 following snippets are identical:

<beans ...>
    <context:component-scan base-package="my.base.package"/>
    ... other configuration ...
</beans>

and:

@Configuration
@ComponentScan(basePackages = "my.base.package")
public class RootConfig {
    ... other configuration ...
}

In both cases, Spring will scan in my.base.package and below for classes annotated with @Component or one of the other annotations that are meta-annotated with @Component such as @Service .

https://youtrack.jetbrains.com/issue/IDEA-187757

includeFilters = @Filter(type = FilterType.ANNOTATION, classes = Component.class )

to

includeFilters = @Filter(type = FilterType.ANNOTATION, value = Component.class )

Resolved.

Thank...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM