Answer the question
In order to leave comments, you need to log in
How to organize DI in Spring?
Hello. Help, please, the current problem.
There is a class
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
@Override
public void createEntity(User user) throws CustomDAOException {
if (!isUserExists(user)) {
userDAO.create(user);
} else System.out.println("User already exists");
}
@Service("userService") //Можно и @Component
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
@Transactional
public void createEntity(User user) throws CustomDAOException {
if (!isUserExists(user)) {
userDAO.create(user);
} else System.out.println("User already exists");
}
[2016-09-25 01:48:32,906] Artifact newBD:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.undertow.deployment.default-server.default-host./" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [operator.services.api.UserService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [operator.services.api.UserService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [operator.services.api.UserService]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"},"WFLYCTL0412: Required services that are not installed:" => ["jboss.undertow.deployment.default-server.default-host./"],"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined}
@Repository("userDAO")
public class UserDAOImpl extends GenericDAOImpl<User, Integer> implements UserDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
public User getUserByNumber(String number) throws UserNotFoundException {
try {
Query query = entityManager.createQuery("select c.user from Contract c where c.number=:number")
.setParameter("number", number);
return (User) query.getSingleResult();
} catch (PersistenceException e) {
throw new UserNotFoundException("User " + number + " wasn't found", e);
}
}
Answer the question
In order to leave comments, you need to log in
You don't have a UserDAO bean. Because of this, the container does not know where to get it from.
Add the @Component annotation to the UserDAO class. Or explicitly write in the configuration:
@Configuration
public class DAOConfig {
@Bean
public UserDAO userDAO() {
return new UserDAO();
}
}
<bean id="userDAO" class="com.example.dao.UserDAO">
<!-- конфигурация bean UserDAO -->
</bean>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question