0

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.