IOC/DI配置管理第三方bean
很多时候我们会需要用到第三方jar包中的类,此时我们需要管理这些类,本次案例将使用数据源druid(DataSource)来完成配置学习。
需求分析:使用Spring的IOC容器来管理Druid连接池对象
简单代码实现
- 在pom.xml中添加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>org.example</groupId>
<artifactId>itheima_spring_ioc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!--spring-context为spring开发的基本包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--数据库连接池 德鲁伊-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
</project>
- 在spring配置文件中将数据源制作成一个bean,让IOC容器进行管理。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///mybatis"/>
<property name="username" value="root"/>
<property name="password" value="qq12345652"/>
</bean>
- 从IOC容器中获取对应的bean对象,将其打印到控制台查看结果。
public class App {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource)applicationContext.getBean("dataSource");
System.out.println(dataSource);
}
}
加载properties文件
在上面的代码中,我们把数据库连接的四要素直接写在了Spring的配置文件中,这样不利于后期维护。更好的方式是我们将四要素写在jdbc.properties中,然后由spring的配置文件来读取。
- 在resources下创建jdbc.properties
- 将数据库连接四要素配置到jdbc.properties中
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=root
- 在Spring的配置文件中加载properties文件
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="NEVER"/>
- 使用加载到的值实现属性注入
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<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>
学习总结
使用IOC/DI配置管理第三方bean最终版本步骤如下:
- 创建maven项目
- 在pom.xml中添加spring-context、druid依赖
- 创建jdbc.properties,并在其中配置数据库连接四要素
- 在spring的配置文件中创建新的命名空间context,读取外部properties
- 在spring的配置文件中添加关于DataSource的bean,并使用${}为其属性赋值
- 从容器中获取对象并调用其方法