简体   繁体   English

Java Spring 无法使用枚举作为构造函数参数创建 Bean

[英]Java Spring Cannot Create Bean with Enums as Constructor Parameters

I am new to Spring but I haven't found an answer to this after searching for over an hour.我是 Spring 的新手,但在搜索了一个多小时后我还没有找到答案。 I am making a simple deck of cards and I can't get my Card object to instantiate with two Enums as parameters in the constructor.我正在制作一副简单的纸牌,但无法让我的 Card object 在构造函数中使用两个枚举作为参数进行实例化。 Are Enums possible with Spring? Spring 是否可以使用枚举? Can I not hardwire in the config class?我不能在配置 class 中进行硬连线吗? Here is my code:这是我的代码:

@Component
public class Card {

    EnumValue value;
    EnumSuit suit;

    public Card(EnumValue v, EnumSuit s) {
        value = v;
        suit = s;

    }

    public EnumValue getValue() {
        return value;
    }

    public void setValue(EnumValue value) {
        this.value = value;
    }

    public EnumSuit getSuit() {
        return suit;
    }

    public void setSuit(EnumSuit suit) {
        this.suit = suit;
    }

    @Override
    public String toString() {
        return String.format("[%s][%s]", value.getShortName(), suit.getShortName());
    }
}

@Component
public enum EnumValue {
    ACE("A",1), TWO("2", 2), THREE("3", 3), FOUR("4", 4), FIVE("5", 5), SIX("6", 6), SEVEN("7", 7), 
    EIGHT("8", 8), NINE("9", 9), TEN("T", 10), JACK("J", 10), QUEEN("Q", 10), KING("K", 10);

    private String shortName;
    private int points;


    EnumValue(String name, int score){
        shortName = name;
        points = score;
    }


    public String getShortName() {
        return shortName;
    }


    public void setShortName(String shortName) {
        this.shortName = shortName;
    }


    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }
}


@Component
public enum EnumSuit {
    CLUBS, DIAMONDS, HEARTS, SPADES;

    public String getShortName() {
        return this.toString().substring(0,1);
    }
}


@Configuration
@ComponentScan(basePackages = "com.betterstuff.learnspring")
public class AppConfig {

    @Bean
    public Card getSampleCard() {
        return new Card(EnumValue.ACE, EnumSuit.CLUBS);
    }
}

public class App {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        Card obj = (Card) context.getBean(Card.class);

        System.out.println("My Card is: " + obj.toString());

    }

}

I am getting the error:我收到错误消息:

WARNING: Exception encountered during context initialization - cancelling refresh attempt: UnsatisfiedDependencyException: Error creating bean with name 'card' defined in file [...\Card.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is NoSuchBeanDefinitionException: No qualifying bean of type 'com...EnumValue' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Exception in thread "main" UnsatisfiedDependencyException: Error creating bean with name 'card' defined in file [...\Card.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is NoSuchBeanDefinitionException: No qualifying bean of type 'com...EnumValue' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Since you annotated @Component on Card class spring is trying to create bean during application context, and since you declared argument constructor there will no default constructor available由于您在Card class spring 上注释@Component正在尝试在应用程序上下文期间创建 bean,并且由于您声明了参数构造函数,因此将没有可用的默认构造函数

public Card(EnumValue v, EnumSuit s) {
    value = v;
    suit = s;

}

So remove the @Component from Card class, and also same mistake for Enum class EnumValue , either you can add default constructor or remove the @Component因此,从Card class 中删除@Component ,以及Enum class EnumValue的相同错误,您可以添加默认构造函数或删除@Component

EnumValue(){
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM