在singleton bean中引用原型bean时获取Bean Creation错误

时间:2014-08-31 19:27:04

标签: spring

在单例bean中引用原型bean时出现Bean Creation错误请查看我的以下代码快照

Sep 1, 2014 12:36:03 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@100ab23: startup date [Mon Sep 01 00:36:03 IST 2014]; root of context hierarchy
Sep 1, 2014 12:36:03 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Sep 1, 2014 12:36:03 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5dcec6: defining beans [triangle,pointA,pointB,pointC,circle]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pointA' defined in class path resource [spring.xml]: 1 constructor arguments specified but no matching constructor found in bean 'pointA' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:175)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087)
    at org.koushik.javabrrians.Triangle.getPointA(Triangle.java:18)
    at org.koushik.javabrrians.Triangle.draw(Triangle.java:46)
    at org.koushik.javabrrians.DrawingApp.main(DrawingApp.java:26)

My Triangle课程就像这样

package org.koushik.javabrrians;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class Triangle implements ApplicationContextAware {

    private Point pointA;
    private Point pointB;
    private Point pointC;

    ApplicationContext context = null;

    public Point getPointA() {
        //return pointA;
        return (Point) context.getBean("pointA" , "Point.class");
    }

    public void setPointA(Point pointA) {
        this.pointA = pointA;
    }

    public Point getPointB() {
        //return pointB;
        return (Point) context.getBean("pointB" , "Point.class");
    }

    public void setPointB(Point pointB) {
        this.pointB = pointB;
    }

    public Point getPointC() {
        //return pointC;
        return  (Point) context.getBean("pointC" , "Point.class");
    }

    public void setPointC(Point pointC) {
        this.pointC = pointC;
    //  context.getBean("pointC");
    }

    public void draw() {

        System.out.println("PoinA  X = " + getPointA().getX() + " Y = " + getPointA().getY());
        System.out.println("PoinB  X = " + getPointB().getX() + " Y = " + getPointB().getY());
        System.out.println("PoinC  X = " + getPointC().getX() + " Y = " + getPointC().getY());

    }

    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        // TODO Auto-generated method stub

        this.context = arg0;
    }
}

My Circle Class就像这样

package org.koushik.javabrrians;

public class Circle {

    private Point centre;

    public Point getCentre() {
        return centre;
    }

    public void setCentre(Point centre) {
        this.centre = centre;
    }

    public void draw() {

        System.out.println("PoinA  Centre  = " + getCentre().getX() + " Y = " + getCentre().getY());


    }

}

我的点类是这样的

package org.koushik.javabrrians;

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 version="1.0" encoding="UTF-8" standalone="no"?>
<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.koushik.javabrrians.Triangle" autowire = "byName" scope = "singleton">
    </bean>

    <bean id="pointA" class="org.koushik.javabrrians.Point" scope = "prototype">
        <property name="x" value="0" />
        <property name="y" value="0" />
    </bean>

    <bean id="pointB" class="org.koushik.javabrrians.Point" scope = "prototype">
        <property name="x" value="-20" />
        <property name="y" value="0" />
    </bean>

    <bean id="pointC" class="org.koushik.javabrrians.Point" scope = "prototype">
        <property name="x" value="0" />
        <property name="y" value="20" />
    </bean>

    <bean id="circle" class="org.koushik.javabrrians.Circle"  scope = "prototype">
        <property name="centre" ref="pointA" />
    </bean>

</beans>

执行后我遇到以下错误

Sep 1, 2014 12:36:03 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@100ab23: startup date [Mon Sep 01 00:36:03 IST 2014]; root of context hierarchy
Sep 1, 2014 12:36:03 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Sep 1, 2014 12:36:03 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5dcec6: defining beans [triangle,pointA,pointB,pointC,circle]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pointA' defined in class path resource [spring.xml]: 1 constructor arguments specified but no matching constructor found in bean 'pointA' (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:175)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:993)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:897)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1087)
    at org.koushik.javabrrians.Triangle.getPointA(Triangle.java:18)
    at org.koushik.javabrrians.Triangle.draw(Triangle.java:46)
    at org.koushik.javabrrians.DrawingApp.main(DrawingApp.java:26)

1 个答案:

答案 0 :(得分:0)

错误的直接原因是“Point.class”周围的引号。删除它们(然后你也可以删除演员表):

public Point getPointA() {
    //return pointA;
    return context.getBean("pointA" , Point.class);
}

(和其他方法类似)。您的版本被视为对varargs方法getBean(String name, Object... args)的调用,其中args是构造函数参数,而您的Point类没有构造函数接受String参数。如果没有引号,则调用非varargs方法getBean(String name, Class<T> type),其中第二个参数是您要检索的bean的类型。