在我的applicationcontext.xml中我有这个:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dashboardsupervisor" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
Run Code Online (Sandbox Code Playgroud)
在这里我连接我的数据库:
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MySQLRdbHelper rdbHelper = (MySQLRdbHelper)
ctx.getBean("ManagerSupervisor");
Run Code Online (Sandbox Code Playgroud)
我想要的是不从我的applicationcontext.xml中读取密码"1234",并从本地驱动器中的某些属性文件中读取它.因为它将在不同的机器上运行,并且每个机器都有不同的密码.
我能做到这一点吗?
谢谢
是的,你可以,而关键是Springs PropertyPlaceholderConfigurer.
例如,您在文件系统上创建一个名为的文件database.properties,其中包含您的密码(注意,您还可以向此文件添加更多设置,如用户名,JDBC URL等).
jdbc.password=1234
Run Code Online (Sandbox Code Playgroud)
接下来,您需要声明一个PropertyPlaceholderConfigurerbean并将其指向该database.properties文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>path/to/database.properties</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
请注意,路径被解释为类路径资源,除非它带有前缀file:.
最后,替换dataSource bean 的配置:replace
<property name="password" value="1234" />
Run Code Online (Sandbox Code Playgroud)
同
<property name="password" value="${jdbc.password}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
381 次 |
| 最近记录: |