天天看点

Spring复习 day(2)IOC以及相关注解一、依赖注入二、IOC有关的注解三、其他注解四、Spring整合junit

文章目录

  • 一、依赖注入
    • 1.1 概念
    • 1.2 能注入的数据类型
    • 1.3 注入的方式
    • 1.4 构造函数注入
    • 1.5 set方式注入
    • 1.6 复杂数据类型的注入
    • 1.7 依赖注入的优点和缺点
  • 二、IOC有关的注解
    • 2.1 用于创建对象的
    • 2.2 spring使用注解
    • 2.3 用于注入数据的
    • 2.4 用于改变范围的
  • 三、其他注解
    • 3.1 @Configuration
    • 3.2 @ComponentScan
    • 3.3 @ Bean
    • 3.4 @import
    • 3.5 @PropertySource
  • 四、Spring整合junit
    • 4.1 问题
    • 4.2 解决方法
    • 4.3 注意
    • 4.5 对某一个类进行测试

一、依赖注入

1.1 概念

IOC的作用是降低程序间的耦合性,依赖关系的管理交由spring去维护。加入我们在当前类需要其他的类,只需在配置文件中进行相关的配置即可。

1.2 能注入的数据类型

  • 基本数据类型和string
  • 其他bean类型
  • 集合类型/复杂类型

1.3 注入的方式

  • 构造函数提供
  • set方法注入
  • 使用注解

1.4 构造函数注入

  • 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-3.0.xsd">
    <bean id="stuDao" class="com.example.demo.Service.stuDao">
        <constructor-arg name="name" value="zhangsan"></constructor-arg>
        <constructor-arg name="age" value="12"></constructor-arg>
        <constructor-arg name="birth" ref="birth"></constructor-arg>
    </bean>
    <bean id="birth" class="java.util.Date"></bean>
</beans>
           
  • test类
public class Controller {
    //取到Spring的核心ioc容器,根据ID获取对象
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        stuDao stuDao = (stuDao) applicationContext.getBean("stuDao");
        System.out.println(stuDao.getAge() + " " + stuDao.getBirth() + stuDao.getName());
    }
}
           
  • 结果
12 Thu Jan 23 18:44:50 CST 2020zhangsan
           
  • ref: 用于指定其他的bean类型,即在spring ioc容器中出现的bean对象
  • value: 用于提供基本类型和string类型的数据
  • index: 指定是第几个参数
  • name: 参数的名称
  • type: 参数的类型

1.5 set方式注入

  • 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-3.0.xsd">
    <bean id="stuDao" class="com.example.demo.Service.stuDao">
        <property name="age" value="12"></property>
        <property name="name" value="jack"></property>
        <property name="birth" ref="birth"></property>
    </bean>
    <bean id="birth" class="java.util.Date"></bean>
</beans>
           
  • test类
public class Controller {
    //取到Spring的核心ioc容器,根据ID获取对象
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        stuDao stuDao = (stuDao) applicationContext.getBean("stuDao");
        System.out.println(stuDao.getAge() + " " + stuDao.getBirth() + " " + stuDao.getName());
    }
}
           
  • 结果
12 Thu Jan 23 19:00:24 CST 2020 jack
           
需要一个默认的构造函数

1.6 复杂数据类型的注入

  • 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-3.0.xsd">
    <bean id="stuDao" class="com.example.demo.Service.stuDao">
        <property name="list">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
                <value>ddd</value>
            </array>
        </property>
    </bean>
    <bean id="birth" class="java.util.Date"></bean>
</beans>
           
  • 给list结构集合注入的标签:

    list,array,set

  • 给map结构集合注入的标签:

    map,props

1.7 依赖注入的优点和缺点

使用依赖注入的方式创建对象,必须对相应的属性进行赋值。就算不使用这些属性,也必须提供。

二、IOC有关的注解

2.1 用于创建对象的

  • @Component,@Controller(表现层),@Service(业务层) ,@Repsitory(持久层)

    把当前类对象存入spring容器中

2.2 spring使用注解

  • 首先在xml中配置context相关内容,添加需要扫描的包
<?xml version="1.0" encoding="UTF-8"?>

<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">
    <bean id="stuDao" class="com.example.demo.Service.stuDao">
        <property name="list">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
                <value>ddd</value>
            </array>
        </property>
    </bean>
    <bean id="birth" class="java.util.Date"></bean>
    <context:component-scan base-package="com.example.demo.Service"></context:component-scan>
</beans>
           
  • 在需要添加进容器的类上面加@Component注解
@Component
public class stuDao {
    private List list;
    private Map map;

    public stuDao(List list, Map map) {
        this.list = list;
        this.map = map;
    }

    public stuDao() {
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }
}

           
  • 测试
public class Controller {
    //取到Spring的核心ioc容器,根据ID获取对象
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        stuDao stuDao = (stuDao) applicationContext.getBean("stuDao");
        System.out.println(stuDao.getList().toString());
    }
}

           

2.3 用于注入数据的

  • @Autowired

    只要容器中存在唯一的对象和需要注入的对象是同一类型的,就可以注入成功。如果有多个类型一致的对象时,这时候就需要去对比对象的名称,如果有一致的,则注入成功。

    可以独立的用在字段或者方法上。

  • @Qualifier

    按照类型注入的基础上再按照对象名称注入。不能单独使用,需要与@Autowired配合使用。

    value:用于指定注入对象的ID

  • @Resource

    直接按照bean的id注入,可以单独使用。

    name:用于指定bean的ID

  • 基本类型和string类型不能通过上述注解实现
  • 集合类型只能通过xml注入
  • @value

    用于注入基本类型和string类型

2.4 用于改变范围的

  • @scope

    用于指定bean的作用范围

    value:singleton,prototype

  • @PreDestroy

    指定销毁的方法

  • @PostConstruct

    指定初始化方法PostCon

三、其他注解

3.1 @Configuration

指定当前类是一个配置类

3.2 @ComponentScan

指定spring容器创建时要扫描的包

3.3 @ Bean

创建对象并且将对象存入spring容器中。

name:用于指定创建对象的id,默认id是方法名称。

3.4 @import

导入配置类

value:指定其他配置类的字节码

有inport注解的类是主配置类

3.5 @PropertySource

指定properties文件的名称和路径,关键字:classpath

四、Spring整合junit

4.1 问题

junit单元测试无IOC容器,@Autowired注解是不起作用的。

4.2 解决方法

  • 倒入spring整合junit的jar包。
<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
           
  • 使用junit提供的注解替换原有的main方法。替换成spring提供的。
@Runwith(SpringJunit4ClassRunner.class)
  • 告知spring容器,ioc容器的创建是基于xml还是基于注解的,并且说明位置

@ContextConfiguration(xxx.class)

locations:指定xml文件所在的位置

classes:指定注解类所在的位置

  • 这样就可以使用@Autowired

演示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Stuservice.class)
class stuServiceTest {
    @Autowired
    Stuservice stuservice;
    @Test
    void getCount() {
        stuservice.insertById(new Student(1500,1600,2));
    }
}s
           

4.3 注意

spring5.x版本时对应的junit版本是4.12以上。\

4.5 对某一个类进行测试

  • 在要测试的类上面ctrl + shift + t
  • 选中要测试的方法,进行测试。