为什么 Spring 找不到默认构造函数?
Why can't Spring find a default constructor when it is right there?
我一直收到错误消息:
Error creating bean with name 'category' defined in file [/home/dazikiri_anikar/IdeaProjects/shop/target/classes/pl/shop/models/Category.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [pl.shop.models.Category]: No default constructor found; nested exception is java.lang.NoSuchMethodException: pl.shop.models.Category.<init>()
这里是 class,Spring 有问题:
package pl.shop.models;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Component
public enum Category {
HEALTHY_FOOD,
JUNK_FOOD,
TEAS_AND_COFFEES,
SPICES,
GRAINS_AND_LENTILS,
NUTS_AND_SEEDS,
DRIED_FRUITS,
SNACKS,
DRINKS;
private String categoryName;
List<Product> productList = new ArrayList<>();
}
我找到的所有答案都是...定义默认构造函数。哪个...就在那里,
来自龙目岛的@NoArgsConstructor,对吧?
即使我删除它并自己定义它,它也不会改变任何事情。
我尝试自己定义两个构造函数(无参数和所有参数)无济于事,然后添加
@Autowired 导致没有 'String' bean 的错误,这让我在这个阶段完全无能为力。
拜托,帮忙。
在Java中,Enum只有私有构造函数。因此不能被 Spring
实例化
我一直收到错误消息:
Error creating bean with name 'category' defined in file [/home/dazikiri_anikar/IdeaProjects/shop/target/classes/pl/shop/models/Category.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [pl.shop.models.Category]: No default constructor found; nested exception is java.lang.NoSuchMethodException: pl.shop.models.Category.<init>()
这里是 class,Spring 有问题:
package pl.shop.models;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Component
public enum Category {
HEALTHY_FOOD,
JUNK_FOOD,
TEAS_AND_COFFEES,
SPICES,
GRAINS_AND_LENTILS,
NUTS_AND_SEEDS,
DRIED_FRUITS,
SNACKS,
DRINKS;
private String categoryName;
List<Product> productList = new ArrayList<>();
}
我找到的所有答案都是...定义默认构造函数。哪个...就在那里, 来自龙目岛的@NoArgsConstructor,对吧? 即使我删除它并自己定义它,它也不会改变任何事情。
我尝试自己定义两个构造函数(无参数和所有参数)无济于事,然后添加 @Autowired 导致没有 'String' bean 的错误,这让我在这个阶段完全无能为力。
拜托,帮忙。
在Java中,Enum只有私有构造函数。因此不能被 Spring
实例化