【问题标题】:How to use constructor injection in a main program如何在主程序中使用构造函数注入
【发布时间】:2016-05-27 09:30:57
【问题描述】:

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

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

A 类:

public class ClassA {

  public int x1;
  public String x2;

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

B 类:

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配置中?还是在主程序中?

非常感谢你:)

【问题讨论】:

  • 它是否有效取决于您的applicationContext.xml
  • “行得通吗?”尝试一下。但是不行,目前编译不了,这里有很多错别字。
  • 编译器错误 -> public void setA(Class a){
  • 谢谢!我知道 xml 配置:) 我的问题是这是否是使用构造函数注入的正确示例
  • 我的疑惑在于:System.out.println("x1="+a.x1); System.out.println("x2="+a.x2);

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


【解决方案1】:

关于您对以下方面的担忧:

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

x1 和 x2 都是私有变量,因此您将无法像以前那样使用 a.x1 或 a.x2。您需要 x1 和 x2 的公共吸气剂。没有看到您的 application.xml,很难再说什么。请给出一些可以运行的代码。

【讨论】:

  • 我已经修改了我的帖子,请你看看 :)
【解决方案2】:

将 applicationContext.xml 中的 aBean 修改为

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

【讨论】:

  • 好的,谢谢,主程序怎么样?这是: System.out.println("x1="+a.x1); System.out.println("x2="+a.x2);在主程序中获取属性的正确方法
  • 正确的方法是对 x1 和 x2 使用 getter,但是因为它们是公共的,所以你仍然可以使用这种方式:)
猜你喜欢
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 2011-05-11
  • 2019-05-15
  • 2018-11-02
相关资源
最近更新 更多