Search code examples
javadependency-injectionjava-ee-6constructor-injectionspring-ioc

How to use constructor injection in a main program


my question is verry simple: using the constructor injection, how can I get the attributes of a class ClassA declared as attribut in a class ClassB, knowing that the ClassA has just a constructur (no setter and getter)?

here i will write my java code :

ClassA:

public class ClassA {

  public int x1;
  public String x2;

  ClassA(int x1, String x2) {
    this.x1 = x1;
    this.x2 = x2;
  }
}

ClassB:

public class ClassB {

  private ClassA a;
  private String y1;
  private String y2;

  public ClassA getA() {
    return a;
  }

  public void setA(Class a) {
    this.a = a;
  }

  public String getY1() {
    return y1;
  }

  public void setY1(String y1) {
    this.y1 = y1;
  }

  public String getY2() {
    return y2;
  }

  public void setY2(String y2) {
    this.y2 = y2;
  }
}

the main program:

public class ConstructorInjection {
    public static void main(String args[]){
        Resource xmlResource = new FileSystemResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(xmlResource);
        ClassB b= (ClassB)factory.getBean("bBean");
        ClassA a= b.getA();
        System.out.println("y1="+b.getY1);
        System.out.println("y2="+b.getY2);
        System.out.println("x1="+a.x1);
        System.out.println("x2="+a.x2);
    }
}

is it the right example of practicing constructor injection?

Xml configuration:

applicationContext.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="bBean" class="javabeat.net.spring.ioc.ClassB">
        <property name="y1" value="expy1" />
        <property name="y2" value="expy2" />
        <property name="a" ref="aBean" />
    </bean>

    <bean id="aBean" class="javabeat.net.spring.ioc.ClassA">
        <constructor-arg name="x1" type="java.lang.int" value="0"/>
        <constructor-arg name="x2" type="java.lang.String" value="exp"/>
    </bean>

</beans>

this part is what bothers me:

System.out.println("x1="+a.x1);
System.out.println("x2="+a.x2);

not sure if that's the right way to get the attributes of ClassA! i have read that constructor injection enforce the initialisation of attributes, but where? in the xml configuration? or in the main program?

And thank you so much :)


Solution

  • Regarding your concern about the following:

    System.out.println("x1="+a.x1); System.out.println("x2="+a.x2);
    

    Both x1 are x2 are private variables, so you will not be able to use a.x1 or a.x2 like you did. You need public getter for x1 and x2. Without seeing your application.xml, it is difficult to say anything more. Please give some code which can be run.