3

I am trying to use spring injection to inject into an enum. Like this:

public enum Car {
HONDA("Honda", "hondas") {
    @Inject
    Carparts carparts;

    @Override
    public List<Carpart> getCarparts() {
        return carparts.getCarpartsListings();
    }
};
//more logic here
}

My Carparts bean is defined as follows in the configuration class:

@Bean
@Singleton
public Carparts geCarparts() {
    return new Carparts();
}

But no matter what I get a null value for carparts injection. Any help will be greatly appreciated.

15
  • I'm not sure if this works at all but if that works, then you'll have to make this enum at least a component s.t. it gets picked up by spring. This is not particularly a qualified answer but you need at least something like a @Component annotation on a class that you want to inject into. Commented May 23, 2018 at 23:43
  • @StefanFalk isn't annotating a class with "Component" another way of making that class a bean? I did not know that a class needs to be annotated with "Component" even for injection. Commented May 23, 2018 at 23:47
  • 2
    @Component tells Spring that this class should be managed by Spring. See this answer for more details but the essence is that Spring won't inject anything if you don't tell Spring to take care about a class. Commented May 23, 2018 at 23:49
  • 2
    What do you want to achieve? Tell your ultimate goal and we might help by provide a workaround. You cannot inject into an enum because enum's static final, and the inject process is happens after enum loaded Commented May 23, 2018 at 23:58
  • 2
    Your Enum should not contain logic. If it contains logic, then your design is wrong. Refactoring the logic part out. You test your util class which has static methods is enough. Commented May 24, 2018 at 0:12

1 Answer 1

4

You need to create a workaround with an inner static class that will be a Spring component, inject it and set carparts for entire Car EnumSet.

public enum Car {
    private Carparts carparts;

    @Component
    public static class CarPartsInjector {
        @Inject
        private Carparts carparts;

        @PostConstruct
        public void construct() {
            for (Car car: EnumSet.allOf(Car.class))
               car.setCarparts(carparts);
        }

        private void setCarparts(Carparts carparts) {
            this.carparts = carparts;
        }
    //...

}

In general, I think this is not a proper solution.

The problem here is Data Model.

The Car shouldn't be an enum. It thould be a class with list of Carparts, enum as type like CarType or ManufacturerType.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is quite handy when you need to correlate multiple "models" with distinct implementation behaviours and need to reference a repository/service/implementation that shares a common interface and is critical. A suggestion is to use a switch under that cycle to ensure that specific cases and default cases are covered for distinct services/implementations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.