gpt4 book ai didi

Spring : Loading Properties from Database and local files

转载 作者:行者123 更新时间:2023-12-02 09:30:56 29 4
gpt4 key购买 nike

我正在尝试从数据库加载属性。我在这方面取得了成功。但现在的问题是,对于 dataSource bean,我想使用占位符。请查看applicationProperties.xml文件,然后就可以明白:

<!-- Data Source Bean -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>

<!-- My Own class for managing properites came from Database -->
<bean class="PropFromDB.PropFromDB.PropertiesUtil" >
<property name="propertiesArray">
<list>
<ref bean="propertiesFromDB" />
</list>
</property>
</bean>

<!-- PropertiesFactoryBean bean -->
<bean id="propertiesFromDB"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties" ref="commonsConfigurationFactoryBean" />
</bean>

<!-- CommonsConfigurationFactoryBean bean -->
<bean id="commonsConfigurationFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="databaseConfiguration"></constructor-arg>
</bean>

<!-- DatabaseConfiguration bean -->
<bean name="databaseConfiguration"
class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg index="0" ref="dataSource" />
<constructor-arg index="1" value="properties" />
<constructor-arg index="2" value="key" />
<constructor-arg index="3" value="value" />
</bean>

上面的代码用于从数据库加载属性。现在,正如您所看到的 dataSource bean,使用了一些占位符。所以我在顶部添加了这一行:

<context:property-placeholder location="classpath:databaseForConfiguration.properties"/>

databaseForConfiguration.properties 包含所有必需的属性并且位于类路径中:

driverClassName=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/mydb
username=user
password=pass

但是当我尝试执行时,出现以下异常:

 Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${driverClassName}]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:108)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489)
... 60 more

{url}{用户名}{密码}类似。

很容易理解,要初始化 PropertiesUtil bean,首先需要初始化 dataSource bean。对于 dataSource bean,本地属性占位符必须存在。 在这种情况下,没有得到。

我想要这两个东西从本地文件加载占位符也从数据库加载占位符

谁能帮我解决这个问题。

提前致谢。

最佳答案

对于

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>

我为数据源创建了工厂。现在它看起来像这样:

<bean id="dataSource" class="core.factory.DataSourceFactory" factory-method="createDataSource">
<constructor-arg type="java.lang.String" value="DBConfig.properties" />
</bean>

DataSourceFactory 类如下所示:

public class DataSourceFactory
{

private static final String DRIVER_CLASS_NAME = "db.driver";
private static final String URL = "db.url";
private static final String URL_LOGDB = "logdb.url";
private static final String USER_NAME = "db.username";
private static final String PASSWORD = "db.password";

public static DataSource createDataSource(String propertyFilename) throws IOException
{
Properties properties = getProperties(propertyFilename);
return getDataSource(URL, properties);
}

public static DataSource createDataSourceForLogDb(String propertyFilename) throws Exception
{
Properties properties = getProperties(propertyFilename);
return getDataSource(URL_LOGDB, properties);
}

private static Properties getProperties(final String fileName) throws IOException
{
Properties properties = new Properties();
InputStream in = DataSourceFactory.class.getClassLoader().getResourceAsStream(fileName);
properties.load(in);
in.close();
return properties;
}

private static DataSource getDataSource(final String url, final Properties properties)
{
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(properties.getProperty(DRIVER_CLASS_NAME));
dataSource.setUrl(properties.getProperty(url));
dataSource.setUsername(properties.getProperty(USER_NAME));
dataSource.setPassword(properties.getProperty(PASSWORD));
return dataSource;
}
}

现在我也可以从本地文件和数据库获取属性。

关于我在评论中提出的另一个问题,即从数据库加载 i18n 资源,我从此链接中得到了解决方案:Spring MVC: Database MessageSource fall back to properties file .

我自己发布了问题,现在我得到了解决方案。希望这对某人有用。

关于 Spring : Loading Properties from Database and local files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114615/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com