1. jdbc.properties
database.url=jdbc:mysql://localhost/smaple
database.driver=org.gjt.mm.mysql.Driver
database.user=root
database.password=star1xing
2.conf.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>com/starxing/test/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
</beans>
3.Config.java
package com.starxing.test;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Config {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"com/starxing/test/conf.xml"));
// 如果要在BeanFactory中使用,bean factory post-processor必须手动运行:
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource(
"com/starxing/test/jdbc.properties"));
cfg.postProcessBeanFactory(factory);
DriverManagerDataSource dataSource = (DriverManagerDataSource) factory
.getBean("dataSource");
System.out.println(dataSource.getDriverClassName());
// 注意,ApplicationContext能够自动辨认和应用在其上部署的实现了BeanFactoryPostProcessor的bean。这就意味着,当使用ApplicationContext的时候应用PropertyPlaceholderConfigurer会非常的方便。由于这个原因,建议想要使用这个或者其他bean
// factory postprocessor的用户使用ApplicationContext代替BeanFactroy。
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/starxing/test/conf.xml");
DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context
.getBean("dataSource");
System.out.println(dataSource2.getDriverClassName());
}
}
分享到:
相关推荐
2. **Spring配置文件**:在Spring的XML配置文件中,我们需要声明一个`SchedulerFactoryBean`,它是Spring对Quartz调度器的封装。通过这个bean,我们可以配置Quartz的相关属性,如JobStore类型(内存或数据库存储),...
这是Spring MVC的配置文件,负责定义控制器(Controller)、视图解析器、静态资源处理等。 6. 配置SpringContext.xml。这部分通常是定义Spring的Bean,包括业务逻辑组件、数据访问对象(DAO)、事务管理器等。 7. ...
1. 从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfactory 产生 session 3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做...
application.properties:Spring Boot应用的配置文件;可以修改一些默认设置; ———————————————— 版权声明:本文为CSDN博主「倦眼问酒书j」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上...
2. `src/main/resources`: 包含配置文件,如`application.properties`或`application.yml`,以及静态资源(如HTML、CSS、JavaScript)和模板引擎文件(如Thymeleaf或Freemarker)。 3. `pom.xml`: Maven项目对象模型...
在Spring Boot中,可以通过在`application.properties`或`application.yml`配置文件中设置`server.ssl.*`属性来开启和配置HTTPS。 总的来说,`pay5160`项目是一个使用Java编写的Web应用,重点关注HTTPS的安全通信。...
(不可偷懒想双击redis-server.exe完事,这样虽然也能打开,但不会加载配置文件)。 另外和别的数据库一样,我们需要安装一个辅助的可视化工具Redis Desktop Manager,这是下载地址:...
2009-03-20 16:37 254350 86752 常见的专业问题解决办法\解决Struts中ApplicationResources_properties文件不支持中文问题_微科工作室 QQ:28790953____.mht 2009-02-25 08:45 文件夹 文件夹 常见的专业问题解决办法...