Issue
I am using Spring 4.1.5-RELEASE and java 8 for my application. On looking at the class, there are 2 parametrizations to be considered, one at class level and other at class's instance variable. On dependency injection I am not having trouble for class level parametetrization ( as I have constructor with super()), while the instance variable serviceErrorResponseProcessor, causes following error. If serviceErrorResponseProcessor variable is removed from class and bean injection, I am not seeing the error. So something is wrong in defining the generic instance variable.
Class:
public class ServiceResponseProcessor implements ResponseProcessor<T, R> {
private ErrorResponseProcessor<Error> serviceErrorResponseProcessor;
private ServiceInfoResponseProcessor serviceInfoResponseProcessor;
// respective getters and setters
}
Bean injection:
<bean id="responseProcessor"
class="com.path.ServiceResponseProcessor">
<property name="serviceErrorResponseProcessor" ref="beanServiceErrorResponseProcessor" />
<property name="serviceInfoResponseProcessor" ref="beanServiceInfoResponseProcessor" />
</bean>
<bean id="beanServiceErrorResponseProcessor"
class="com.path.processor.ErrorResponseProcessor"/>
<bean id="beanServiceInfoResponseProcessor"
class="com.path.processor.ServiceInfoResponseProcessor"/>
Error:
Error creating bean with name 'responseProcessor' defined in class path resource [config/bean-dependency.xml]:
Initialization of bean failed; nested exception is java.lang.reflect.MalformedParameterizedTypeException
at com.path.ServiceResponseProcessor.testResponseProcessor(ServiceResponseProcessor.java:326)
at com.path.ServiceResponseProcessor.testSteps(ServiceResponseProcessor.java:276)
Caused by: java.lang.reflect.MalformedParameterizedTypeException
at com.path.ServiceResponseProcessor.testResponseProcessor(ServiceResponseProcessor.java:326)
at com.path.ServiceResponseProcessor.testSteps(ServiceResponseProcessor.java:276)
Solution
I have found something which worked for me using spring 4.1.5-RELEASE, that is one cannot use generics in instance variable when the class where it is used for bean injection.
Class:
public class ServiceResponseProcessor implements ResponseProcessor<T, R> {
private ServiceErrorResponseProcessor serviceErrorResponseProcessor;
private ServiceInfoResponseProcessor serviceInfoResponseProcessor;
// respective getters and setters
}
while,
ServiceErrorResponseProcessor implements ErrorResponseProcessor<Error>{
// implementation
}
Bean injection:
<bean id="responseProcessor"
class="com.path.ServiceResponseProcessor">
<property name="serviceErrorResponseProcessor" ref="beanServiceErrorResponseProcessor" />
<property name="serviceInfoResponseProcessor" ref="beanServiceInfoResponseProcessor" />
Answered By - yellow
Answer Checked By - Terry (JavaFixing Volunteer)