天天看點

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
  • 選中要測試的方法,進行測試。