嗨,我是spring的新手,我正在尝试spring中的基本程序。我的代码模板是
SpringDemo.java
public class SpringDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle=(Triangle)context.getBean("triangle");
triangle.draw();
}
}
Triangle.java
package org.amulya.springs;
public class Triangle {
private Point pointA;
private Point pointB;
private Point pointC;
public Point getPointA() {
return pointA;
}
public void setPointA(Point pointA) {
this.pointA = pointA;
}
public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
public Point getPointC() {
return pointC;
}
public void setPointC(Point pointC) {
this.pointC = pointC;
}
public void draw() {
System.out.println("pointA is (" + getPointA().getX() + "," + getPointA().getY() + ")");
System.out.println("pointB is (" + getPointB().getX() + "," + getPointB().getY() + ")");
System.out.println("pointC is (" + getPointC().getX() + "," + getPointC().getY() + ")");
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="org.amulya.springs.Triangle">
<property name="pointA" ref="zeropoint"/>
<property name="pointB" ref="point1"/>
<property name="pointC" ref="point2"/>
</bean>
<bean id="zeropoint" class="org.amulya.springs.Point">
<property name="x" value="0"/>
<property name="x" value="0"/>
</bean>
<bean id="point1" class="org.amulya.springs.Point">
<property name="x" value="-20"/>
<property name="x" value="0"/>
</bean>
<bean id="point2" class="org.amulya.springs.Point">
<property name="x" value="20"/>
<property name="x" value="0"/>
</bean>
</beans>
point.java
package org.amulya.springs;
public class Point {
private int x;
private int 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:/SpringDemo/src/spring.xml
例外:
在类路径中定义名称为“triangle”的bean时出错 resource [spring.xml]:无法解析匹配的构造函数(提示: 为简单参数指定索引/类型/名称参数以避免类型 模糊度)
请帮帮我
答案 0 :(得分:0)
<bean id="zeropoint" class="org.amulya.springs.Point">
<property name="x" value="0"/>
<property name="x" value="0"/>
</bean>
<bean id="point1" class="org.amulya.springs.Point">
<property name="x" value="-20"/>
<property name="x" value="0"/>
</bean>
<bean id="point2" class="org.amulya.springs.Point">
<property name="x" value="20"/>
<property name="x" value="0"/>
</bean>
此代码中的将属性名称更改为 X和Y
错误是由于歧义造成的