Constructor Confusion Demo
File: context.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="constructorConfusion" class="ConstructorConfusionDemo"> <constructor-arg value="1" type="int"/> </bean> </beans> File: Main.java import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class Main { public static void main(String[] args) { BeanFactory factory = new XmlBeanFactory(new ClassPathResource( "context.xml")); ConstructorConfusionDemo cc = (ConstructorConfusionDemo) factory .getBean("constructorConfusion"); System.out.println(cc); } } class ConstructorConfusionDemo { private String someValue; public ConstructorConfusionDemo(String someValue) { System.out.println("ConstructorConfusionDemo(String) called"); this.someValue = someValue; } public ConstructorConfusionDemo(int someValue) { System.out.println("ConstructorConfusionDemo(int) called"); this.someValue = "Number: " + Integer.toString(someValue); } public String toString() { return someValue; } }
1. | Call Constructor | ![]() | |
2. | ConstructorCaller In ContextConfig | ![]() | |
3. | Constructor Argument And Local Reference | ![]() | |
4. | SingletonScope And PrototypeScope | ![]() |