ryekayo
ryekayo

Reputation: 2421

Trouble passing constructor-arg in Spring

I am experiencing some trouble passing a constructor-arg into my Project with Spring. I am sure this is something trivial but I am still trying to learn Spring. I have my code here:

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

And my xml file:

<?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>

Whenever I attempt to run the project, I always get this for a stacktrace:

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)

I have went up and down on the XML file and could not find any reason why it would produce this stacktrace. Any suggestions?

Upvotes: 2

Views: 1298

Answers (1)

Ga&#235;l J
Ga&#235;l J

Reputation: 15050

There are multiple wrong things in your configuration.

First, concerning your constructor-arg issue, you use a single arg constructor in the XML :

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

But your Java only define this constructor with 4 args :

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

What you could write is :

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

But before you need to correct your vacation bean (and same for car and spinWheel). You typed it as com.WheelOfFortune.Client.Spinner but it is a String.

Eventually, you are mixing annotations and XML configuration. This is not a problem but it might be confusing when starting Spring, I would recommend to either do it all in XML or all in annotations.

If you need more details, feel free to ask ;)


EDIT : re-reading your post, I actually understand what you wanted to write and just correcting the types of your beans might be sufficient if you activate component scanning.

Upvotes: 2

Related Questions