我在Spring 4.1.7上使用hibernate sesstionFactory进行配置。当我使用@Autowire
时效果很好:
@Repository
public class UserDao implements InitializingBean {
@Autowired
protected SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(sessionFactory);
}
public void save(User user) {
}
public void persist(User user) {
}
public User findByUsername(String username) {
List<User> users = sessionFactory.getCurrentSession().createQuery("from User where username=:username").setParameter("username", username).list();
if (users.isEmpty()) {
return null;
} else {
return users.get(0);
}
}
}
但当我将其更改为@Inject
时,它无效并触发IllegalArgumentException assertion failed
sessionFactory
为空。
任何人都可以解释原因吗?