我刚接触Spring,我试图创建一个Database Manager页面,该页面显示页面加载时的数据库详细信息,并在用户按下Submit时更新数据库设置。
我遵循了本教程,并在属性文件中设置了数据库设置。
我设法以编程方式更新了属性文件中的数据库设置。当我使用以下代码检索数据库设置时
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>
我检查了属性文件,发现它已使用最新输入进行更新。
我想念什么?
提前致谢
PS我在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()
应用程序运行正常。请帮助
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
因此,我可以假设进行了一些更新。无法确定我为什么继续获得旧价值
UPDATE 完整的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。好的,现在我删除了它们之间的代理和服务,应用程序现在直接调用
我现在不使用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);
您需要刷新 Application上下文并DriverManagerDataSource再次获取Bean ,以反映写入属性文件的更改。
DriverManagerDataSource
context.refresh(); DriverManagerDataSource databaseSource = (DriverManagerDataSource)context.getBean("dataSource"); databaseSource.getUsername();
获取资源文件对象的提示:
URI a = getClass().getResource("/com/smartcall/bundle/database.properties").toURI(); File file = new File(a);
您无需解码路径,即用“”替换%20。推荐人