Steps to reproduce
Scenario 1: : Singleton beans 1. Create a Java Pojo
public class Reader {
public Reader() {
}
}
- Create AnnotationConfigApplicationContext by supplying Pojo class like below
public class Application {
public static void main(String[] args) {
try(var context = new AnnotationConfigApplicationContext(Reader.class, JLabel.class)) {
System.out.println("Get singleton reader bean: "+context.getBean("reader").hashCode());
System.out.println("Get singleton reader bean: "+context.getBean("reader").hashCode());
System.out.println("Get singleton reader bean: "+context.getBean("reader").hashCode());
System.out.println("Get singleton reader bean: "+context.getBean("reader").hashCode());
// Another example on JLABEL class
System.out.println("Get singleton JLabel bean: "+context.getBean("JLabel").hashCode());
System.out.println("Get singleton JLabel bean: "+context.getBean("JLabel").hashCode());
}catch(Exception e) {
System.err.print(e);
}
}
}
Here is a sample output
Get singleton reader bean: 1048855692 Get singleton reader bean: 1048855692 Get singleton reader bean: 1048855692 Get singleton reader bean: 1048855692 Get singleton jlabel bean: 2119891622 Get singleton jlabel bean: 2119891622
**Why does AnnotationConfigApplicationContext create a bean from a POJO, this behavior is not mentioned anywhere is document either. Is this the default behavior ? **
Scenario 2: : Configuration class without @Configuration
Another example with a configuration class without @configuration Annotation like below
- Create a POJO
public class AnotherPojo {
public AnotherPojo() {
}
}
- Create config class without @Configuration.
public class SomeConfiguration {
@Bean
public AnotherPojo anothe context.close();rPojo() {
return new AnotherPojo();
}
}
- Create AnnotationConfigApplicationContext
public class Application {
public static void main(String[] args) {
try(var context = new AnnotationConfigApplicationContext(SomeConfiguration.class)) {
// through config class without @Configuration
System.out.println("Get singleton anotherPojo through config: "+context.getBean("anotherPojo").hashCode());
System.out.println("Get singleton anotherPojo through config: "+context.getBean("anotherPojo").hashCode());
}catch(Exception e) {
System.err.print(e);
}
}
}
This is the output
Get singleton anotherPojo through config: 739973450 Get singleton anotherPojo through config: 739973450
Scenario 3: Prototype bean with only @Scope
- create a class with only @Scope
@Scope("prototype")
public class Writer {
public Writer() {
}
}
- Create AnnotationConfigApplicationContext
public class Application {
public static void main(String[] args) {
try (var context = new AnnotationConfigApplicationContext(Writer.class)) {
System.out.println("Get prototype writer bean: "+context.getBean("writer"));
System.out.println("Get prototype writer bean: "+context.getBean("writer")
System.out.println("Get prototype writer bean: "+context.getBean("writer"));
} catch (Exception e) {
System.err.print(e);
}
}
Here is the sample output:
Get prototype writer bean: org.entitylabs.beans.Writer@44ebcd03 Get prototype writer bean: org.entitylabs.beans.Writer@6356695f Get prototype writer bean: org.entitylabs.beans.Writer@4f18837a
Spring version used :
Also, source code is attachment for all three scenarios
01-singleton-beans-test.zip 02-configuration-bean.zip 03-prototype-beans.zip
Comment From: entitylabs
Spring version used :
Comment From: snicoll
The Javadoc of AnnotationConfigApplicationContext
states:
Create a new AnnotationConfigApplicationContext, deriving bean definitions from the given component classes and automatically refreshing the context.
What you call "POJO" (I assume non annotated types) are no different from a service or anything that the context should handle if told so. The second case is what's called lite mode. As for the last one, this is also working as expected, the scope being read from the component like any other stereotype.
Going forward, please ask questions on StackOverflow, as mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements.