SpringIOC 原理详解与及注解配置 整合Junit

SpringIOC

​ 控制反转(IoC)原理的Spring框架实现。IoC也称为依赖注入(DI)。在此过程中,对象仅通过构造函数参数,工厂方法的参数或在构造或从工厂方法返回后在对象实例上设置的属性来定义其依赖项(即,与它们一起使用的其他对象) 。然后,容器在创建bean时注入那些依赖项。此过程从根本上讲是通过使用类的直接构造或诸如服务定位器模式之类的控件来控制其依赖项的实例化或位置的bean本身的逆过程(因此称为Control Inversion)。

在这里插入图片描述

SpingIOC容器:
容器所为SpringIOC的核心它主要有两种:

BeanFactory:BeanFactory为IOC容器提供了基础功能,Spring文档中提到,当前该类仅仅是为了向后兼容老的版本,除非你有更好的原因否则就应该使用第二种容器。

ApplicationContext:通过API文档可以知道,ApplicationContext是BeanFactory的子接口,并且从文档中也可以看到ApplicaionContext除了包含有BeanFactory的所有功能还支持了更多的功能。

ApplicationContext的实现有四种方式:

FileSystemXmlApplicationContext:加载配置文件的时候采用的是项目的路径。

ClassPathXmlApplicationContext:加载配置文件的时候根据ClassPath位置。

XmlWebApplicationContext:在Web环境下初始化监听器的时候会加载该类。

AnnotationConfigApplicationContext:根据注解的方式启动Spring 容器。

所以可以通过bean构建所有类对象,实现工厂模式解耦。

案例:

  1. 首先创建一个maven项目。
  2. 在pom.xml导入包
 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
 </dependencies>

3.创建一个Account类

package com.ppl.domain.entity;

public class Account implements Serializable {
    private String name;
    private Double money;

    public Account() {
    }

    public Account(String name, Double money) {
        this.name = name;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }
}

  1. 在resources下创建一个bean.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">

    <bean id="account" class="com.ppl.domain.entity.Account"></bean>
    
</beans>
  1. 构建好了,现在创建一个test类,实现反射创建类对象
public static void main(String[] args) {
	//SpingIOC容器
    ApplicationContext ac =new ClassPathXmlApplicationContext("bean.xml");
    System.out.println(ac);
	//容器中获取emp的类对象
    Emp emp = ac.getBean("account", Account.class);
	//默认单例对象所以输出的对象结构一样。
    for (int i = 0; i <10 ; i++) {
        Account account = ac.getBean("account", Account.class);
        System.out.println(account);
    }
}
使用注解实现IOC

需要在配置文件中扫描,并识别出来,扫描有注解的bean,然后直接生成对象,存入仓库。

bean.xml需要增加扫描空间:

<!-- 配置spring创建容器时要扫描的包-->
  <context:component-scan base-package="com.ppl"></context:component-scan>

常用注解:@Component(零件) @Autowired(自动连线)

Bean标注注解
注解在类上
@Component(“name”)等于@Component(value=“user”)
@Component相当于@Component(“className”)
解释
@Component组件通用注解,常用于Model类
@Controller常用于对Controller实现类进行标注
@Service常用于对Service实现类进行标注
@Repository常用于对DAO实现类进行标注

Bean属性注入注解

注解在类属性上解释
@Value注入普通类型属性 给属性赋初始值
@Resource注入对象类型
@Autowired注入对象类型,默认按照类型注入。结合@Qualifier注解完成按名称的注入。通过set方法注入。所以需要生成
Bean的作用范围注解@Scope 默认单例
注解在类上:单例或多例 作用域解释
@Scope值1:singleton 单例
值2:protiotype 多例
值3:作用域

作用域:

  • request : request域,需要在web环境
  • session : session域,需要在web环境
  • application: context域,需要在web环境
  • globalsession: 集群环境的session域,需要在web环境
Bean的生命周期注解
注解在类中的方法上解释
@PostConstruct相当于init-method 创建时调用,初始方法
@PreDestroy相当于destroy-method 销毁时调用,销毁方法
@PostConstruct
public void init() {
	System.out.println("初始化方法......");
}

@PreDestroy
public void destroy() {
	System.out.println("销毁方法......");
}
整合Junit

Spring整合junit的配置

1、导入spring整合junit的jar(坐标)

2、使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的

​ @Runwith 告知运行环境,初始化一些配置

3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置

  @ContextConfiguration

​ locations:指定xml文件的位置,加上classpath关键字,表示在类路径下

​ classes:指定注解类所在地位置

当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上

第一步:pom.xml

 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
</dependency>

第二步:test/java/TestConn.java

//整合Junit test测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class TestAC {
    //bean中的配置,连接数据库的runner
    @Autowired
    private QueryRunner runner = null;

    @Test
    public void test01() throws SQLException {
        List<Account> query = runner.query("Select * from account", new BeanListHandler<>(Account.class));
        query.forEach(System.out::println);
    }

}

<bean id=“runner” …>

<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
    <!--注入数据源-->
    <constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>

<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--连接数据库的必备信息-->
    <property name="driverClass" value="${driver}"></property>
    <property name="jdbcUrl" value="${url}"></property>
    <property name="user" value="${user}"></property>
    <property name="password" value="${password}"></property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值