Could not instantiate bean : Constructor threw exception; nested exception is java.lang.NullPointerException

后端 未结 3 1421
自闭症患者
自闭症患者 2021-02-19 23:08
package baseDao;

public interface BaseDao {

    public void create(Object obj);
    public void delete(Object obj);
    public void update(Object obj);
    public void         


        
3条回答
  •  囚心锁ツ
    2021-02-20 00:12

    You are trying to initialize session object, when usernameSessionFactory object is not initialized yet.

    In a class, all Spring @Autowired objects are initialized just after the construction of the instance of such class is done.

    private  Session session = usermanagementSessionFactory.getCurrentSession();
    

    But if an instance decleared in a way like the above code, will be initialized earlier than construction, in a way that they can even be used as parameters of construction.

    @Autowired
    private  SessionFactory usermanagementSessionFactory;
    private  Session session;
    
    @PostConstruct
    public void init() {
        session = usermanagementSessionFactory.getCurrentSession();
    }
    

    Above code, will initialize session object after the construction, which means usermanagementSessionFactory is ready and not null.

    Hopefull it helps..

提交回复
热议问题