I have read that
"The fact that @Configuration classes are @Components also means that your @Configuration classes are picked up automatically if you have component scanning enabled on the package that contains them. This can have both intended and unintended consequences. If your @Configuration class enables component scanning on the same package it resides in, your beans could be instantiated twice, which isn’t good."(Williams 346)
I am not sure what the author was trying to convey, as there weren't any concrete examples and after a day of thinking the closest I can to try to produce this problem was the code below, which to me works fine without initializing any beans(whether through @Bean tag) or through @Component tag, twice.
Notice that the object code is the same for bean X, and also every single method is called only once, including the constructor.
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);
}
}
and the result of running 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
However, I am getting an error msg that seems like spring made this problem has been fixed by spring
it says
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
And actually, if I try this with spring 3.2.9.RELEASE, it will barf with
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
So I am guessing, I had the correct idea the author was trying to convey, but if I didn't I'd really appreciate if someone can show me a demo program that can cause the problem Williams commented on to surface.
This problem does not exist anymore.
As a proof - look at @SpringBootApplication
annotation from Spring Boot. It's essentially combination of @Configuration
and @ComponentScan
on the same package as the class itself (plus few other things not relevant here).