P
P
parkito2016-09-25 02:05:35
Java
parkito, 2016-09-25 02:05:35

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");
    }

It is necessary that when accessing it, private UserDAO userDAO is raised; etc.
I do as it is written in the tutorial.
@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");
    }

But right there, when deploying, I catch errors
[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}

What am I doing wrong?

UPD

@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

1 answer(s)
A
Artem Kileev, 2016-09-25
@akileev

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();
     }
}

or
<bean id="userDAO" class="com.example.dao.UserDAO">
    <!-- конфигурация bean UserDAO -->
</bean>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question