我已经读过那个
“@Configuration类是@Components这一事实也意味着如果你在包含它们的软件包上启用了组件扫描,你的@Configuration类会被自动拾取。这可能会产生预期和意外的后果。如果你的@Configuration class允许组件扫描它所在的同一个包,你的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修复了
它说
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
实际上,如果我尝试使用弹簧3.2.9.RELEASE,它将使用
进行barfMay 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
所以我猜,我有正确的想法,作者试图表达,但如果我没有,我真的很感激,如果有人可以给我一个演示程序,可以导致威廉姆斯评论的问题浮出水面。
答案 0 :(得分:2)
此问题不再存在。
作为证明 - 查看Spring Boot中的@SpringBootApplication
注释。它基本上是@Configuration
和@ComponentScan
在同一个包上与类本身的组合(加上其他一些与此无关的东西)。