简体   繁体   English

org.springframework.beans.NotWritablePropertyException。 无效属性<blah>不可写或具有无效的 setter 方法</blah>

[英]org.springframework.beans.NotWritablePropertyException. Invalid property <blah> is not writable or has an invalid setter method

I have the requirement to remove all passwords and encryption keys from the source code of my project.我需要从项目的源代码中删除所有密码和加密密钥。 I'm struggling to get this to work in my spring-servlet.xml file.我正在努力让它在我的 spring-servlet.xml 文件中工作。 This worked before I tried making the change:这在我尝试进行更改之前有效:

  • the database username, encrypted password, URL and driver were defined in a file jdbc_server.properties as a classpath resource.数据库用户名、加密密码、URL 和驱动程序在文件 jdbc_server.properties 中定义为类路径资源。
  • the encryption/decryption key was passed on start-up as -DENCRYPTION_PASSWORD=加密/解密密钥在启动时作为 -DENCRYPTION_PASSWORD= 传递

I want to move the jdbc_server_properties file to the filesystem and include the key in the file.我想将 jdbc_server_properties 文件移动到文件系统并在文件中包含密钥。

This is my last (of at least 25) attempt at getting this to work.这是我最后一次(至少 25 次)尝试让它发挥作用。

<bean id="propertyConfigurer" class="org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="standardEncryptor" />
        <property name="jdbcUsername" value="JDBC_USERNAME" />
        <property name="jdbcPassword" value="JDBC_PASSWORD" />
        <property name="jdbcUrl" value="JDBC_URL" />
        <property name="jdbcDriver" value="JDBC_DRIVER" />
        <property name="locations">
            <list>
                <value>file:///${USERPROFILE}/credentials/jdbc_server.properties</value>
            </list>
        </property>
</bean>

<bean id="standardEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
</bean>

<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES" />
        <property name="passwordSysPropertyName" value="ENCRYPTION_PASSWORD" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbcDriver}" p:url="${jdbcUrl}"
        p:username="${jdbcUsername}" p:password="${jdbcPassword}">
</bean>

Using the above configuration, I get a NotWritablePropertyException exception.使用上面的配置,我得到一个 NotWritablePropertyException 异常。 I've seen tons of posts on this issue but not where both the properties and encryption key are in a file on the filesystem.我已经看到了大量关于这个问题的帖子,但没有看到属性和加密密钥都在文件系统上的文件中。 When this was working and the properties were read from a file in the classpath, there were no getters/setters for jdbcUsername (or the other properties) so I don't know why it's failing in this way now.当它工作并且从类路径中的文件中读取属性时,jdbcUsername(或其他属性)没有getter / setter,所以我不知道为什么它现在以这种方式失败。

I tried adding the getters and setters (as String) to my BaseDaoImpl class but I still get the same error so if I'm supposed to add them, I'm not sure where they go.我尝试将 getter 和 setter(作为字符串)添加到我的 BaseDaoImpl class 但我仍然遇到相同的错误,所以如果我应该添加它们,我不确定它们在哪里 go。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertyConfigurer' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'jdbcUsername' of bean class [org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer]: Bean property 'jdbcUsername' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1568)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
    at ...

That is probably because jdbcUsername is not a property of EncryptablePropertyPlaceholderConfigurer .这可能是因为jdbcUsername不是EncryptablePropertyPlaceholderConfigurer的属性。

Please, try something like this (just remove the jdbc* properties from the EncryptablePropertyPlaceholderConfigurer configuration, and use it directly in your dataSource bean):请尝试这样的事情(只需从EncryptablePropertyPlaceholderConfigurer配置中删除jdbc*属性,然后直接在您的dataSource bean 中使用它):

<bean id="propertyConfigurer" class="org.jasypt.spring4.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="standardEncryptor" />
        <property name="locations">
            <list>
                <value>file:///${USERPROFILE}/credentials/jdbc_server.properties</value>
            </list>
        </property>
</bean>

<bean id="standardEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
</bean>

<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES" />
        <property name="passwordSysPropertyName" value="ENCRYPTION_PASSWORD" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${JDBC_DRIVER}" p:url="${JDBC_URL}"
        p:username="${JDBC_USERNAME}" p:password="${JDBC_PASSWORD}">
</bean>

Assuming your jdbc_server.properties file contains the required information:假设您的jdbc_server.properties文件包含所需的信息:

JDBC_USERNAME=ENC(...)
JDBC_PASSWORD=ENC(...)
JDBC_URL=ENC(...)
JDBC_DRIVER=your.jdbc.driver

暂无
暂无

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

相关问题 NotWritablePropertyException:Bean属性“ dataSource”不可写或具有无效的setter方法 - NotWritablePropertyException: Bean property 'dataSource' is not writable or has an invalid setter method org.springframework.beans.NotWritablePropertyException:无效的属性 - org.springframework.beans.NotWritablePropertyException: Invalid property org.springframework.beans.NotWritablePropertyException:bean类的属性&#39;xxxx&#39;无效: - org.springframework.beans.NotWritablePropertyException: Invalid property 'xxxx' of bean class : org.springframework.beans.NotWritablePropertyException:bean class 的无效属性“adminEmails” - org.springframework.beans.NotWritablePropertyException: Invalid property 'adminEmails' of bean class org.springframework.beans.NotWritablePropertyException:Bean类的无效属性“ configurationClass” - org.springframework.beans.NotWritablePropertyException: Invalid property 'configurationClass' of bean class org.springframework.beans.NotReadablePropertyException:无效的属性。 Bean属性不可读或具有无效的getter方法 - org.springframework.beans.NotReadablePropertyException: Invalid property. Bean property is not readable or has an invalid getter method Bean属性“ bagBisDomainService”不可写或具有无效的setter方法 - Bean property 'bagBisDomainService' is not writable or has an invalid setter method 春季:Bean属性不可写或具有无效的setter方法 - Spring: Bean property is not writable or has an invalid setter method Bean属性“transactionManagerBeanName”不可写或具有无效的setter方法 - Bean property 'transactionManagerBeanName' is not writable or has an invalid setter method Bean属性xxxDAO无法写或具有无效的setter方法 - Bean property xxxDAO is not writable or has an invalid setter method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM