简体   繁体   English

Spring bean创建失败。 setter的参数类型可以是getter的返回类型的父代吗?

[英]Spring bean creation failed. Can parameter type of the setter be parent of the return type of the getter?

I am facing this exception while creating bean of datasource from DBCP2. 从DBCP2创建数据源bean时,我遇到此异常。 Exception is 例外是

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'connectionInitSqls' of bean class [org.apache.commons.dbcp2.BasicDataSource]: Bean property 'connectionInitSqls' is not writable or has an invalid setter method. 由以下原因引起:org.springframework.beans.NotWritablePropertyException:Bean类[org.apache.commons.dbcp2.BasicDataSource]的无效属性'connectionInitSqls':Bean属性'connectionInitSqls'是不可写的或具有无效的setter方法。 Does the parameter type of the setter match the return type of the getter? setter的参数类型是否与getter的返回类型匹配?

here is my bean configuration 这是我的bean配置

<bean id="fileStore_dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close" lazy-init="true">
    <!-- Just that property which causes problem -->
    <property name="connectionInitSqls">
        <list>
            <value>#{filestore.jdbc.connectionInitSql}</value>
        </list>
    </property>

</bean>

Here is the setter and getter code for connectionInitSqls in BasicDataSource class. 这是BasicDataSource类中connectionInitSqls的设置程序和获取程序代码。 version of DBCP2 is 2.1.1 DBCP2的版本是2.1.1

private volatile List<String> connectionInitSqls;

public List<String> getConnectionInitSqls() {
    final List<String> result = connectionInitSqls;
    if (result == null) {
        return Collections.emptyList();
    }
    return result;
}

public void setConnectionInitSqls(final Collection<String> connectionInitSqls) {
    if (connectionInitSqls != null && connectionInitSqls.size() > 0) {
        ArrayList<String> newVal = null;
        for (final String s : connectionInitSqls) {
        if (s != null && s.trim().length() > 0) {
                if (newVal == null) {
                    newVal = new ArrayList<>();
                }
                newVal.add(s);
            }
        }
        this.connectionInitSqls = newVal;
    } else {
        this.connectionInitSqls = null;
    }
}

You can see that argument in setter is Collection which is Super type of List. 您可以看到setter中的参数是Collection,它是List的超级类型。 But I dont know why spring could not instantiate the bean. 但是我不知道为什么春天不能实例化bean。 Is this Spring problem or Bug in DBCP2 code. 这是Spring问题还是DBCP2代码中的Bug? Can we give parent type of property in setter argument? 我们可以在setter参数中指定父类型的属性吗? How can I resolve this problem? 我该如何解决这个问题? Any Help would be appreciated. 任何帮助,将不胜感激。

Try ${ instead of #{ 尝试${代替#{

<property name="connectionInitSqls">
        <list>
            <value>${filestore.jdbc.connectionInitSql}</value>
        </list>
    </property>

Spring will use reflection to find the setter property. Spring将使用反射来查找setter属性。 So it will find the setter using setConnectionInitSqls and argument list because of the property type(which it will find from getter method getConnectionInitSqls) and it will not find therefore the exception. 因此,由于属性类型(它将从getter方法getConnectionInitSqls中找到),它将使用setConnectionInitSqls和参数列表来查找setter,因此不会找到异常。

Exception message is self explanatory now. 现在,异常消息是不言自明的。 Note that the property may not exist at all. 请注意,该属性可能根本不存在。 Spring just works with getters and setters. Spring只能与getter和setter一起使用。 It finds the appropriate setter method using the getter method's( which is easy to find just prefix with get and no arg method) return value type. 它使用getter方法的返回值类型(该方法很容易找到仅带有get且没有arg方法的前缀)来找到合适的setter方法。

Bean property 'connectionInitSqls' is not writable or has an invalid setter method. Bean属性“ connectionInitSqls”不可写或具有无效的setter方法。 Does the parameter type of the setter match the return type of the getter ?` setter的参数类型是否与getter的返回类型匹配


you can try using MethodInvokingFactoryBean . 您可以尝试使用MethodInvokingFactoryBean

<bean id="fileStore_dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    destroy-method="close" lazy-init="true">
</bean>

<bean id="customInjector"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="fileStore_dataSource" />
    <property name="targetMethod">
        <value>setConnectionInitSqls</value>
    </property>
    <property name="arguments">
        <list>
            <value>#{filestore.jdbc.connectionInitSql}</value>
        </list>
    </property>
</bean>


Alternative way: 替代方式:
I would prefer this as it is SAFER . 更喜欢这个,因为它是SAFER Reason being all properties are set during the instantiation phase itself. 原因是所有属性都在实例化阶段本身中设置。 And then wiring between the beans happen. 然后在豆之间进行接线。 In the previous case it may be error prone because setting connectionInitSqls happens at a different time and chances are that connections might have already been created(without looking into the internals of the implementation of BasicDataSource). 在前一种情况下,它可能易于出错,因为设置connectionInitSqls的时间是不同的,并且可能已经创建了连接(而无需查看BasicDataSource实现的内部)。

public class CustomBasicDataSource extends BasicDataSource{
    public void setConnectionInitSqls(List<String> connectionInitSqls) {
        super.setConnectionInitSqls(connectionInitSqls);
    }
}

replace with this class in xml 用xml中的此类替换

<bean id="fileStore_dataSource"
    class="org.company.somepackage.CustomBasicDataSource" destroy-method="close" lazy-init="true">
  ...<!-- rest remain same-->
</bean>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring MVC:Bean属性不可读或具有无效的getter方法getter的返回类型是否与setter的参数类型匹配 - Spring MVC: Bean property is not readable or has an invalid getter method Does the return type of the getter match the parameter type of the setter Bean属性不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配? 春季批 - Bean property is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Spring Batch Bean属性“ cmpcode”不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配? - Bean property 'cmpcode' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性&#39;xxxx不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配 - Bean property 'xxxx is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter Bean 属性 &#39;xxx&#39; 不可读或具有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配? - Bean property 'xxx' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? Bean属性“ trustStore”不可写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配? - Bean property 'trustStore' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? Bean 属性 'sakilaDao' 不可写或具有无效的 setter 方法。 setter 的参数类型是否与 getter 的返回类型匹配? - Bean property 'sakilaDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? setter的参数类型是否与getter的返回类型匹配? - Does the parameter type of the setter match the return type of the getter? 返回类型的spring bean - return spring bean with type "<i>Spring Boot @Mapper Bean creation issue : Application Failed to start.<\/i> Spring Boot @Mapper Bean 创建问题:应用程序无法启动。<\/b> <i>Error : Consider defining a bean of Type<\/i>错误:考虑定义一个 bean 类型<\/b>" - Spring Boot @Mapper Bean creation issue : Application Failed to start. Error : Consider defining a bean of Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM