✨druid
✨导入druid坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
✨配置数据源对象作为Spring管理的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/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
✨c3p0
✨导入c3p0坐标
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
✨配置数据源对象
<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
✨加载properties
首先在resource资源包下创建jdbc.properties
jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root
✨开启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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
✨使用context命名空间,加载指定的properties文件
<context:property-placeholder location="jdbc.properties"></context:property-placeholder>
✨使用${}读取加载的属性值
<bean id="datasource" 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>
✨注意
- 不加载系统属性
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
- 加载多个properties文件
<context:property-placeholder location="jdbc.properties,msg.properties"/>
- 加载所有properties文件
<context :property-placeholder location="*.properties" />
- 加载properties文件标准格式
<context:property-placeholder location="classpath:*.properties" />·
- 从类路径或jar包中搜索并加载properties文件
<context: property-placeholder location="classpath*: *. properties"/>
✨容器总结
✨Bean相关
<bean
//bean的Id
id="bookDao"
//bean别名
name="dao bookDaoImpl daoImpl"
//bean类型,静态工厂类,FactoryBean类
class="com.jkj.dao.imp1.BookDaoImpl"
//控制bean的实例数量
scope="singleton"
//生命周期初始化方法
init-method="init"
//生命周期销毁方法
destroy-method="destory"
//自动装配类型
autowire="byType"
//bean工厂方法,应用于静态工厂或实例工厂
factory-method="getInstance"
//实例工厂bean
factory-bean=" com.jkj.factory .BookDaoFactory"
//控制bean延迟加载
lazy-init="true"
/>
✨依赖注入
<bean id="bookService" class="com.jkj.service.impl.BookServiceImpl">
//构造器注入引用类型
<constructor-arg name="bookDao" ref="bookDao" / >
<constructor-arg name="userDao" ref="userDao" />
//构造器注入简单类型
<constructor-arg name="msg" value="WARN" />
//类型匹配与索引匹配
<constructor-arg type="java.lang,String" index="3" value="WARN" />
//setter注入引用类型
<property name="bookDao" ref="bookDao" / >
<property name="userDao" ref="userDao" / >
//setter注入简单类型
<property name="msg" value="WARN" />
//setter注入集合类型
<property name="names">
//list集合
<list>
//集合注入简单类型
<value>itcast</value>
//集合注入引用类型
<ref bean="dataSource" />
</list>
</ property>
</bean>