1
I have a controller that has this @Autowired:
@Autowired
Rules rules;
This Rules class is defined as follows::
@Service
public class Rules {
@Autowired
private List<RegistrationRule> allRules;
public List<RegistrationRule> getAllRules() {
return allRules;
}
}
I want when doing the @Autowired in the list allRules it already comes with some standard items within it, as follows:
allRules.add( new EmployeePositionRule() );
allRules.add( new CostCenterRule() );
I tried to put in the Rules class builder in the following way:
public Rules() {
allRules.add( new EmployeePositionRule() );
allRules.add( new CostCenterRule() );
}
But if I do so an exception is released at the time of compiling the project:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'intRaptMecController': Unsatisfied dependency expressed through field 'rules'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rules' defined in file [...validations\rules\Rules.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [validations.rules.Rules]: Constructor threw exception; nested exception is java.lang.NullPointerException
What’s the right way to do it?
UnsatisfiedDependencyException
means that Spring doesn’t know where a dependency comes from that needs to be injected. In his case, he doesn’t know how to inject aList<RegistrationRule>
in your classRules
.– Leonardo Lima