0

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
ovod
  • 1,118
  • 4
  • 18
  • 33
  • 2
    How can `propertyA` be null when it's declared as a primitive? Seems odd! – Abubakkar Oct 12 '15 at 14:48
  • 1
    it should work fine .. you have some other mistakes i guess – stinepike Oct 12 '15 at 14:49
  • Post some real code so we can verify your problem. – khelwood Oct 12 '15 at 14:50
  • post whole code please – Rahman Oct 12 '15 at 14:50
  • If I do A inst3 = new A() - it is not null. It is simplified code. Classes have more methods. I just skipped them. – ovod Oct 12 '15 at 14:50
  • 2
    B will not have access to a protected member of A if it is not in the same package. – HulkSmash Oct 12 '15 at 14:52
  • post the full stack trace / Exception – clD Oct 12 '15 at 14:52
  • Results of my testing: public class A { protected int propertyA = 1; } public class B extends A{ public int propertyB = propertyA + 1; public static void main(String[] args) { A inst1 = new B(); B inst2 = new B(); System.out.println(inst1.propertyA); // prints 1 System.out.println(inst2.propertyB); // prints 2 } } How do you have null? – awsome Oct 12 '15 at 14:54
  • I thought I do smth wrong. Ok. I will edit it. – ovod Oct 12 '15 at 14:54
  • 1
    The object returned by `new B()` has no relation to Spring. There's no way for Spring to inject anything into it. Why would you expect any `@Autowired` annotated fields to be non-`null`? – Sotirios Delimanolis Oct 12 '15 at 17:28
  • ***1.*** Spring will not magically inject the property for you when you are `new`ing your object instance manually. Your instance of `B` needs to be created/managed by Spring ***2.*** I have no clue on your meaning of "*class B dont have access to property A*". I cannot see any "property A". Be precise and clear when you ask – Adrian Shum Oct 13 '15 at 01:43

1 Answers1

0

If you are getting NPE, is because sessionFactory is not being injected, instead that you haven't access to sessionFactory property.

If you can get an instance of sessionFactory after loading your application context, then this bean is right configured.

So I believe your problem has to be related with autowired annotation config. Do you have <context:annotation-config /> into your application context? or have you got a bean of class org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor defined into your application context? - Has your beans xml node

malaguna
  • 4,183
  • 1
  • 17
  • 33