gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:55:05 26 4
gpt4 key购买 nike

我是 Spring 新手,但在搜索了一个多小时后我还没有找到答案。我正在制作一副简单的纸牌,但无法使用两个枚举作为构造函数中的参数来实例化我的 Card 对象。 Spring 可以使用枚举吗?我可以不在配置类中进行硬连线吗?这是我的代码:

@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());

}

}

我收到错误:

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: {}

最佳答案

由于您在 Card 类上注释了 @Component,spring 尝试在应用程序上下文期间创建 bean,并且由于您声明了参数构造函数,因此将没有可用的默认构造函数

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

}

因此,从 Card 类中删除 @Component ,并且对于 EnumEnumValue 也存在同样的错误,或者您可以添加默认构造函数或删除 @Component

EnumValue(){
}

关于Java Spring 无法创建使用枚举作为构造函数参数的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58443457/

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