Constructor Caller In Context Config : Constructor Injection « Spring « Java Tutorial






File: context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
   <bean id="testBean" class="ConstructorTestBean">
      <constructor-arg value="Steven Devijver"/>
<!--
      <constructor-arg value="1"/>
-->
      <constructor-arg value="1" type="java.lang.Integer"/>
   </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) throws Exception {
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    ConstructorTestBean testBean = (ConstructorTestBean) beanFactory.getBean("testBean");

    System.out.println(testBean.isConstructor1Used());

    System.out.println(testBean.isConstructor2Used());

  }
}

class ConstructorTestBean {
  private boolean constructor1Used = false;

  private boolean constructor2Used = false;

  public ConstructorTestBean(String name, Integer id) {
    this.constructor1Used = true;
  }

  public ConstructorTestBean(String firstName, String lastName) {
    this.constructor2Used = true;
  }

  public boolean isConstructor1Used() {
    return this.constructor1Used;
  }

  public boolean isConstructor2Used() {
    return this.constructor2Used;
  }
}
  Download:  Spring-ConstructorCallerInContextConfig.zip( 2,599 k)








28.9.Constructor Injection
28.9.1.XML Based Bean Configuration: Constructor Injection
28.9.2.Constructor Confusion Demo
28.9.3.Constructor Caller In Context Config
28.9.4.Constructor Argument And Local Reference