我正在学习春天,我只是在ApplicationContextAware和豆荚一起玩.
我将附上代码,然后描述我想要做的事情.
到目前为止,我已经创建了一个
点类:
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;
}
@Override
public String toString() {
return "x: "+this.x+",y: "+this.y;
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个Triangle类,它有3个Point类的实例:
public class Triangle implements ApplicationContextAware, BeanNameAware{
private Point pointA;
private Point pointB;
private Point pointC;
private String beanName;
private ApplicationContext context=null;
public Point getPointA() {
return pointA;
}
public void setPointA(Point pointA) {
this.pointA = (Point)this.context.getBean("point1");
}
public Point getPointB() {
return pointB;
}
public void setPointB(Point pointB) {
this.pointB = (Point)this.context.getBean("point2");
}
public Point getPointC() {
return pointC;
}
public void setPointC(Point pointC) {
this.pointC = (Point)this.context.getBean("point3");
}
public void draw() {
System.out.println("BeanName is: "+this.beanName);
System.out.println(pointA);
System.out.println(pointB);
System.out.println(pointC);
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
this.context = arg0;
}
@Override
public void setBeanName(String arg0) {
this.beanName = arg0;
}
}
Run Code Online (Sandbox Code Playgroud)
下面是spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 5.1.1//EN" "http://www.springframework.org/dtd/spring-beans-5.1.1.dtd">
<beans>
<bean id="triangle" class="org.java.learning.Triangle" >
<property name="pointA" ref="point1" />
<property name="pointB" ref="point2" />
<property name="pointC" ref="point3" />
</bean>
<bean id="point1" class="org.java.learning.Point" scope="prototype">
<property name="x" value="0" />
<property name="y" value="0" />
</bean>
<bean id="point2" class="org.java.learning.Point" scope="prototype">
<property name="x" value="20" />
<property name="y" value="20" />
</bean>
<bean id="point3" class="org.java.learning.Point" scope="prototype">
<property name="x" value="-20" />
<property name="y" value="20" />
</bean>
Run Code Online (Sandbox Code Playgroud)
这是主类:
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle t = (Triangle) context.getBean("triangle");
t.draw();
}
}
Run Code Online (Sandbox Code Playgroud)
所以,你会看到的三种豆是绿豆范围point1,point2并且point3是prototype的,但他们的成员bean triangle,默认情况下,其范围是singleton在spring.xml
所以,我的假设是,这不应该工作,除非我能以某种方式得到ApplicationContext我的Triangle阶级和设置pointA,pointB,pointC分别就像我在他们的setter方法进行使用ApplicationContext.
不确定这是否正确,或者在实时应用程序中这个用途有多大用处.
所以,如果以上所有代码都有效,我应该得到三行输出,如:
x: 0,y: 0
x: 20,y: 20
x: -20,y: 20
Run Code Online (Sandbox Code Playgroud)
但是当我运行main方法时,我得到以下错误:
警告:在上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建类路径资源[spring.xml]中定义名称为"triangle"的bean时出错:设置属性值时出错; 嵌套异常是org.springframework.beans.PropertyBatchUpdateException; 嵌套的PropertyAccessExceptions(3)是:PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'pointA'抛出异常; 嵌套异常是java.lang.NullPointerException PropertyAccessException 2:org.springframework.beans.MethodInvocationException:属性'pointB'抛出异常; 嵌套异常是java.lang.NullPointerException PropertyAccessException 3:org.springframework.beans.MethodInvocationException:属性'pointC'抛出异常; 嵌套的异常是在线程"主" org.springframework.beans.factory.BeanCreationException显示java.lang.NullPointerException例外:错误创建与类路径资源[spring.xml]定义的名称"三角"豆:错误设置属性值; 嵌套异常是org.springframework.beans.PropertyBatchUpdateException; 嵌套的PropertyAccessExceptions(3)是:PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'pointA'抛出异常; 嵌套异常是java.lang.NullPointerException PropertyAccessException 2:org.springframework.beans.MethodInvocationException:属性'pointB'抛出异常; 嵌套异常是java.lang.NullPointerException PropertyAccessException 3:org.springframework.beans.MethodInvocationException:属性'pointC'抛出异常; 嵌套的异常是在显示java.lang.NullPointerException org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1685)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)位于org.springframework.beans.factory的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) .support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)位于org.springframework.beans.factory.support.AbstractBeanFactory的org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222). org.springframework.beans.factory.support中的doGetBean(AbstractBeanFactory.java:318).位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)的org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)中的AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144)org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext) .java:85)at org.java.learning.DrawingApp.main(DrawingApp.java:9)引起:org.springframework.beans.PropertyBatchUpdateException; 嵌套的PropertyAccessExceptions(3)是:PropertyAccessException 1:org.springframework.beans.MethodInvocationException:属性'pointA'抛出异常; 嵌套异常是java.lang.NullPointerException PropertyAccessException 2:org.springframework.beans.MethodInvocationException:属性'pointB'抛出异常; 嵌套异常是java.lang.NullPointerException PropertyAccessException 3:org.springframework.beans.MethodInvocationException:属性'pointC'抛出异常; 嵌套异常是org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122)中的java.lang.NullPointerException,位于org.springframework.beans.factory的org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77) .support.AbstractAutowireCapableBeanFactory.app lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1681)... 13更多
我相信在设置pointA,pointB和pointC成员变量的值时我做错了
如果我方需要任何细节,请告诉我,以摆脱这个错误并实现我想要做的事情.
有两个原因导致您的代码抛出异常\xef\xbc\x9a
\n\nnew ClassPathXmlApplicationContext("spring.xml")方法无法知道类上下文已准备好。ApplicationContext需要成功创建所有bean,但是Triangle需要一个上下文。您可以使用 IOC 来解决您的问题。
\n| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |