You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provider<...> declaration for @Value method argument fails with TypeMismatchException [SPR-12297] #16903
in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug
Would be very useful if Spring allowed for the injection of javax.inject.Provider along with @Value annotation for properties that could change at Runtime.
public class Foo {
private final Provider<Integer> changeableIntegerProperty;
public Foo(Provider<Integer> changeableIntegerProperty) {
this.changeableIntegerProperty = changeableIntegerProperty;
}
public void someOperation() {
changeableIntegerProperty.get(); // Gets current value of the property
}
}
@Configuration
public class FooConfig {
@Bean
public Foo foo(@Value("${fooProp}") Provider<Integer> fooProp) {
return new Foo(fooProp);
}
}
If the same code shown above were attempted now, a TypeMismatchException gets thrown in TypeConverterSupport due to the nesting level of the parameter not set to 1.
My request for improvement is to allow the injection of java.inject.Provider along with @Value.
Thanks.
As of #10747, this should work already, so I'll consider this as a bug. I suppose it just fails when type conversion is necessary... Anyway, if it's easy enough to fix, this is a candidate for a backport to 4.0.8 and 3.2.12 as well.
Thanks Juergen. This appears broken in 4.1.1 as well. My dirty workaround was to trick the TypeConverter with a custom one with the nesting level:
public class CustomTypeConverter implements TypeConverter {
private SimpleTypeConverter simpleTypeConverter;
public CustomTypeConverter() {
simpleTypeConverter = new SimpleTypeConverter();
}
public <T> T convertIfNecessary(Object newValue, Class<T> requiredType,
MethodParameter methodParam) throws IllegalArgumentException {
Type type = methodParam.getGenericParameterType();
MethodParameter parameterTarget = null;
if (type instanceof ParameterizedType) {
ParameterizedType paramType = ParameterizedType.class.cast(type);
Type rawType = paramType.getRawType();
if (rawType.equals(Provider.class) && methodParam.hasParameterAnnotation(Value.class)) { // Do this only if @Value and @Provider exist on the MethodParam
parameterTarget = new MethodParameter(methodParam); // Don't fiddle with original, send a new MethodParameter downstream
parameterTarget.decreaseNestingLevel();
}
}
return simpleTypeConverter.convertIfNecessary(newValue, requiredType, parameterTarget);
}
Setting the above as the TypeConverter default seems to get the code to work. I don't think this is the cleanest way to handle the same.
This seems to be a 4.x regression introduced through #14133, only failing for Provider-declared @Value method arguments but not for fields. And as it always is, we only had tests for such scenarios with fields...
in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug
Activity
spring-projects-issues commentedon Oct 5, 2014
Juergen Hoeller commented
As of #10747, this should work already, so I'll consider this as a bug. I suppose it just fails when type conversion is necessary... Anyway, if it's easy enough to fix, this is a candidate for a backport to 4.0.8 and 3.2.12 as well.
Juergen
spring-projects-issues commentedon Oct 5, 2014
Sanjay Acharya commented
Thanks Juergen. This appears broken in 4.1.1 as well. My dirty workaround was to trick the TypeConverter with a custom one with the nesting level:
Setting the above as the TypeConverter default seems to get the code to work. I don't think this is the cleanest way to handle the same.
spring-projects-issues commentedon Oct 5, 2014
Sanjay Acharya commented
I am surprised if the String version even worked, my simple test using 4.1.1.RELEASE, seems to fail with String as the Provider type:
Where app.properties is simply:
If you set the TypeConverter to the one I mentioned via the below, the test passes:
spring-projects-issues commentedon Oct 6, 2014
Juergen Hoeller commented
This seems to be a 4.x regression introduced through #14133, only failing for
Provider-declared@Valuemethod arguments but not for fields. And as it always is, we only had tests for such scenarios with fields...Juergen
spring-projects-issues commentedon Oct 6, 2014
Juergen Hoeller commented
Fixing that nestingLevel assertion makes it work, so it is indeed an accidental side effect from #14133. Fixed for 4.1.2; to be backported to 4.0.8.
For the time being, it'll work fine for fields, just not for method arguments...
Juergen
spring-projects-issues commentedon Oct 6, 2014
Sanjay Acharya commented
Great. Thanks much for the rapid turn around. We will look toward the new release.
spring-projects-issues commentedon Oct 6, 2014
Juergen Hoeller commented
Actually, this fails in 3.2.11 too, just with a fix necessary in a different place. Backported to 3.2.12 as well now.
Juergen