1

I use jpa, eclispelink and spring 3. I have UserDAO interface:

  public interface UserDAO {
    public void saveUser(User user);
  }

and implementing class :

@Service
@Transactional
public class UserDAOImpl implements UserDAO{

@PersistenceContext
EntityManager em;

@Override
public void saveUser(User user) {
    em.persist(user);
}


When I start app I have got error:

HTTP Status 500 - Servlet.init() for servlet appServlet threw exception<br>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cartController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: foo.endpoints.UserEndpoint foo.controller.CartController.userEndpoint; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userEndpoint': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: foo.dao.UserDAOImpl foo.endpoints.UserEndpoint.userDAO; nested exception is java.lang.IllegalArgumentException: Can not set foo.dao.UserDAOImpl field foo.endpoints.UserEndpoint.userDAO to com.sun.proxy.$Proxy16

org.springframework.beans.factory.BeanCreationException: Could not autowire field: foo.endpoints.UserEndpoint foo.controller.CartController.userEndpoint; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userEndpoint': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: foo.dao.UserDAOImpl foo.endpoints.UserEndpoint.userDAO; nested exception is java.lang.IllegalArgumentException: Can not set foo.dao.UserDAOImpl field foo.endpoints.UserEndpoint.userDAO to com.sun.proxy.$Proxy16


but if I don't implement interface:

 @Service
 @Transactional
 public class UserDAOImpl {
 @PersistenceContext
 EntityManager em;
  public void saveUser(User user) {
  em.persist(user);
  }


everything work ok. I don't understand it. Maybe it is something with @Override method? Thanks

Niranjan
  • 1,776
  • 1
  • 13
  • 21
user978758
  • 609
  • 3
  • 17
  • 29
  • 3
    I'm sure that there are some exact duplicates of the question http://stackoverflow.com/a/8224772/241986, basically the default strategy of Spring is to use JDK proxy if class implements at least one interface and CGLIB proxy otherwise. When JDK proxy is used you won't be able to inject the class, only interface will be available. – Boris Treukhov Jul 21 '13 at 13:14
  • 1
    BTW, I think the convention is to annotate DAO classes with `@Repository` not `@Service` – Mina Jul 22 '13 at 08:33
  • @BorisTreukhov exactly the problem!!I had been searching for these weird problem for so long – Allan Ruin Mar 10 '14 at 15:56

1 Answers1

4

If you define an interface for your bean, then you must inject an instance of the interface, and not an instance of the concrete class:

@Autowired
private UserDAO userDAO;

and not

@Autowired
private UserDAOImpl userDAOImpl;

Because the actual bean instance is a JDK dynamic proxy which implements the interface and calls your implementation. It's not an instance of UserDAOImpl.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255