Java 在服务器运行时将表内容从Oracle DB加载到Springbean 我在Oracle11数据库中有一个表,有两列(KEY(varchar2),PROPERTY(varchar2)) 通过使用Spring框架,我希望将表中的全部内容(所有行)加载到Spring Bean中,这样我就可以一直使用Bean,而不是从数据库中获取值 我使用了Bean类“org.apache.commons.configuration.DatabaseConfiguration”。它工作正常,但是这个bean类没有将整个表内容加载到其中的规定
PFB,my Spring config.xml:Java 在服务器运行时将表内容从Oracle DB加载到Springbean 我在Oracle11数据库中有一个表,有两列(KEY(varchar2),PROPERTY(varchar2)) 通过使用Spring框架,我希望将表中的全部内容(所有行)加载到Spring Bean中,这样我就可以一直使用Bean,而不是从数据库中获取值 我使用了Bean类“org.apache.commons.configuration.DatabaseConfiguration”。它工作正常,但是这个bean类没有将整个表内容加载到其中的规定,java,spring,spring-mvc,spring-webflow,Java,Spring,Spring Mvc,Spring Webflow,PFB,my Spring config.xml: <bean name="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="commonsConfigurationFactoryBean"/&
<bean name="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="commonsConfigurationFactoryBean"/>
</bean>
<bean name="commonsConfigurationFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="databaseConfiguration"/>
</bean>
<bean name="databaseConfiguration"
class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="dataSource"/>
<constructor-arg type="java.lang.String" index="1" value="schema.Table_Name1"/>
<constructor-arg type="java.lang.String" index="2" value="KEY"/>
<constructor-arg type="java.lang.String" index="3" value="PROPERTY"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:SYSTEM_NS:PORT:sid"/>
<property name="username" value="xxxx"/>
<property name="password" value="xxxx"/>
</bean>
ApplicationContext classpathCtx = new ClassPathXmlApplicationContext("Spring-config.xml");
DatabaseConfiguration dBConfig = (DatabaseConfiguration)classpathCtx.getBean("databaseConfiguration");
// To display the value in the property column by sending the value of column KEY
System.out.println(dBConfig.getProperty("Value in the KEY column"));
上面的代码运行良好。但是每次我将密钥发送到DatabaseConfiguration
时,它都会在内部调用DataBase以获取我不希望发生的属性
上述整个概念都是关于尝试将.properties中存在的所有数据存储到DB中,并将其从DB中提取到我的Java代码中。您可以创建一个FactoryBean,该FactoryBean通过读取数据库配置中的所有键并将它们复制到创建的properties对象来创建properties对象 这个属性对象是一个单例对象,可以在spring配置中使用
<bean name="properties" class="...ConfigurationPropertiesFactoryBean">
<property name="configuration" ref="databaseConfiguration"/>
</bean>
public class ConfigurationPropertiesFactoryBean implements FactoryBean<Properties>
{
private Properties props = new Properties();
public void setConfiguration(Configuration configuration)
{
props.put ...
}
public Properties getObject()
{
return props;
}
}
公共类配置属性FactoryBean实现FactoryBean
{
私有属性props=新属性();
公共无效设置配置(配置)
{
道具。。。
}
公共属性getObject()
{
返回道具;
}
}
或者,您可以使用JdbcTemplate代替数据库配置:
<bean name="properties" class="...DataSourcePropertiesFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
public class DataSourcePropertiesFactoryBean implements FactoryBean<Properties>
{
private Properties props = new Properties();
public void setDataSource(DataSource dataSource)
{
props = new JdbcTemplate(dataSource).query("SELECT KEY, PROPERTY FROM ...",
new ResultSetExtractor(){
public void Properties extractData(ResultSet rs)
{
props.put(...);
}
});
}
public Properties getObject()
{
return props;
}
}
公共类DataSourcePropertiesFactoryBean实现FactoryBean
{
私有属性props=新属性();
public void setDataSource(数据源数据源)
{
props=新的JdbcTemplate(数据源)。查询(“选择键,属性来自…”,
新的resultSetTextRactor(){
公共void属性提取数据(结果集rs)
{
道具。放(…);
}
});
}
公共属性getObject()
{
返回道具;
}
}
您可以创建一个FactoryBean,该FactoryBean通过读取数据库配置中的所有键并将它们复制到创建的properties对象来创建properties对象
这个属性对象是一个单例对象,可以在spring配置中使用
<bean name="properties" class="...ConfigurationPropertiesFactoryBean">
<property name="configuration" ref="databaseConfiguration"/>
</bean>
public class ConfigurationPropertiesFactoryBean implements FactoryBean<Properties>
{
private Properties props = new Properties();
public void setConfiguration(Configuration configuration)
{
props.put ...
}
public Properties getObject()
{
return props;
}
}
公共类配置属性FactoryBean实现FactoryBean
{
私有属性props=新属性();
公共无效设置配置(配置)
{
道具。。。
}
公共属性getObject()
{
返回道具;
}
}
或者,您可以使用JdbcTemplate代替数据库配置:
<bean name="properties" class="...DataSourcePropertiesFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
public class DataSourcePropertiesFactoryBean implements FactoryBean<Properties>
{
private Properties props = new Properties();
public void setDataSource(DataSource dataSource)
{
props = new JdbcTemplate(dataSource).query("SELECT KEY, PROPERTY FROM ...",
new ResultSetExtractor(){
public void Properties extractData(ResultSet rs)
{
props.put(...);
}
});
}
public Properties getObject()
{
return props;
}
}
公共类DataSourcePropertiesFactoryBean实现FactoryBean
{
私有属性props=新属性();
public void setDataSource(数据源数据源)
{
props=新的JdbcTemplate(数据源)。查询(“选择键,属性来自…”,
新的resultSetTextRactor(){
公共void属性提取数据(结果集rs)
{
道具。放(…);
}
});
}
公共属性getObject()
{
返回道具;
}
}
是的,我接受按照您指导的方式创建单例obj,但在您给出的代码“”中,我们指的是databaseConfigurationBean,它没有加载所有行的规定,即数据库中的所有键值对。。。假设数据库中有100个键值对。然后,要加载所有行,必须进行100 dB调用。我的重点是找到一种使用Spring在一个dB调用中加载所有行的方法。添加了第二种替代方法,即使用JdbcTemplate进行单个SQL查询,一次获取所有键值对。是的,我接受按照您指导的方式创建单例obj,但使用您给出的代码“”,我们指的是databaseConfigurationBean,它没有加载所有行的规定,即从数据库中加载所有键值对。。。假设数据库中有100个键值对。然后,要加载所有行,必须进行100 dB调用。我的重点是找到一种使用Spring在一个dB调用中加载所有行的方法。添加了第二种替代方法,即使用JdbcTemplate进行单个SQL查询,一次获取所有键值对。