EDIT: My question is not about why Spring bean is null, but about why inherited bean is null? Proposed answer explains why new operator does not work with autowiring. But my question is about inheritance. I have two classes:
@Transactional
public class A {
@Autowired
protected SessionFactory sessionFactory;
//other methods
}
public class B extends A {
Session session = sessionFactory.getCurrentSession();
//other methods
}
In Main.java I get NPE - class B dont have access to property A((( And I dont understand why(((
A inst1 = new B(); - dont have access to body of B.
B inst2 = new B(); - inside class B sessionFactory is null(((
What I do wrong? I need to have access to body of B, and to properties of superior class. Bean sessionFactory is working:
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
SessionFactory sessionfactory = (SessionFactory) context.getBean("sessionFactory");
Session session = sessionfactory.getCurrentSession();//works