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.
@Componentannotation on a class that you want to inject into.@Componenttells 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.Enumshould 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.