Autowire 在 Spring 中不工作
Autowire not working in Spring
我有以下 class:
public class Triangle implements Shape {
/*@Autowired
@Qualifier("A")*/
private Point A;
/*@Autowired
@Qualifier("B")*/
private Point B;
/*@Autowired
@Qualifier("C")*/
private Point C;
private static AtomicInteger id = new AtomicInteger();
private int ownId;
private int beanAttribute;
@SuppressWarnings("unused")
private int secondaryId;
private String constructor = "Simple type --";
private static final String NAME = "triangle";
Triangle() {
System.out.println("Triangle created without a secondaryId \n");
secondaryId = -1;
}
Triangle(int secondaryId) {
this.secondaryId = secondaryId;
this.ownId = id.addAndGet(1);
}
Triangle(int secondaryId, String constructor) {
this.secondaryId = secondaryId;
this.ownId = id.addAndGet(1);
this.constructor = constructor;
}
public Point getA() {
return A;
}
public void setA(Point a) {
A = a;
}
public Point getB() {
return B;
}
public void setB(Point b) {
B = b;
}
public Point getC() {
return C;
}
public void setC(Point c) {
C = c;
}
public int getOwnId() {
return ownId;
}
}
使用以下 XML 配置:
<beans>
<bean id="triangleDouble" class="draw.Triangle"
scope="prototype" autowire="byName">
<constructor-arg index="1" value="Double type ++" />
<constructor-arg index="0" value="0" />
<property name="beanAttribute" value="2" />
</bean>
<bean id="triangleSimple" class="draw.Triangle"
scope="prototype" autowire="byName">
<constructor-arg value="1" />
<property name="beanAttribute" value="1" />
</bean>
<bean id="A" class="draw.Point">
<property name="x" value="1" />
<property name="y" value="2" />
</bean>
<bean id="B" class="draw.Point">
<property name="x" value="2" />
<property name="y" value="3" />
</bean>
<bean id="C" class="draw.Point">
<property name="x" value="3" />
<property name="y" value="4" />
</bean>
</beans>
问题是自动装配不工作。如果我设置 <property name="A" ref="A"/>
等,将不会出现 NullPointerException 并且我可以访问 Point.x 或 Point.y,因此代码编写正确,但如果我按名称自动装配,自动装配将不起作用并且当我尝试从 Triangle 访问 Point.x 时,我得到一个 java.lang.NullPointerException
.
我尝试将 class 成员 A、B、C 设置为 @Autowire
,都设置为 @Autowire
和 @Qualifier("A")
,并且根本没有注释,但仍然没用。
你的程序运行后的错误:
Bean property 'beanAttribute' is not writable or has an invalid setter
method. Does the parameter type of the setter match the return type of
the getter?
<context:annotation-config />
<bean id="triangleDouble" bean id="triangleDouble" class="draw.Triangle"
scope="prototype" autowire="byName">
<constructor-arg index="1" value="Double type ++" />
<constructor-arg index="0" value="0" />
<property name="beanAttribute" value="2" />
</bean>
您错过了 beanAttribute.Add 的 setter 方法,它应该有效。
有关详细信息,请参阅 setter-injection
将您的 Point bean 名称重命名为小写,并将 'setA()'、'setB' 等设置器添加到三角形 class。它会解决问题。
问题是您没有遵循 JavaBeans 约定。
我有以下 class:
public class Triangle implements Shape {
/*@Autowired
@Qualifier("A")*/
private Point A;
/*@Autowired
@Qualifier("B")*/
private Point B;
/*@Autowired
@Qualifier("C")*/
private Point C;
private static AtomicInteger id = new AtomicInteger();
private int ownId;
private int beanAttribute;
@SuppressWarnings("unused")
private int secondaryId;
private String constructor = "Simple type --";
private static final String NAME = "triangle";
Triangle() {
System.out.println("Triangle created without a secondaryId \n");
secondaryId = -1;
}
Triangle(int secondaryId) {
this.secondaryId = secondaryId;
this.ownId = id.addAndGet(1);
}
Triangle(int secondaryId, String constructor) {
this.secondaryId = secondaryId;
this.ownId = id.addAndGet(1);
this.constructor = constructor;
}
public Point getA() {
return A;
}
public void setA(Point a) {
A = a;
}
public Point getB() {
return B;
}
public void setB(Point b) {
B = b;
}
public Point getC() {
return C;
}
public void setC(Point c) {
C = c;
}
public int getOwnId() {
return ownId;
}
}
使用以下 XML 配置:
<beans>
<bean id="triangleDouble" class="draw.Triangle"
scope="prototype" autowire="byName">
<constructor-arg index="1" value="Double type ++" />
<constructor-arg index="0" value="0" />
<property name="beanAttribute" value="2" />
</bean>
<bean id="triangleSimple" class="draw.Triangle"
scope="prototype" autowire="byName">
<constructor-arg value="1" />
<property name="beanAttribute" value="1" />
</bean>
<bean id="A" class="draw.Point">
<property name="x" value="1" />
<property name="y" value="2" />
</bean>
<bean id="B" class="draw.Point">
<property name="x" value="2" />
<property name="y" value="3" />
</bean>
<bean id="C" class="draw.Point">
<property name="x" value="3" />
<property name="y" value="4" />
</bean>
</beans>
问题是自动装配不工作。如果我设置 <property name="A" ref="A"/>
等,将不会出现 NullPointerException 并且我可以访问 Point.x 或 Point.y,因此代码编写正确,但如果我按名称自动装配,自动装配将不起作用并且当我尝试从 Triangle 访问 Point.x 时,我得到一个 java.lang.NullPointerException
.
我尝试将 class 成员 A、B、C 设置为 @Autowire
,都设置为 @Autowire
和 @Qualifier("A")
,并且根本没有注释,但仍然没用。
你的程序运行后的错误:
Bean property 'beanAttribute' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
<context:annotation-config />
<bean id="triangleDouble" bean id="triangleDouble" class="draw.Triangle"
scope="prototype" autowire="byName">
<constructor-arg index="1" value="Double type ++" />
<constructor-arg index="0" value="0" />
<property name="beanAttribute" value="2" />
</bean>
您错过了 beanAttribute.Add 的 setter 方法,它应该有效。
有关详细信息,请参阅 setter-injection
将您的 Point bean 名称重命名为小写,并将 'setA()'、'setB' 等设置器添加到三角形 class。它会解决问题。
问题是您没有遵循 JavaBeans 约定。