我在Spring中遇到自动装配问题。
我有这样的AccessLevel对象的dao类:
接口:
public interface AccessLevelDAO {
public AccessLevel select(Integer id);
}
它的实施:
@Repository
public class AccessLevelDAOImpl implements AccessLevelDAO {
private SessionFactory sessionFactory;
@Autowired
public void AccessLevelDAO(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session currentSession() {
return sessionFactory.getCurrentSession();
}
@Override
@Transactional(propagation = Propagation.SUPPORTS,
isolation = Isolation.READ_UNCOMMITTED)
public AccessLevel select(Integer id) {
return (AccessLevel) currentSession().createCriteria(AccessLevel.class).add(Restrictions.idEq(id)).uniqueResult();
}
}
使用此DAO类的服务类:
接口:
public interface UserServices {
String getUserAccessLevel(String username);
AuthorizationResponseDTO authorize(String username, String password);
}
并实施:
@Service
public class UserServicesImpl implements UserServices {
@Autowired private AccessLevelDAO accessLevelDAO;
@Autowired private UserDAO userDAO;
@Override
public String getUserAccessLevel(String username) {
User user = userDAO.select(username);
return accessLevelDAO.select(user.getAccessLevel()).getAccessLevel();
}
@Override
public AuthorizationResponseDTO authorize(String username, String password) {
return null;
}
}
当我尝试@Autowire accessLevelDAO和userDAO时,我收到错误消息“无法自动装配。未找到”AccessLevelDAO“类型的bean”。
Spring配置包括组件扫描定义:
<context:annotation-config />
<context:component-scan base-package="com.core"/>