问题描述:

I work on Spring 4.1.7 configuring with hibernate sesstionFactory . When I use @Autowire it works well:

 @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);

}

}

}

But when I change it to @Inject it does not work and fires IllegalArgumentException assertion failed the sessionFactory is null.

Can anyone explain why?

相关阅读:
Top