0
votes

I am getting an exception: No qualifying bean found for dependency [java.lang.String]: expected at least 1 bean which qualifies as autowire candidate.

In beans.xml

<bean id = "database" class = "com.price.compare.service.DAO" scope="singleton">
    <constructor-arg index="0" type = "java.lang.String" value="localhost"/>
    <constructor-arg index="1" type = "java.lang.String" value="5432"/>
</bean>

DAO.java


    @Component
    public class DAO {
        private final String host;
        private final String port;
        public DAO(String host, String port) {
            this.host = host;
            this.port = port;
        }
        @PostConstruct
        public void init() {
            // custom initialization logic
        }
    }

1

1 Answers

3
votes

You have defined the bean twice; in the XML config and as a @Component bean.

By using the @Component annotation the bean is picked up during component scanning. However, as the required string arguments are not clear during component scanning, the exception is thrown.

Remove the @Component annotation from your bean to let your XML config bean be the only DAO bean.