如何在主程序中使用构造函数注入

时间:2016-05-27 09:30:58

标签: java dependency-injection java-ee-6 constructor-injection spring-ioc

我的问题很简单: 使用构造函数注入,我如何获得在类ClassB中声明为attribut的类ClassA的属性,知道ClassA只有一个构造函数(没有setter和getter)?

这里我将编写我的java代码:

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;
  }
}

主程序:

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);
    }
}

它是练习构造函数注入的正确示例吗?

Xml配置:

的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>

这部分困扰我:

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

不确定这是否是获得ClassA属性的正确方法! 我已经读过构造函数注入强制执行属性的初始化,但在哪里?在xml配置?还是在主程序中?

非常感谢你:)。

2 个答案:

答案 0 :(得分:0)

关于您对以下问题的关注:

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

x1都是x2是私有变量,所以你不能像你那样使用a.x1或a.x2。你需要x1和x2的公共getter。如果没有看到你的application.xml,就很难再说了。请提供一些可以运行的代码。

答案 1 :(得分:0)

将applicationContext.xml中的aBean修改为

^[a-z A-Z]+$---> this one is allowing any word names  
^[a-zA-Z\s]+$  
^[a-zA-Z\\s]+$  
^[a-z A-Zs]+$,  
^([a-zA-Z]\s[a-zA-Z])+$ ---> this one is allowing only three word or more.