Answer the question
In order to leave comments, you need to log in
Why doesn't @Autowired work?
Good day everyone! I've been having this problem for a while now and can't seem to solve it. Apparently @Autowired doesn't want to work, however, I can't even imagine why any more, any help would be greatly appreciated:
UserDaoImpl
import com.springapp.mvc.Entities.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Iterator;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {
/* UserEntity userEntity;*/
@PersistenceContext
private EntityManager entityManager;
public UserEntity findById(int id) {
return entityManager.find(UserEntity.class, id);
}
public UserEntity findBySSO(String sso) {
System.out.println("And here!");
/*
Query query = entityManager.createQuery("from UserEntity u where u.sso_id=:sso");
query.setParameter("sso", sso);
List<UserEntity> list = query.getResultList();
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println("And depeeeee!");
System.out.println(iterator.next());
userEntity=(UserEntity)iterator.next();*/
return null ;
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
import com.springapp.mvc.DAO.UserDaoImpl;
import com.springapp.mvc.Entities.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDaoImpl userDaoImpl;
@Transactional(propagation = Propagation.REQUIRED)
public UserEntity findById(int id) {
return userDaoImpl.findById(id);
}
@Transactional(propagation = Propagation.REQUIRED)
public UserEntity findBySso(String sso) {
return userDaoImpl.findBySSO(sso);
}
public UserDaoImpl getDao() {
return userDaoImpl;
}
public void setDao(UserDaoImpl dao) {
this.userDaoImpl = dao;
}
}
package com.springapp.mvc.Config.CustomSecure;
import java.util.ArrayList;
import java.util.List;
import com.springapp.mvc.Entities.UserEntity;
import com.springapp.mvc.Entities.UserRoles;
import com.springapp.mvc.Service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private UserServiceImpl service;
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserEntity user = service.findBySso(username);
if (user == null)
throw new UsernameNotFoundException("user name not found");
return buildUserFromUserEntity(user);
}
private User buildUserFromUserEntity(UserEntity user) {
String username = user.getSsoId();
String password = user.getPassword();
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
UserRoles role=user.getRole();
List<UserRoles> roles = new ArrayList<UserRoles>();
roles.add(role);
User springUser = new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked,roles );
return springUser;
}
public UserServiceImpl getService() {
return service;
}
public void setService(UserServiceImpl service) {
this.service = service;
}
}
28-Feb-2016 13:18:59.089 WARNING [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springapp.mvc.Service.UserServiceImpl com.springapp.mvc.Config.CustomSecure.UserDetailServiceImpl.service; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springapp.mvc.DAO.UserDaoImpl com.springapp.mvc.Service.UserServiceImpl.userDaoImpl; nested exception is java.lang.IllegalArgumentException: Can not set com.springapp.mvc.DAO.UserDaoImpl field com.springapp.mvc.Service.UserServiceImpl.userDaoImpl to com.sun.proxy.$Proxy37
28-Feb-2016 13:18:59.093 INFO [RMI TCP Connection(2)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.destroy Closing JPA EntityManagerFactory for persistence unit 'default'
28-Feb-2016 13:18:59.096 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springapp.mvc.Service.UserServiceImpl com.springapp.mvc.Config.CustomSecure.UserDetailServiceImpl.service; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springapp.mvc.DAO.UserDaoImpl com.springapp.mvc.Service.UserServiceImpl.userDaoImpl; nested exception is java.lang.IllegalArgumentException: Can not set com.springapp.mvc.DAO.UserDaoImpl field com.springapp.mvc.Service.UserServiceImpl.userDaoImpl to com.sun.proxy.$Proxy37
Answer the question
In order to leave comments, you need to log in
@Autowired
private UserService service;
after all, the beauty of DI is that you do not specify which specific implementation to associate with ..
And there it is clearly said at the end of this endless footcloth:Can not set com.springapp.mvc.DAO.UserDaoImpl field com.springapp.mvc.Service.UserServiceImpl.userDaoImpl to com.sun.proxy.$Proxy37
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question