简体   繁体   English

Intellij @自动接线。 找不到匹配的春豆

[英]Intellij @Autowired. No matching spring beans found

JAVA 1.7 JAVA 1.7
Spring 4.3.7 春季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 IntelliJ IDEA 2017.3.4(Ultimate Edition)版本#IU-173.4548.28,建于2018年1月30日JRE:1.8.0_152-release-1024-b11 x86_64 JVM:JetBrains在Mac OS X 10.13上提供的OpenJDK 64位服务器VM .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. useDefaultFilters false =找不到。

@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. useDefaultFilters = true =找到。

在此处输入图片说明

AOP code AOP代码

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. 我不明白为什么IntelliJ似乎是一个错误。

I think it isn't able to autowire the component because the class ModuleConfiguration where you are autowiring ModuleContextAOP isn't a component. 我认为它无法自动装配组件,因为您要自动装配ModuleContextAOP的ModuleConfiguration类不是组件。 Try annotating ModuleConfiguration with component and it should work. 尝试用组件注释ModuleConfiguration,它应该可以工作。 Usually @Configuration is used for defining beans but you are trying to autowire them. 通常,@ Configuration用于定义bean,但是您尝试将它们自动装配。

Your class ModuleConfiguration is not a Spring bean class hence dependencies on it cannot be autowired. 您的类ModuleConfiguration 不是Spring Bean类,因此无法自动关联对其的依赖关系。 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 . 您需要将类的bean条目放入XML文件中(在其中声明其他spring bean),或者用@component对其进行@component

@Component Indicates that an annotated class is a "component". @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. 或者,您可以使用@Configuration将所有spring bean扫描到一个类中@Configuration是Spring 3中引入的基于Java的配置机制的核心。它提供了基于XML的配置的替代方法。

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 . 在这两种情况下,Spring会在my.base.package及以下扫描带注释的类@Component或者是其他注释的一个荟萃标注有@Component@Service

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

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

to

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

Resolved. 解决。

Thank... 谢谢...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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