操纵Spring Bean 属性
Manipulation of Spring Bean property
我想换一个属性的bean。我只想更改一次以提高性能(从 XML 读取时更好),而不是在每个 bean 实例实例化中更改它。在 Spring 中最好的方法是什么?
为了详细说明并给出一个具体的例子:
下面是databaseContext.xml中的数据源bean声明。
我想用 JASYPT 解密值为 ENC(....) 的 ${jdbc.password}。
我无法使用 Jaspt Spring 集成来做到这一点,因为 Jaspt 还不兼容 Spring5 并且不兼容 Jasypt Hibernate 集成,因为使用了 Hibernate 以外的其他数据源。
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1 from dual" />
<property name="dataSourceClassName" value="oracle.jdbc.pool.OracleDataSource" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">${jdbc.url}</prop>
<prop key="user">${jdbc.user}</prop>
<prop key="password">${jdbc.password}</prop>
</props>
</property>
</bean>
这对我帮助很大:
Spring property placeholder decrypt resolved property
只是想对一些发现添加一些小的更正和额外的注释:
上面的link写的是"location",但是一定是下面写的"locations",在applicationContext.xml
<bean class="com.dummy.util.EncryptationAwarePropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:database.properties</value>
<value>classpath*:hibernate.properties</value>
</list>
</property>
</bean>
"PropertyPlaceholderConfigurer" 已弃用。但它仍然有效。如果您尝试使用新提出的 class "PropertySourcesPlaceholderConfigurer",它有一个错误,它不会调用 "convertPropertyValue" 方法,如下所述:https://github.com/spring-projects/spring-framework/issues/13568。顺便说一下解决方法。
public class EncryptationAwarePropertySourcesPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected String convertPropertyValue(String originalValue) {
if (originalValue != null && originalValue.startsWith("ENC(")) {
return decrypt(originalValue);
}
return originalValue;
}
.
.
}
我想换一个属性的bean。我只想更改一次以提高性能(从 XML 读取时更好),而不是在每个 bean 实例实例化中更改它。在 Spring 中最好的方法是什么?
为了详细说明并给出一个具体的例子:
下面是databaseContext.xml中的数据源bean声明。 我想用 JASYPT 解密值为 ENC(....) 的 ${jdbc.password}。 我无法使用 Jaspt Spring 集成来做到这一点,因为 Jaspt 还不兼容 Spring5 并且不兼容 Jasypt Hibernate 集成,因为使用了 Hibernate 以外的其他数据源。
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="connectionTestQuery" value="SELECT 1 from dual" />
<property name="dataSourceClassName" value="oracle.jdbc.pool.OracleDataSource" />
<property name="maximumPoolSize" value="10" />
<property name="idleTimeout" value="30000" />
<property name="dataSourceProperties">
<props>
<prop key="url">${jdbc.url}</prop>
<prop key="user">${jdbc.user}</prop>
<prop key="password">${jdbc.password}</prop>
</props>
</property>
</bean>
这对我帮助很大: Spring property placeholder decrypt resolved property
只是想对一些发现添加一些小的更正和额外的注释:
上面的link写的是"location",但是一定是下面写的"locations",在applicationContext.xml
<bean class="com.dummy.util.EncryptationAwarePropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:database.properties</value>
<value>classpath*:hibernate.properties</value>
</list>
</property>
</bean>
"PropertyPlaceholderConfigurer" 已弃用。但它仍然有效。如果您尝试使用新提出的 class "PropertySourcesPlaceholderConfigurer",它有一个错误,它不会调用 "convertPropertyValue" 方法,如下所述:https://github.com/spring-projects/spring-framework/issues/13568。顺便说一下解决方法。
public class EncryptationAwarePropertySourcesPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected String convertPropertyValue(String originalValue) {
if (originalValue != null && originalValue.startsWith("ENC(")) {
return decrypt(originalValue);
}
return originalValue;
}
.
.
}