0

Hi I have created a factory, with the help of some guidance from @DruidKuma from the link : Implement a simple factory pattern with Spring 3 annotations

I created the factory as follows:

@Component
public class ValidatorFactory {
    @Autowired
    private List<Validator> validators;

    private static final Map<String, Validator> validatorMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void setValidatorMap() {
        for (Validator validator : validators) {
            validatorMap.putIfAbsent(validator.getCountry().name(), validator);
        }
    }

    public Validator getValidator(Value v) {
        return validatorMap.get(v.name());
    }
}

But when I am trying to start the application I am getting this error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field validators in com.abc.factory.ValidatorFactory required a bean of type 'java.util.List' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'java.util.List' in your configuration.

What I understood from the link which helped me was that spring should create the bean automatically. Can someone please help me with this?

N Borah
  • 1
  • 4

1 Answers1

0

As per your code:

@Autowired
private List<Validator> validators;

Spring tries to find beans of type Validator, if it does not find any bean then it throws the error required a bean of type 'java.util.List' that could not be found.

To fix the issue, make sure that you have beans of type Validator in classpath and they are being scanned by spring container, for example using @ComponentScan.

Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • The `Validator` bean is present and the classpath for the project is provided in the Application file. But I am still getting the error. – N Borah May 17 '20 at 10:31
  • @NBorah, can you add more details to your post, your bean details and spring configuration file etc. – Chaitanya May 17 '20 at 12:27
  • I had made a minor mistake, did not declare component for the Validator implementation. Its fixed and working as expected. – N Borah May 18 '20 at 06:36