要做的 认识 的spring的是 :
====== 进行 xml 或 全注解 的通用 的 实验 =========
小需求 : 实现账户的 CRUD 操作 ========
技术要求
使用 全注解 开发
使用 spring 的 IoC 实现对象的管理
使用 DBAssit 作为持久层解决方案 使用 c3p0 数据源
回顾 在前一篇里 我们已经对于spring 的 的基本面的 认识 :
spring 框架所拥有的生态 : 有 7个 :ioc aop 事务 测试 集成 模板 源码学习
提供了 后端3层 的 解决方案 :
1: 表现层(控制层) springMVC
2: 持久层 SpringDate /或者还有封装jdbc 操作的模板
3: 与spring 的业务层
最新的版本迭代 是Spring5
spring 的2个core :IOC 与 AOP
一个重要作用 : 整合 其他的开源框架
他的核心 ioc 所实现的原理 :
依赖反转 将创建 对象 托付 给spring进行管理 交给spring 来做,----------
让 spring 给代理了 --------- 而 spring是的ioc 的实现方式 也有 他的 代理方式--------
动态代理 -------动态代理 2种 的实现:java jdk 基于接口 与 cglib 基于 父类 与 子实现类 ---------
都是要 基于反射 ---------- 而反射:需要java编写程序的代码文件-------
3个获取 ------加载类加载器 。。。。。
注解+ xml 配置 : Spring 开发的时候
第三方jar 包 用的是xml 实际中 纯 xml 配置 已经很少在用
基于xml 的注解 : 作用 于 crud 的增删改查 :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 加载数据库 配置文件 -->
<context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder>
<!-- 包扫描 -->
<context:component-scan base-package="com.fhw"></context:component-scan>
<!-- 配置 sercice -->
<bean id="accountServiceI" class="com.fhw.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="dao" ref="accountDao"></property>
</bean>
<!-- 配置 dao -->
<bean id="accountDao" class="com.fhw.dao.impl.AccountImpl">
<!-- 注入 QueryRunner -->
<property name="query" ref="queryRunner"></property>
</bean>
<!-- 配置 QueryRunner -->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!--<bean id="" class=""></bean>-->
<!--配置数据源的 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
3 同样可以 完成 CRUD 的操作 :
.1 需求和技术要求
1.1.1 需求
实现账户的 CRUD 操作
1.1.2 技术要求
使用 spring 的 IoC 实现对象的管理 使用 DBAssit 作为持久层解决方案 使用 c3p0 数据源
测试
import com.fhw.pojo.Account;
import com.fhw.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class AccountTest {
@Test
public void testFindAll(){
// 获取 容器
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//
IAccountService accountService = ac.getBean("accountServiceI", IAccountService.class);
List<Account> allAccount = accountService.findAllAccount();
for (Account account : allAccount) {
System.out.println(account);
}
}
在注入的 时候 写 的业务类 常用 是 set 的
public class AccountImpl implements IAccountDao {
private QueryRunner query;//用apache 的工具类 来进行
public void setQuery(QueryRunner query) {
this.query = query;
}
public class AccountServiceImpl implements IAccountService {
private IAccountDao dao =new AccountImpl();
public void setDao(IAccountDao dao) {
this.dao = dao;
}
测试图 =======
html 做表现 的 xml 做配置
后面的注解开发就会 : 常用 @Autowired 就不用set 了
纯注解的开发的 在springBoot 里比较常用 或 自定义 的类
数据源 的
都要实现的额接口 都是 实现 DataSource 接口 都是 使用getConnection 对象
c3p0 dirud
dbcp :Tomcat 内置的
c3p0 的使用 :
关闭数据库 归还的 到数据库连接池POOLED 里
c3p0,Druid 的 spring 集成 的使用 的 :
的
基于注解的 IOC 配置 :
都是减低程序之间的耦合 : 使用的是依赖反转的思想 :
入门的操作 :
3个注解 :
@Compont :只加这个注解 : Bean 的id 默认 是该类名称的 首字母小写
---- 使用 value 属性 来指定Bean的id 名称 就是你的自定义
使用位置 : 实现类的作用 : 让spring 反射 创建该类的实
@Autowired +@Qualifier (id)
在@ @Compont 是如何 然Spring 的识别的 就要
要告知 spring 在那些包 及子包 下