首页 > 解决方案 > 为什么我的配置类没有用@Configurable 和@ComponentScan 注释两次没有初始化bean?

问题描述

我读过

“@Configuration 类是 @Components 的事实也意味着,如果您在包含它们的包上启用了组件扫描,则会自动拾取您的 @Configuration 类。这可能会产生有意和无意的后果。如果您的 @Configuration 类启用组件扫描它所在的同一个包,你的 bean 可能会被实例化两次,这不好。”(Williams 346)

我不确定作者试图传达什么,因为没有任何具体的例子,经过一天的思考,我能尝试产生这个问题的最接近的代码是下面的代码,这对我来说可以在没有初始化任何 bean 的情况下正常工作(无论是通过@Bean 标签)还是通过@Component 标签,两次。

请注意,bean X 的目标代码是相同的,而且每个方法都只调用一次,包括构造函数。

package com.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

public class ConfigurationCSTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        System.out.println(context.getBean("config"));
        context.registerShutdownHook();
    }
}

@Configuration
@ComponentScan(basePackages = "com")
class Config {
     public Config() {
       System.out.println("called once");
    }

    @Bean
    public Object x() {
        final Object o = new Object();
        System.out.println("now creating bean " + o + " only called once");
        return o;
    }

    public String toString() {
        return "I am also a component";
    }
}

@Component
class ComponentY {
    @Autowired
    public ComponentY(Object x) {
        System.out.println("now printing bean " + x);
    }
}

以及运行 main 的结果

called once
now creating bean java.lang.Object@290222c1 only called once
now printing bean java.lang.Object@290222c1
I am also a component

但是,我收到一条错误消息,似乎是 spring 使这个问题已由 spring 修复

它说

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$1 (file:/home/someuser/.m2/repository/org/springframework/spring-core/5.0.2.RELEASE/spring-core-5.0.2.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

实际上,如果我在 spring 3.2.9.RELEASE 上尝试这个,它会与

May 06, 2018 3:44:12 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6f96c77: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,config,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [/home/someuser/dev/repl/target/classes/com/spring/Config.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [/home/someuser/dev/repl/target/classes/com/spring/Config.class]; nested exception is java.lang.IllegalArgumentException
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:290)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)
    at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:130)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:189)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:164)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:139)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:284)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:225)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
    at com.spring.ConfigurationCSTest.main(ConfigurationCSTest.java:12)
Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file [/home/someuser/dev/repl/target/classes/com/spring/Config.class]; nested exception is java.lang.IllegalArgumentException
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:56)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:266)
    ... 11 more
Caused by: java.lang.IllegalArgumentException
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:53)
    ... 14 more

所以我猜,我有作者试图传达的正确想法,但如果我没有,如果有人能向我展示一个可能导致威廉姆斯评论的问题浮出水面的演示程序,我将不胜感激。

标签: javaspringjavabeansconfigurablecomponent-scan

解决方案


这个问题已经不存在了。

作为证明 - 查看@SpringBootApplicationSpring Boot 中的注释。它本质上是与类本身在同一个包上的组合@Configuration@ComponentScan加上一些其他不相关的东西)。


推荐阅读