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 it when I do @Autowired in the allRules list it already comes with some standard items inside it, as follows:
allRules.add( new EmployeePositionRule() );
allRules.add( new CostCenterRule() );
I tried putting it in the constructor of the Rules class as follows:
public Rules() {
allRules.add( new EmployeePositionRule() );
allRules.add( new CostCenterRule() );
}
But if I make an exception, it is thrown 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 is the right way to do this?