Java 如何在主程序中使用构造函数注入
我的问题非常简单: 使用构造函数注入,我如何在知道类a只有一个构造函数(没有setter和getter)的情况下,获得类B中声明为attribute的类a的属性 在这里,我将编写我的java代码: A类:Java 如何在主程序中使用构造函数注入,java,dependency-injection,java-ee-6,constructor-injection,spring-ioc,Java,Dependency Injection,Java Ee 6,Constructor Injection,Spring Ioc,我的问题非常简单: 使用构造函数注入,我如何在知道类a只有一个构造函数(没有setter和getter)的情况下,获得类B中声明为attribute的类a的属性 在这里,我将编写我的java代码: A类: public class ClassA { public int x1; public String x2; ClassA(int x1, String x2) { this.x1 = x1; this.x2 = x2; } } B类: public cl
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>
不确定这是否是获取ClassA属性的正确方法!
我已经读到构造函数注入强制初始化属性,但是在哪里呢?在xml配置中?还是在主程序中
非常感谢:)关于您对以下问题的关注:
System.out.println("x1="+a.x1); System.out.println("x2="+a.x2);
x1和x2都是私有变量,因此您将无法像以前那样使用a.x1或a.x2。x1和x2需要公共getter。如果看不到application.xml,就很难再多说了。请给出一些可以运行的代码。将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>
它是否有效取决于您的applicationContext.xml
“是否有效?”请尝试。但是不,它目前不会编译,这里有很多拼写错误。编译器错误在->public void setA(Class a){谢谢!我知道xml配置:)我的问题是,这是否是使用构造函数注入的正确示例我的疑问在于:System.out.println(“x1=“+a.x1”);System.out.println(“x2=“+a.x2”);我已经修改了我的帖子,请看一看(plz:)好的,谢谢,主程序是什么?它正确吗?这是:System.out.println(“x1=“+a.x1”);System.out.println(“x2=“+a.x2”);在主程序中获取属性的正确方法正确方法是对x1和x2使用getter,但因为它们是公共的,所以仍然可以使用这种方法:)
<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>