Spring 第二篇 注解+ xml 配置 与 纯注解开发 与集成 junit测试

本文详细介绍了如何在Spring框架中使用全注解开发进行账户的CRUD操作,包括使用IoC、DBAssit和c3p0数据源。探讨了Spring的核心——IOC的实现原理,并对比了注解+XML配置和纯注解开发。文章通过案例展示了Spring的注解如@Component、@Controller、@Service和@Repository的使用,以及@Autowired和@Qualifier等注解在对象注入中的应用。此外,还涉及了Spring与JUnit的集成,以及@Configuration、@Import等配置类注解的使用。
摘要由CSDN通过智能技术生成

要做的 认识 的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 在那些包 及子包 下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值