Bartosz_76
Bartosz_76

Reputation: 87

Why can't Spring find a default constructor when it is right there?

I keep getting the error:

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

Here is the class that Spring has a problem with:

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

All the answers I am finding are... to define a default constructor. Which... is right there, the @NoArgsConstructor from Lombok, right? And even if I remove this and define it myself, it doesn't change a thing.

I tried defining both constructors (no args and all args) myself to no avail and then added @Autowired which resulted in an error that there is not 'String' bean which makes me absolutely clueless at this stage.

Please, help.

Upvotes: 1

Views: 471

Answers (1)

asgarov1
asgarov1

Reputation: 4161

In Java, Enum only has private constructors. Hence can’t be instantiated by Spring

Upvotes: 4

Related Questions