gpt4 book ai didi

java - spring Autowiring 上的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 09:50:33 24 4
gpt4 key购买 nike

我为 Spring Autowiring 编写了一个练习问题,并得到了 NullPointerException

这是我的代码
界面:形状

public interface Shape {
public void draw();
}

类别:三角形

    public class Triangle implements Shape{

private Point A;
private Point B;
private Point C;

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 void draw(){
System.out.println("PointA("+ A.getX() + "," + A.getY() +")");
System.out.println("PointB("+ B.getX() + "," + B.getY() +")");
System.out.println("PointC("+ C.getX() + "," + C.getY() +")");
}
}

类别:分数

public class Point {

private int x,y;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

}

Spring 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="triangle" class="org.demo.javabrains.Triangle" autowire="byName">
</bean>

<bean id="A" class="org.demo.javabrains.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>

<bean id="B" class="org.demo.javabrains.Point">
<property name="x" value="10"/>
<property name="y" value="20"/>
</bean>

<bean id="C" class="org.demo.javabrains.Point">
<property name="x" value="30"/>
<property name="y" value="40"/>
</bean>
</beans>

主类

public class DrawingApp {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Shape triangle =(Shape) context.getBean("triangle");
triangle.draw();

}

}

我不知道为什么会出现 NullPointer 异常当我尝试运行此代码时,它会抛出异常

INFO: Loading XML bean definitions from class path resource [spring.xml]
Exception in thread "main" java.lang.NullPointerException
at org.demo.javabrains.Triangle.draw(Triangle.java:34)
at org.demo.javabrains.DrawingApp.main(DrawingApp.java:16)

A点被赋值为null,不知道为什么

最佳答案

当您使用按属性名称 Autowiring 时:

<bean id="triangle" class="org.demo.javabrains.Triangle" autowire="byName" />

Spring 查找与需要 Autowiring 的属性同名的 bean。例如,如果 bean 定义设置为 Autowiring byName,并且它包含名为 a 的属性,即它具有 setA(..) 方法(不要与名为 A 的字段混淆),Spring 查找名为 a 的 bean 定义,并使用它来设置属性。

正如 @Tunaki 在评论中正确指出的那样,您将属性名称与字段本身的名称(即 A)混淆了。无论如何,如果您为 Point beans 使用小写名称,那就没问题:

<bean id="a" class="org.demo.javabrains.Point"> /* Same as before */ </bean>

<bean id="b" class="org.demo.javabrains.Point"> /* Same as before */ </bean>

<bean id="c" class="org.demo.javabrains.Point"> /* Same as before */ </bean>

有关 Spring Autowiring 协作者的更详细讨论,请查看 documentation .

关于java - spring Autowiring 上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com