Spring框架使用properties配置数据源Bean【Spring入门】
2017-02-26 20:54
351 查看
使用了Spring框架之后,我们可以在xml文件里配置bean,也可以在java代码中配置bean,这已经大大方便了我们。现在我们要把数据源的类配置成bean,并且把数据源bean需要的属性写在jdbc.properties中,这样可以方便之后更改,也更加清晰明了。
jdbc.properties代码:#jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db
jdbc.username=root
jdbc.password=adminspring容器xml代码:
jdbc.properties代码:#jdbc
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db
jdbc.username=root
jdbc.password=adminspring容器xml代码:
<context:property-placeholder location="classpath*:jdbc.properties"/><!--加载配置文件--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/><!--用表达式来获取配置文件中的值--> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
相关文章推荐
- Spring入门Blog[十、Spring用xml和properties方式配置数据源]
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- Spring框架——使用spring的特殊bean完成分散配置
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- spring管理属性配置文件properties——使用PropertiesFactoryBean
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- 使用Spring的JndiObjectFactoryBean配置数据源时需要注意的问题
- 使用dom4j:读取spring配置文件,实例化bean对象并注入依赖对象
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
- 使用properties配置spring
- 什么是jndi,什么时候使用jndi,tomcat配置jndi,spring使用jndi数据源
- Spring 配置使用 - Bean 生命周期
- spring 配置sqlserver数据源-使用集成身份登陆
- spring 第3天使用java类和XML配置bean
- Spring多数据源的配置和使用
- Spring入门学习——bean属性配置(二)