学习Srping注解配置时,会遇到一些配置问题,未能完全熟悉之前,先做个笔记记录下,后续翻看:
如果需要在实例变量中注解对象,如果该对象为一个类,则直接注解即可,例如:
public class UserDaoImpl{...} //并未实现接口,普通功能DAO类
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserDaoImpl userDaoImpl;
}
如果注解对象为实现类,则注解时需要定义接口,而不是实现类,例如:
public class UserDaoImpl implements UserDao{...} //实现接口
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDao;
}
如果注入对象为实现类,但是不是定义接口的话,则会报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource fields failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDaoImpl' must be of type [com.stephen.spring.n_jdbc.UserDaoImpl], but was actually of type [com.sun.proxy.$Proxy7]
未能深明其中含义,回头再看。