一篇文章带你掌握主流基础框架——Spring( 九 )

案例:数据源对象管理针对一个新的数据源对象 , 我们采用两步来创建bean(我们以druid为案例):

  • 导入druid坐标
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.itheima</groupId><artifactId>spring_09_datasource</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.10.RELEASE</version></dependency><!--这里导入druid坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency></dependencies></project>
  • 配置数据源对象作为Spring管理的bean
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--管理DruidDataSource对象--><!--起id 设置class地址--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--配置基本信息--><property name="driverClassName" value="https://www.huyubaike.com/biancheng/com.mysql.jdbc.Driver"/><property name="url" value="https://www.huyubaike.com/biancheng/jdbc:mysql://localhost:3306/spring_db"/><property name="username" value="https://www.huyubaike.com/biancheng/root"/><property name="password" value="https://www.huyubaike.com/biancheng/123456"/></bean></beans>案例:加载properties文件这个案例我们将会介绍如何加载properties文件 , 并将文件带入到property基本信息中
我们大致将步骤分为以下三步:
  • 开辟context命名空间:
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--上述beans中的内容是我们的命名空间开辟过程在原本的xml中只有:xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">在下面的内容中我们添加:xmlns:context="http://www.springframework.org/schema/context"并在xsi:schemaLocation中添加:http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd(整体从上述内容复制 , 然后修改末尾xsi即可)--></beans>
  • 使用context命名空间 , 加载指定properties文件
<!--2.使用context空间加载properties文件--><context:property-placeholder location="jdbc.properties"/>
  • 使用${}读取加载的属性值
<!--3.使用属性占位符${}读取properties文件中的属性--><!--说明:idea自动识别${}加载的属性值 , 需要手工点击才可以查阅原始书写格式--><bean class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="https://www.huyubaike.com/biancheng/${jdbc.driver}"/><property name="url" value="https://www.huyubaike.com/biancheng/${jdbc.url}"/><property name="username" value="https://www.huyubaike.com/biancheng/${jdbc.username}"/><property name="password" value="https://www.huyubaike.com/biancheng/${jdbc.password}"/></bean>

经验总结扩展阅读