我是春天新手 我正在尝试创建一个数据库管理器页面,该页面显示页面加载时的数据库详细信息,并在用户按提交时更新数据库设置
我跟着this tutorial并在属性文件中设置了数据库设置。
我设法以编程方式更新属性文件中的数据库设置。当我使用以下代码检索数据库设置时
DriverManagerDataSource databaseSource = (DriverManagerDataSource)context.getBean("dataSource");
databaseSource.getUsername();
我设法获得旧值并且无法获得新值
这是applicationContext.xml文件中的映射
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/bundle/database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
我检查了属性文件,发现它是用最新的输入更新的。
我错过了什么?
提前致谢
P.S我在Spring 3中使用JSF1.2
更新
让我的要求变得简单。我正在创建一个setparate dbsettings页面,这样当用户希望连接到不同的数据库时,他/她可以在dbsettings页面中输入详细信息并连接
更新
我使用此代码检索应用程序上下文
private ApplicationContext context;
...
ServletContext servletContext = FacesUtil.getServletContext();
this.context = WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
如果我将其更改为AbstractApplicationContext,我需要在上面的代码中进行哪些更改?
更新
this.context = (AbstractApplicationContext)WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
String valueForTheProperty = "";
context.refresh();
当我添加上面的行时,我正在
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is java.lang.NullPointerException
org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:599)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy1.getDatabasePasswordFromSettings(Unknown Source)
com.smartcall.delegate.DbSettingsDelegate.getDatabasePassswordFromSettings(DbSettingsDelegate.java:38)
com.smartcall.bean.DbSettingsBean.loadDatabaseSettings(DbSettingsBean.java:49)
com.smartcall.bean.DbSettingsBean.<init>(DbSettingsBean.java:41)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
java.lang.Class.newInstance0(Class.java:355)
java.lang.Class.newInstance(Class.java:308)
com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:185)
com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105)
com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:368)
com.sun.faces.mgbean.BeanManager.create(BeanManager.java:230)
com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:68)
org.apache.el.parser.AstValue.getValue(AstValue.java:112)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:246)
com.sun.facelets.tag.jsf.ComponentHandler.createComponent(ComponentHandler.java:224)
com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:139)
com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java:314)
com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169)
com.sun.facelets.tag.jsf.core.ViewHandler.apply(ViewHandler.java:109)
com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
com.sun.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:49)
com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
com.sun.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:25)
com.sun.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:95)
com.sun.facelets.FaceletViewHandler.buildView(FaceletViewHandler.java:524)
com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:567)
org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
root cause
java.lang.NullPointerException
org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.nextTimestamp(RegionFactoryCacheProviderBridge.java:85)
org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:526)
org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:535)
org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:493)
org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377)
org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy1.getDatabasePasswordFromSettings(Unknown Source)
com.smartcall.delegate.DbSettingsDelegate.getDatabasePassswordFromSettings(DbSettingsDelegate.java:38)
com.smartcall.bean.DbSettingsBean.loadDatabaseSettings(DbSettingsBean.java:49)
com.smartcall.bean.DbSettingsBean.<init>(DbSettingsBean.java:41)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
java.lang.Class.newInstance0(Class.java:355)
java.lang.Class.newInstance(Class.java:308)
com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:185)
com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:105)
com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:368)
com.sun.faces.mgbean.BeanManager.create(BeanManager.java:230)
com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:68)
org.apache.el.parser.AstValue.getValue(AstValue.java:112)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
com.sun.faces.application.ApplicationImpl.createComponent(ApplicationImpl.java:246)
com.sun.facelets.tag.jsf.ComponentHandler.createComponent(ComponentHandler.java:224)
com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:139)
com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
com.sun.facelets.tag.jsf.ComponentHandler.applyNextHandler(ComponentHandler.java:314)
com.sun.facelets.tag.jsf.ComponentHandler.apply(ComponentHandler.java:169)
com.sun.facelets.tag.jsf.core.ViewHandler.apply(ViewHandler.java:109)
com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
com.sun.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:49)
com.sun.facelets.tag.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:47)
com.sun.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:25)
com.sun.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:95)
com.sun.facelets.FaceletViewHandler.buildView(FaceletViewHandler.java:524)
com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:567)
org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
当我注释掉这行
时context.refresh()
申请工作正常。 Plz帮助
更新
DBSettingBean
public DbSettingsBean()
{
dbSettingsDelegate = new DbSettingsDelegate();
loadDatabaseSettings();
submitButton.setRendered(false);
testButton.setRendered(true);
}
public void loadDatabaseSettings()
{
this.setUserName(dbSettingsDelegate.getDatabaseUserNameFromSettings());
this.setPassword(dbSettingsDelegate.getDatabasePassswordFromSettings());
this.setServerName(dbSettingsDelegate.getDatabaseServerNameFromSettings());
this.setDataSourceName(dbSettingsDelegate.getDatabaseNameFromSettings());
}
public HtmlOutputText getText()
{
return text;
}
public void setText(HtmlOutputText text)
{
this.text = text;
}
public HtmlAjaxCommandButton getSubmitButton()
{
return submitButton;
}
public void setSubmitButton(HtmlAjaxCommandButton submitButton)
{
this.submitButton = submitButton;
}
public HtmlCommandButton getTestButton()
{
return testButton;
}
public void setTestButton(HtmlCommandButton testButton)
{
this.testButton = testButton;
}
public HtmlOutputText getStatusMessageComponent()
{
return statusMessageComponent;
}
public void setStatusMessageComponent(HtmlOutputText statusMessageComponent)
{
this.statusMessageComponent = statusMessageComponent;
}
public UIInput getPassworder()
{
return passworder;
}
public void setPassworder(UIInput passworder)
{
this.passworder = passworder;
}
public String getServerName()
{
return serverName;
}
public void setServerName(String serverName)
{
this.serverName = serverName;
}
public String getDataSourceName()
{
return dataSourceName;
}
public void setDataSourceName(String dataSourceName)
{
this.dataSourceName = dataSourceName;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String testWhetherTheConnectionExists()
{
return dbSettingsDelegate.checkWhetherDatabaseConnectionExists(getUserName(), getPassword(), getDataSourceName(),getServerName());
}
和..
DbSettingsDelegate
public DbSettingsDelegate()
{
try
{
System.out.println("Dbsettings delegate: "+FacesContext.getCurrentInstance());
dbSettingsService = (DbSettingsService) ServiceLocator
.getInstance().getService(
ServiceLocator.Services.DBSETTINGS);
}
catch (Exception e)
{
logger.debug("error in AddressBookDelegate constructor", e);
}
}
public String getDatabaseUserNameFromSettings()
{
return dbSettingsService.getDatabaseUserNameFromSettings();
}
public String getDatabasePassswordFromSettings()
{
return dbSettingsService.getDatabasePasswordFromSettings();
}
public String getDatabaseServerNameFromSettings()
{
return dbSettingsService.getDatabaseServerNameFromSettings();
}
public String getDatabaseNameFromSettings()
{
return dbSettingsService.getDatabaseNameFromSettings();
}
public String checkWhetherDatabaseConnectionExists(String theUserName,String thePassword,String theDatabaseName,String theServerName)
{
return dbSettingsService.checkWhetherTheDatabaseConnectionExists(theUserName, thePassword, theDatabaseName, theServerName);
}
这是检索要向用户显示的数据库详细信息的代码
ServletContext servletContext = FacesUtil.getServletContext();
this.context = (AbstractApplicationContext)WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
String valueForTheProperty = "";
DriverManagerDataSource databaseSource = null;
context.refresh();
databaseSource = (DriverManagerDataSource)context.getBean("dataSource");
if(theProperty.equalsIgnoreCase("username"))
{
valueForTheProperty = databaseSource.getUsername();
}
else if(theProperty.equalsIgnoreCase("password"))
{
valueForTheProperty = databaseSource.getPassword();
}
else if(theProperty.equalsIgnoreCase("server"))
{
valueForTheProperty = this.splitUrlAndGetValueForKey(databaseSource.getUrl(), "server");
System.out.println("Server: "+valueForTheProperty);
}
else
{
valueForTheProperty = this.splitUrlAndGetValueForKey(databaseSource.getUrl(), "databasename");
}
更新
当我调试代码时,我在控制台中找到了这些行。调用context.refresh()
时
Dec 28, 2010 3:22:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: Running hbm2ddl schema update
Dec 28, 2010 3:22:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: fetching database metadata
Dec 28, 2010 3:22:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: updating schema
Dec 28, 2010 3:22:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: schema update complete
所以我可以假设一些更新继续进行。无法确定为什么我会继续获取旧值
更新 完整的applicationCOntext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="prototype">
<property name="location">
<value>classpath:/bundle/database.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Hibernate SessionFactory Definition -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">
true
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- Spring Data Access Exception Translator Definition -->
<bean id="jdbcExceptionTranslator"
class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- Hibernate Template Definition -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="jdbcExceptionTranslator">
<ref bean="jdbcExceptionTranslator" />
</property>
</bean>
<!--
========================= Start of SERVICE DEFINITIONS
=========================
-->
<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- AddressBook Service Definition -->
<bean id="loginServiceTarget" class="com.smartcall.service.impl.LoginServiceImpl">
<property name="loginDAO">
<ref local="loginDAO" />
</property>
</bean>
<!-- Login DAO Definition: Hibernate implementation -->
<bean id="loginDAO"
class="com.smartcall.dao.hibernate.HibernateLoginDAO">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<!-- Transactional proxy for the AddressBook Service -->
<bean id="loginService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="loginServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
context.refresh()刷新我认为的整个applicationContext bean。好了,我现在删除了那些delgate和服务,应用程序现在直接调用
我现在不使用DBSettingsDelegate
更新
更新属性文件代码
Properties properties = new Properties();
String a = Thread.currentThread()
.getContextClassLoader()
.getResource("/com/smartcall/bundle/database.properties").getFile();
a = a.replaceAll("%20", " ");
String url = "";
url = "jdbc:mysql://"+theServerName+":3306/"+theDatabaseName+"?characterEncoding=UTF-8";
File file = new File(a);
//file.setWritable(true);
System.out.println(url);
System.out.println(file.getAbsolutePath());
FileInputStream stream = new FileInputStream(file);
properties.load(stream);
properties.put("jdbc.username", theUserName);
properties.put("jdbc.password", thePassword);
properties.put("jdbc.url", url);
properties.store(new FileOutputStream(file),null);
答案 0 :(得分:2)
您需要refresh您的应用程序上下文并再次获取bean DriverManagerDataSource
,以反映写入属性文件的更改。
context.refresh();
DriverManagerDataSource databaseSource = (DriverManagerDataSource)context.getBean("dataSource");
databaseSource.getUsername();
获取资源的File对象的提示:
URI a = getClass().getResource("/com/smartcall/bundle/database.properties").toURI();
File file = new File(a);
您无需解码路径,即用“”替换%20。 Referrence
答案 1 :(得分:1)
在应用程序启动时读取值,将它们设置为目标对象(在您的情况下为数据源),并且不再读取属性文件。因此,即使您更改了值,它们也不会反映在任何地方。
要使PropertyPlaceholderConfigurer
重新加载值,您必须对其进行扩展并指定重新加载期。但是仍然不会更新数据源。您还应该扩展数据源类,并在给定的时间间隔后重新读取属性。或者您可以将DataSource
声明为范围prototype
,以便获取新实例。每次都会重新加载属性。
如果您不想重新启动整个上下文(这可能需要很长时间),那就是