Announcement Announcement Module
Collapse
No announcement yet.
Constructor injection, int vs Integer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Constructor injection, int vs Integer

    Hi, I have a simple class:

    Code:
    public class TestBean1 {
        private String info;
    
        public TestBean1(String v) {
            info = "Constructor: TestBean1(String)";
        }
    
        public TestBean1(int v) {
            info = "Constructor: TestBean1(int)";
        }
    
        public TestBean1(Integer v) {
            info = "Constructor: TestBean1(Integer)";
        }
    
        public TestBean1(Number v) {
            info = "Constructor: TestBean1(Number)";
        }
    
        public String toString() {
            return info;
        }
    }
    and the following XML configuration (only the relevant part):

    Code:
        <bean id="anInteger" class="java.lang.Integer">
            <constructor-arg value="123456"/>
        </bean>
    
        <bean id="bean1_a" class="TestBean1">
            <constructor-arg value="123"/>
        </bean>
    
        <bean id="bean1_b" class="TestBean1">
            <constructor-arg type="int" value="123"/>
        </bean>
    
        <bean id="bean1_c" class="TestBean1">
            <constructor-arg type="java.lang.Integer" value="123"/>
        </bean>
    
        <bean id="bean1_d" class="TestBean1">
            <constructor-arg ref="anInteger"/>
        </bean>
    
        <bean id="bean1_e" class="TestBean1">
            <constructor-arg type="java.lang.Integer" ref="anInteger"/>
        </bean>
    
        <bean id="bean1_f" class="TestBean1">
            <constructor-arg type="java.lang.Number" ref="anInteger"/>
        </bean>
    
        <bean id="bean1_g" class="TestBean1">
            <constructor-arg type="int" ref="anInteger"/>
        </bean>
    and this in a test class:

    Code:
            XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("constructor-arg-test.xml"));
    
            String[] beanNames = {
                "bean1_a", "bean1_b", "bean1_c", "bean1_d", "bean1_e",
                "bean1_f", "bean1_g"
            };
    
            for (String beanName : beanNames) {
                System.out.print("bean \"" + beanName + "\" --> ");
                System.out.println(factory.getBean(beanName));
            }
    The output is:

    Code:
    bean "bean1_a" --> Constructor: TestBean1(String)
    bean "bean1_b" --> Constructor: TestBean1(int)
    bean "bean1_c" --> Constructor: TestBean1(Integer)
    bean "bean1_d" --> Constructor: TestBean1(int)
    bean "bean1_e" --> Constructor: TestBean1(Integer)
    bean "bean1_f" --> Constructor: TestBean1(Number)
    bean "bean1_g" --> Constructor: TestBean1(int)
    It's all quite clear to me ... except for one thing: the bean "bean1_d". Since the ref is to an Integer object, initially I would have expected that TestBean1(Integer) was called. But instead TestBean1(int) is called.

    Why? I could think that conversion to a primitive is preferred over an "identity" assignment, is it right?
    Thank.
    Last edited by andbin; May 5th, 2012, 08:08 AM.
Working...
X