我有一个场景,我从属性文件中获取编码的密码值。我有自己的解密实现,因此想要从我的 java 类中解密密码,然后想要在其引用的地方进一步使用该解密值。
例如
<bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource"
... // more attributed set from properties like user, hostname etc.
p:password="${myPropertey.password}"
>
上面的代码我需要实现如下所示的内容,针对特定于密码属性,因为其余属性都很好,但密码需要在使用之前解密。
(下面的实现是错误的,但我刚刚提到是为了提供更多和清晰的想法)
<bean id="myDataSource" class="org.apache.common.dbcp.BasicDataSource"
... // more properties
p:password="myDecryptBean.decryptMyPassword(${myPropertey.password})"
>
基本上,我需要解密从属性文件中获得的密码,然后再使用它建立数据库连接。
感谢您的时间和任何帮助!!
最佳答案
由于您使用的是 Spring,我强烈建议您查看 PropertyResourceConfigurer
类。
请参阅Official Documentation .
特别要查看 convertProperty(String propertyName, String propertyValue)
方法,并考虑创建一个重写它的类。然后,您将能够将自定义解密逻辑添加到重写方法中,该方法将在每次 Spring 访问该属性时运行。
直接来自文档:
Allows for configuration of individual bean property values from a property resource, i.e. a properties file. Useful for custom config files ... that override bean properties configured in the application context.
关于java spring - 覆盖从属性文件接收的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35231414/