简体   繁体   中英

Spring bean with @Autowire in superclass

I have a subclass like below:-

@Component
public class Subclass extends Superclass {

    //few inherited methods implementation

}

Superclass is like below:-
@Component
public class Superclass implements InterfaceA {
     @Autowired
     @Qualifier("envBean")
     private EnvironmentBean envBean;
     private DateTime effective_date = envBean.getProperty("effective.date");
}

Now while deploying the application, I am getting below errors

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name "Subclass"

Constructor threw exception; nested exception is java.lang.NullPointerException
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [Subclass]:Constructor threw exception; nested exception is java.lang.NullPointerException.

and finally I saw -

Caused by: java.lang.NullPointerException: null
at Superclass <init> (SuperClass.java:{lineNumber} 

which is at the below line :-

**envBean.getProperty("effective.date");**

I have tried using constructor injection of EnvironmentBean property from the subclass itself Tried configuring it in xml and to instantiate Superclass bean with constructor injection. Does someone have any idea how to resolve it?

Maybe you can try interface -> InitializingBean , and override method 'afterPropertiesSet', then you can assign effective_date value. just like:

@Override
public void afterPropertiesSet() {
    effective_date = envBean.getProperty("effective.date");
}

It seems that this is because Spring has to first create an instance of the class Superclass and only then inject EnvironmentBean . That is, when the class Superclass is instantiated, Java will try to instantiate the field DateTime effective_date even before Spring tries to inject the dependency @Autowired @Qualifier("envBean") private EnvironmentBean envBean; . At this time, envBean refers to null . Hence, this will surely throw an NPE. (My view.)

So, not sure if this is really related to the class hierarchy itself.

There must be a class called EnvironmentBean It must be annotated with any one of Spring stereotype shown in doc https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/stereotype/package-summary.html

Component - Indicates that an annotated class is a "component".

Controller - Indicates that an annotated class is a "Controller"

Indexed - Indicate that the annotated element represents a stereotype for the index.

Repository - Indicates that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects".

Service - Indicates that an annotated class is a "Service", originally defined by Domain-Driven Design (Evans, 2003) as "an operation offered as an interface that stands alone in the model, with no encapsulated state."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM