Spring配置管理第三方Bean
在Spring中,容器除了要配置我们自己写的类外,有时还需要配置其它jar包中的类,下面通过配置数据**Druid(德鲁伊)和C3P0来演示
1. 环境准备
-
在Maven项目中的Pom.xml中添加Spring和Mysql的配置文件
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency>
-
在resources下创建 **applicationContext.xml ** 文件
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
-
编写一个运行类App
public class App{ public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); } }
2. 实现Druid配置
-
在Pom.xml中添加Druid依赖坐标
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency>
-
在applicationContex.xml中配置Bean
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property> <property name="username" value="root"></property> <property name="password" value="1234"></property> </bean>
说明:
- driverClassName:数据库驱动
- url:数据库连接地址
- username:数据库连接用户名
- password:数据库连接密码
- 数据库连接的四要素要和自己使用的数据库信息一致。
-
在App类中调用上下文创建对象并使用:这里直接打印
public class App { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = (DataSource) app.getBean("dataSource"); System.out.println(dataSource); } }
程序运行效果:
-
通过上面案例可以知道:第三方类为Druid数据源,通过setter来进行依赖注入。注入的内容要根据自己的数据库信息来。
3. 实现C3P0
- 在Pom.xml中添加C3P0依赖坐标
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
-
在applicationContex.xml中配置Bean
<!-- 配置第三方Bean C3p0--> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="dataSourceName" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean>
这里要注意setter注入的name名称与Druid稍有不同
-
在App类中调用上下文创建对象并使用:这里直接打印
public class App { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); DataSource dataSource = (DataSource) app.getBean("dataSource2"); System.out.println(dataSource); } }
程序运行效果:
4. 通过加载properties文件优化上面案例代码
上面案例中数据好库连接的相关信息都是直接写在Spring的配置文件中,具有高耦合,不利于后期代码的维护。因此可以将数据库连接信息放在一个properties文件中,再通过Spring将Properties配置到容器内。
-
在resources中创建一个Properties文件(文件名任意取)在内部编辑好数据库连接信息根据直接数据库信息来
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis jdbc.username=root jdbc.password=1234
-
在applicationContext.xml中添加context命名空间
<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命名空间只需要复制之前的
xmlns="http://www.springframework.org/schema/beans" 再将其改为: xmlns:context="http://www.springframework.org/schema/context" 然后在xsi:schemaLocation再复制一边里面的内容,再将所有的beans改为context就行。
-
加载Properties文件
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
-
再将之前注入的数据用**${}**来代替
<!-- 配置第三方Bean druid数据源--> <bean id="dataSource3" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
最后分享一个网站,可以在里面找到不知道具体坐标的依赖jar包。Maven仓库