Spring bean with @Autowire in superclass

Sid :

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?

liangzhi :

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");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Autowire Spring Bean into interface for default method

How can I @Autowire a spring bean that was created from an external jar?

Autowire a bean within Spring's Java configuration

How to Autowire Bean of generic type <T> in Spring?

How do I manually autowire a bean with Spring?

In Spring, can I autowire new beans from inside an autowired bean?

Spring autowire a stubbed service - duplicate bean

Spring Boot can't autowire map bean in @configuration class

Autowire a Spring bean in a Singleton class

When a SubClass which is a spring bean calls method of SuperClass which is also a spring bean, the autowired field in SuperClass is null

How to autowire a bean in other class in a Spring Boot application?

How to Autowire a OkHttpClient bean in Spring Boot?

Spring Boot autowire field in singleton service/controller by request scoped bean

How to autowire a spring bean based on some session info

Unable to autowire spring bean in log appender

How to define which bean to autowire in Spring

Spring: Autowire bean that does not have qualifier

Spring autowire only if bean is present as method argument

how to autowire bean in the servlet filters in spring application?

Spring factory bean with @Autowired in superclass

Spring: autowire bean with same type as existing class

Not able to autowire a bean using constructor in Spring

Spring @Autowire is behaving in a weird way returning null for initialized bean

How to autowire one bean to another bean in spring mvc

Spring Bean and Autowire to datasource

@Autowire Spring Bean with Injected Constructor args?

Autowire a bean from spring config - Environment specific

Autowire Enum in Spring Bean

Spring @Autowire fails with No qualifying bean of type found for dependency error