Affects: \<5.3.22>
Under normal conditions,everything is ok:
But if I modify the getter like this:
autowire will fail:
And in this case it also fails:
But sometimes it works:
or
Comment From: yoa1226
@wangweng You can display you code repo and use branch distinguishing bad code or good.
Comment From: yoa1226
@wangweng
You can see the reason in this method named org.springframework.beans.BeanUtils#isSimpleValueType
.
public static boolean isSimpleProperty(Class<?> type) {
Assert.notNull(type, "'type' must not be null");
return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType()));
}
public static boolean isSimpleValueType(Class<?> type) {
return (Void.class != type && void.class != type &&
(ClassUtils.isPrimitiveOrWrapper(type) ||
Enum.class.isAssignableFrom(type) ||
CharSequence.class.isAssignableFrom(type) ||
Number.class.isAssignableFrom(type) ||
Date.class.isAssignableFrom(type) ||
Temporal.class.isAssignableFrom(type) ||
URI.class == type ||
URL.class == type ||
Locale.class == type ||
Class.class == type));
}
Comment From: wangweng
@wangweng You can see the reason in this method named
org.springframework.beans.BeanUtils#isSimpleValueType
.```java public static boolean isSimpleProperty(Class<?> type) { Assert.notNull(type, "'type' must not be null"); return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType())); }
public static boolean isSimpleValueType(Class<?> type) { return (Void.class != type && void.class != type && (ClassUtils.isPrimitiveOrWrapper(type) || Enum.class.isAssignableFrom(type) || CharSequence.class.isAssignableFrom(type) || Number.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || Temporal.class.isAssignableFrom(type) || URI.class == type || URL.class == type || Locale.class == type || Class.class == type)); } ```
Okay, I got it. In my intuition, I think it is only related to setters. But actually it use getters to check if the given type represents a "simple" property. That's what makes me confused. Thank you very much!
Comment From: wangweng
@wangweng You can see the reason in this method named
org.springframework.beans.BeanUtils#isSimpleValueType
.```java public static boolean isSimpleProperty(Class<?> type) { Assert.notNull(type, "'type' must not be null"); return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType())); }
public static boolean isSimpleValueType(Class<?> type) { return (Void.class != type && void.class != type && (ClassUtils.isPrimitiveOrWrapper(type) || Enum.class.isAssignableFrom(type) || CharSequence.class.isAssignableFrom(type) || Number.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || Temporal.class.isAssignableFrom(type) || URI.class == type || URL.class == type || Locale.class == type || Class.class == type)); } ```
老哥,非常感谢,我看到你ip是在上海,早知道我就用中文跟你沟通了,我英语比较烂😂,虽然知道原因了,但是我感觉这里还是有点怪怪的,不知道能不能只用setter来做判断,这样逻辑上也比较一致,自动装配通过setter注入就只与setter有关。因为有时候万一想控制get的逻辑就可能会出现这个错误。
Comment From: yoa1226
@wangweng just practice english. 😂
Comment From: leihuazhe
May be you can see this: - https://stackoverflow.com/questions/9724490/why-cant-spring-autowire-simple-types - https://stackoverflow.com/questions/52136756/you-cannot-autowire-so-called-simple-properties-such-as-primitives-strings-and
Comment From: leihuazhe
@wangweng You can see the reason in this method named
org.springframework.beans.BeanUtils#isSimpleValueType
. ```java public static boolean isSimpleProperty(Class<?> type) { Assert.notNull(type, "'type' must not be null"); return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType())); }public static boolean isSimpleValueType(Class<?> type) { return (Void.class != type && void.class != type && (ClassUtils.isPrimitiveOrWrapper(type) || Enum.class.isAssignableFrom(type) || CharSequence.class.isAssignableFrom(type) || Number.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || Temporal.class.isAssignableFrom(type) || URI.class == type || URL.class == type || Locale.class == type || Class.class == type)); } ```
老哥,非常感谢,我看到你ip是在上海,早知道我就用中文跟你沟通了,我英语比较烂😂,虽然知道原因了,但是我感觉这里还是有点怪怪的,不知道能不能只用setter来做判断,这样逻辑上也比较一致,自动装配通过setter注入就只与setter有关。因为有时候万一想控制get的逻辑就可能会出现这个错误。
Beacuse the Spring framework uses the java Introspector.getBeanInfo(Class) to get the Bean info.It's the Introspector mechanism uses the Getter and Setter to determine the write method and the read method of the bean you defined.
Comment From: wangweng
@wangweng You can see the reason in this method named
org.springframework.beans.BeanUtils#isSimpleValueType
. ```java public static boolean isSimpleProperty(Class<?> type) { Assert.notNull(type, "'type' must not be null"); return isSimpleValueType(type) || (type.isArray() && isSimpleValueType(type.getComponentType())); }public static boolean isSimpleValueType(Class<?> type) { return (Void.class != type && void.class != type && (ClassUtils.isPrimitiveOrWrapper(type) || Enum.class.isAssignableFrom(type) || CharSequence.class.isAssignableFrom(type) || Number.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || Temporal.class.isAssignableFrom(type) || URI.class == type || URL.class == type || Locale.class == type || Class.class == type)); } ```
老哥,非常感谢,我看到你ip是在上海,早知道我就用中文跟你沟通了,我英语比较烂😂,虽然知道原因了,但是我感觉这里还是有点怪怪的,不知道能不能只用setter来做判断,这样逻辑上也比较一致,自动装配通过setter注入就只与setter有关。因为有时候万一想控制get的逻辑就可能会出现这个错误。
Beacuse the Spring framework uses the java Introspector.getBeanInfo(Class) to get the Bean info.It's the Introspector mechanism uses the Getter and Setter to determine the write method and the read method of the bean you defined.
okay, I see. Thank you!
Comment From: bclozel
Please use StackOverflow for questions and discussions.