简体   繁体   中英

Spring Maven - Failed to load ApplicationContext NoSuchBeanDefinitionException: No qualifying bean of type

I encounter a exception in test phase when try to build my project.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [java.lang.String] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true),
@org.springframework.beans.factory.annotation.Qualifier(value=findUserByLogin)}

Project consist with 4 modules:

AdviceExchange
    ├── domain
    ├── dao
    ├── service
    ├── web

DAO classes (here occur throws exception)

package com.dozortsev.adviceexchange.dao;
import com.dozortsev.adviceexchange.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import static java.lang.String.format;
import static org.springframework.transaction.annotation.Propagation.MANDATORY;

@Transactional(propagation = MANDATORY)
@Repository
public class UserDaoImpl extends GenericDaoImpl<Long, User> implements UserDao {

    @Autowired
    private String findUserByLogin;   // Spring can't Autowired this String

    public UserDaoImpl() {
        this.entityClass = User.class;
    }

    @Override public User findByLogin(String login) {
        try {
            log.info(format("Finding %s by Login: %s", getEntityClass(), login));
            User user = (User) getCurrentSession().createSQLQuery(findUserByLogin)
                        .addEntity(getEntityClass()).setString("login", login)
                        .uniqueResult();

            if (user != null) {
                log.info("Successful found");
                return user;
            }
            log.info(format("%s not exist", getEntityClass()));
        } catch (Exception e) {
            log.error("Error: ", e);
        }
        return null;
    }
}

What wrong with my ApplicationContext ? Please help me with solve this issue.

If you use @Qualifier then use

bean name="findUserByLogin"

instead of

bean id="findUserByLogin"

I don't see an import of the dao-application-context.xml in the test application context. Since for testing purposes, you are using a separate data source, you should add 1 more application context file with the shared beans needed in both the real dao-application-context and the test-application context. That is, create a new context file shared-application-context.xml with the following:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"...>
    <!-- Queries -->
    <bean id="findUserByLogin" class="java.lang.String">
       <constructor-arg index="0"
                     value="
                     SELECT u.* FROM
                        user u, badge b, user_badge ub
                     WHERE
                        u.user_id = ub.ub_user_id
                        AND
                        b.bdg_id = ub.ub_badge_id
                        AND
                        u.user_email = :login"/>
    </bean>
</beans>

Then import that into both the dao-application-context.xml and the test application context file using:

<import resource="classpath:/META-INF/spring/shared-application-context.xml"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM