麻烦在Spring中传递constructor-arg

时间:2015-10-14 18:30:09

标签: java spring

我在使用Spring将构造函数arg传递给我的Project时遇到了一些麻烦。我确信这是微不足道的,但我仍然在努力学习Spring。我的代码在这里:

package com.WheelOfFortune.Client;

import java.util.ArrayList;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;



@Controller
public class Spinner implements SpinnerInterface{

    private int balance;
    private String vacation;
    private String car;
    private ArrayList<String> spinWheel;

    //Methods can access same instance of factory
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
            "src/main/resources/spring/SpinWheel.xml"));

    public Spinner(){}

    @Autowired
    public Spinner(int balance, String vacation, String car, ArrayList<String> spinWheel)
    {
        this.balance = balance;
        this.vacation = vacation;
        this.car = car;
        this.spinWheel = spinWheel;
    }

    //Setters and Getters
    public void setSpinWheel()
    {
        this.spinWheel = spinWheel;
    }
    public ArrayList<String> getSpinWheel(ArrayList<String> getSpinWheel)
    {
        return spinWheel;
    }
    public void setBalance()
    {
        this.balance = balance;
    }
    public void setVacation()
    {
        this.vacation = vacation;
    }
    public void setCar()
    {
        this.car = car;
    }
    public int getBalance(int balance)
    {
        return balance;
    }
    public String getVacation(String vacation)
    {
        return vacation;
    }
    public String getCar(String car)
    {
        return car;
    }

    public void spin() {
        Spinner spin = (Spinner) factory.getBean("spinWheel");

    }

    public void addToBank(boolean isCorrect) {
        // TODO Auto-generated method stub

    }

    public void deductFromBank() {
        // TODO Auto-generated method stub

    }

    public void vacation(boolean isCorrect) {
        Spinner vacation = (Spinner) factory.getBean("vacation");
        System.out.println(vacation);
    }
    public void car(boolean isCorrect){
        Spinner car = (Spinner) factory.getBean("car");
    }   
}

我的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-2.5.xsd">

    <bean id="balance" class="com.WheelOfFortune.Client.Spinner">
        <constructor-arg>
            <value>0</value>
        </constructor-arg>
    </bean>

    <bean id="vacation" class="com.WheelOfFortune.Client.Spinner">
        <constructor-arg value="You Won a Trip to Hawaii!!" />
    </bean>

    <bean id="car" class="com.WheelOfFortune.Client.Spinner">
        <constructor-arg value="You Won a brand new Corvette!!" />
    </bean>

    <bean id="spinWheel" class="com.WheelOfFortune.Client.Spinner">
        <property name="word">
            <list>
                <value>$100</value>
                <value>$150</value>
                <value>$275</value>
                <value>$400</value>
                <value>$500</value>
                <value>$750</value>
                <value>$1000</value>
                <value>Bankruptcy</value>
                <value>$5000</value>
                <value>Vacation</value>
                <value>Car</value>
            </list>
        </property>
    </bean>
</beans>

每当我尝试运行项目时,我总是得到一个堆栈跟踪:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vacation' defined in file [/home/ryan/workspace/wheel/src/main/resources/spring/SpinWheel.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
    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$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at com.WheelOfFortune.Client.Spinner.vacation(Spinner.java:74)
    at com.WheelOfFortune.Client.Client.main(Client.java:10)

我在XML文件上上下下,找不到任何可能产生这种堆栈跟踪的原因。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您的配置中有多个错误的内容。

首先,关于您的constructor-arg问题,您在XML中使用了一个arg构造函数:

<bean id="balance" class="com.WheelOfFortune.Client.Spinner">
    <constructor-arg>
        <value>0</value>
    </constructor-arg>
</bean>

但是你的Java只用4个args定义了这个构造函数:

public Spinner(int balance, String vacation, String car, ArrayList<String> spinWheel);

你能写的是:

<bean id="spinner" class="com.WheelOfFortune.Client.Spinner">
    <constructor-arg>
        <value>0</value>
    </constructor-arg>
    <constructor-arg ref="vacation">
    ...
</bean>

但在您需要更正vacation bean之前(同样适用于carspinWheel)。您输入的内容为com.WheelOfFortune.Client.Spinner,但它是String

最终,您正在混合注释和XML配置。这不是问题,但在启动Spring时可能会让人感到困惑,我建议用XML或全部注释来完成。

如果您需要更多详细信息,请随时提问;)

编辑:重新阅读您的帖子,我实际上了解您想要写的内容,如果您激活组件扫描,只需更正您的bean类型即可。