Details
Description
The issue is that while Spring Starts bean creation of datasource, It throws exception org.springframework.beans.NotWritablePropertyException. And complains that Invalid property 'connectionInitSqls' of bean class [org.apache.commons.dbcp2.BasicDataSource]: Bean property 'connectionInitSqls' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Here is My bean configuration in Spring's XML config file.
<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 Code from DBCP2 version 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; } }
There should be a new setter method for connectionInitSqls which accept List<E> Type argument.
There is a good discussion on this issue on Stackoverflow. http://stackoverflow.com/questions/40636372/spring-bean-creation-failed-can-parameter-type-of-the-setter-be-parent-of-the-r/40637584?noredirect=1#comment68514083_40637584