java Autowire在春季不工作
我有以下课程:
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,我可以访问该点。x或点。y、 因此,代码编写正确,但如果我按名称自动连线,自动连线将无法工作,我将尝试何时访问该点。从三角形中得到一个java.lang.NullPointerException
我试图用@Autowire
设置类成员A、B、C,同时使用@Autowire
和@Qualifier("A")
并且完全没有注释,但仍然不起作用
共 (0) 个答案