gpt4 book ai didi

java - 在 Bean 类中使用 ApplicationContextAware 来初始化原型(prototype)作用域 bean 成员

转载 作者:搜寻专家 更新时间:2023-10-31 20:25:02 25 4
gpt4 key购买 nike

我正在学习 spring,我只是在玩 ApplicationContextAware 和 bean 作用域。

我将附上代码,然后描述我想要做什么。

到目前为止,我已经创建了一个

点类:

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;
}

}

我创建了一个 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;
}
}

下面是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>

这是主类

public class DrawingApp {

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

t.draw();
}
}

因此,您会看到 point1point2point3 这三个 bean 的 bean 作用域是 prototype,但是它们都是bean triangle的成员,在spring.xml

中默认作用域是 singleton

所以,我的假设是,这应该行不通,除非我能以某种方式在我的 Triangle 类中获取 ApplicationContext 并设置 pointApointB, pointC 就像我在他们的 setter 方法中使用 ApplicationContext.

所做的一样

不确定这是不是正确的方法,或者这个东西在实时应用程序中有多大用处。

所以,如果以上所有代码都有效,我应该得到如下三行输出:

x: 0,y: 0
x: 20,y: 20
x: -20,y: 20

但是当我运行 main 方法时,出现以下错误:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (3) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'pointA' threw exception; nested exception is java.lang.NullPointerException PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'pointB' threw exception; nested exception is java.lang.NullPointerException PropertyAccessException 3: org.springframework.beans.MethodInvocationException: Property 'pointC' threw exception; nested exception is java.lang.NullPointerException Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (3) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'pointA' threw exception; nested exception is java.lang.NullPointerException PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'pointB' threw exception; nested exception is java.lang.NullPointerException PropertyAccessException 3: org.springframework.beans.MethodInvocationException: Property 'pointC' threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1685) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85) at org.java.learning.DrawingApp.main(DrawingApp.java:9) Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (3) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'pointA' threw exception; nested exception is java.lang.NullPointerException PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'pointB' threw exception; nested exception is java.lang.NullPointerException PropertyAccessException 3: org.springframework.beans.MethodInvocationException: Property 'pointC' threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:122) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:77) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.app lyPropertyValues(AbstractAutowireCapableBeanFactory.java:1681) ... 13 more

我认为我在设置pointA、pointB 和pointC 成员变量的值时做错了什么

如果我需要任何详细信息,请告诉我,以消除此错误并实现我正在尝试做的事情。

最佳答案

独立于您面临的错误:让我们看看您的类(class)设置:

您希望您的观点成为原型(prototype)。所以每次你通过注入(inject)请求一个新的 Bean 时,Spring 都会给你一个新的实例。

现在你将你的原型(prototype) bean 注入(inject)到你的 Triangle bean 中,它默认是单例的(这意味着它只被请求和创建一次)。因此,原型(prototype) bean (Point) 也被请求一次,以将它们注入(inject)到单例 Triangle bean 中。

从现在开始,您的原型(prototype) bean 不再像原型(prototype) bean 那样工作,这意味着它们从您的 Triangle bean 继承单例作用域。

但是在这个设置上你没问题,但你应该牢记这个陷阱:只有当从容器/上下文请求 beans 时,Spring 才会创建原型(prototype) beans 的新实例。

关于java - 在 Bean 类中使用 ApplicationContextAware 来初始化原型(prototype)作用域 bean 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156992/

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