为什么@Inject在春季4不起作用?

时间:2015-10-14 12:45:17

标签: spring dependency-injection dao autowired inject

我在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为空。
任何人都可以解释原因吗?

0 个答案:

没有答案