成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

長文干貨丨一文搞懂IoC的依賴注入

開發 前端
本篇給大家介紹IoC的依賴注入,篇幅較長,希望對你有所幫助。

[[377607]]

 一、注解驅動IoC

xml驅動的IoC容器使用的是ClassPathXmlApplicationContext讀取xml內bean信息

注解驅動的IoC容器使用的是AnnotationConfigApplicationContext讀取Java類中的bean信息

1. AnnotationConfigApplicationContext 的注冊使用

相比于xml文件作為驅動, 注解驅動需要指明配置類 一個配置類可以理解為"相當于"一個xml 配置類只需要在類上標注注解 @Configuration

  1. @Configuration 
  2. public class DemoConfiguration { 

在xml中聲明bean的方式

在配置類中使用的是@Bean注解

  1. <bean id="person" class="com.huodd.bean.Person"></bean> 

說明: 向IoC容器注冊一個類型為Persion,id為Person的Bean

方法名表示的是bean的id 返回值表示的是注冊的bean的類型

@Bean注解也可以顯示的聲明bean的id 如 @Bean("person1")

  1. @Bean 
  2. public Person person() { 
  3.     return new Person(); 

2. 注解IoC容器的初始化

  1. public class AnnotationConfigApplication { 
  2.     public static void main(String[] args) { 
  3.         ApplicationContext ctx = new AnnotationConfigApplicationContext(DemoConfiguration.class); 
  4.         Person person = ctx.getBean(Person.class); 
  5.         System.out.println(person); 
  6.     } 

運行后Person控制臺打印結果

  1. com.huodd.bean.Person@55536d9e 

3. 組件的注冊和掃描

上述初始化時 我們在使用AnnotationConfigApplicationContext時傳遞了參數 Class... componentClasses

翻看AnnotationConfigApplicationContext的構造方法可以發現還可以傳遞參數的參數類型還有 String... basePackages

這里就涉及到組件的注冊和掃描

  • 這里可以思考一個問題, 如果我們要注冊的組件特別多, 那進行編寫這些@Bean的時候代碼工作量也會特別多,這時候該如何解決呢?

Spring 給我們提供了幾個注解,可以幫助我們快速注冊需要的組件, 這些注解被稱為模式注解(stereotype annotations)

@Component

@Component可以說是所有組件注冊的根源 在類上標注 @Component 代表該類被注冊到IoC容器中作為一個Bean

  1. @Component 
  2. public class Person { 

如果未指定 Bean 的名稱 默認規則是 "類名稱首字母小寫" 上面的bean名稱默認會是 person

如果要自定義bean的名稱 可以在@Component聲明value的值即可 如

  1. @Component("person1"
  2. public class Person { 

在xml中相當于

  1. <bean id="person1" class="com.huodd.bean.Person"/> 

@ComponentScan

這個時候 如果我們直接運行啟動類 獲取Person的bean對象,會報錯NoSuchBeanDefinitionException 這是為什么呢?

因為我們只是聲明了組件,而后直接啟動了IoC容器,這樣容器是感知不到有@Component存在的,

解決方案1:

我們需要在寫配置類時再額外標注一個新的注解@ComponentScan

目的是告訴IoC容器 我要掃描哪個包下面的帶有@Component注解的類

  1. @Configuration 
  2. @ComponentScan("com.huodd.bean"
  3. public class DemoComponentScanConfiguration { 

: 如果不指定掃描路徑, 則默認掃描本類所在包及所有子包下帶有@Component的組件

啟動類代碼如下:

  1. public class AnnotationConfigApplication { 
  2.     public static void main(String[] args) { 
  3.         ApplicationContext ctx = new AnnotationConfigApplicationContext(DemoComponentScanConfiguration.class); 
  4.         Person person = ctx.getBean(Person.class); 
  5.         System.out.println(person); 
  6.     } 

解決方案2:

這里也可以不寫@ComponentScan 而直接在AnnotationConfigApplicationContext方法參數內傳入String類型的包掃描路徑 代碼如下

  1. public class AnnotationConfigApplication { 
  2.     public static void main(String[] args) { 
  3.         ApplicationContext ctx = new AnnotationConfigApplicationContext("com.huodd.bean"); 
  4.         Person person = ctx.getBean(Person.class); 
  5.         System.out.println(person); 
  6.     } 

PS: 組件掃描并非是注解驅動IoC所特有的, 其實在xml驅動的IoC模式下 同樣可以啟用組件掃描, 只需要在xml中聲明一個標簽即可

  1. <context:component-scan base-package="com.huodd.bean"/> 

這里需要注意下: 如需要掃描多個路徑,需要寫多個標簽 也就是 一個標簽只能聲明一個根包

組件注冊的補充

SpringFramework 提供了在進行Web開發三層架構時的擴展注解: 分別為 @Controller、 @Service 、@Repository 小伙伴有沒有很熟悉?

分別代表 表現層、業務層、持久層 這三個注解的作用與 @Component完全一樣 扒開源碼我們可以看到 底層在這三個注解類上又添加了 @Component

  1. @Target({ElementType.TYPE}) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Documented 
  4. @Component 
  5. public @interface Service { 

這樣 我們在進行符合三層架構的開發時 對于相應的如 ServiceImpl等 就可以直接標注 @Service 等注解了

@Configuration

@Configuration 底層也有標注@Component

  1. @Target({ElementType.TYPE}) 
  2. @Retention(RetentionPolicy.RUNTIME) 
  3. @Documented 
  4. @Component 
  5. public @interface Configuration { ... } 

由此可以說明,配置類不是向我們所想的那樣,只是單純的做一個配置而已, 它也會被視為 bean,也被注冊到IoC容器里面

4. 注解驅動與xml驅動互相引用

4.1 xml引用注解

需開啟注解配置 再注冊相應配置類

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xmlns:context="http://www.springframework.org/schema/context" 
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  6.         https://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context  
  8.         https://www.springframework.org/schema/context/spring-context.xsd"> 
  9.  
  10.     <!-- 開啟注解配置 --> 
  11.     <context:annotation-config /> 
  12.     <!-- 注冊配置類 --> 
  13.     <bean class="com.huodd.config.AnnotationConfigConfiguration"/> 
  14. </beans> 

4.2 注解引用XMl

  1. @Configuration 
  2. @ImportResource("classpath:annotation/demo-beans.xml"
  3. public class ImportXmlAnnotationConfiguration {  

 二、IoC的依賴注入

1.Setter屬性注入

創建對象 將屬性值set進去 之后返回對象

  1. @Bean 
  2. public Person person() { 
  3.     Person person = new Person(); 
  4.     person.setId(1); 
  5.     person.setName("PoXing"); 
  6.     person.setAge(18); 
  7.     return person; 

xml中的setter注入

  1. <bean id="person" class="com.huodd.bean.Person"
  2.     <property name="id" value="1"/> 
  3.     <property name="name" value="PoXing"/> 
  4.     <property name="age" value="18"/> 
  5. </bean> 

2. 構造器注入

使用構造器注入,需要在bean本身添加有參構造方法, 如在Person中添加有參構造方法如下

  1. public Person(Integer id, String nameInteger age) { 
  2.         this.id = id; 
  3.         this.name = name
  4.         this.age = age; 

注解驅動中,我們創建bean的時候注入屬性時 就需要同時指定參數值

  1. @Bean 
  2. public Person person() { 
  3.     return new Person(1, "PoXing", 18); 

xml驅動中如下

  1. <bean id="person" class="com.huodd.bean.Person"
  2.     <!--  
  3.   index: 表示構造器的參數索引 
  4.   value: 表示對應的參數值 
  5.  --> 
  6.     <constructor-arg index="0" value="1"/> 
  7.     <constructor-arg index="1" value="PoXing"/> 
  8.     <constructor-arg index="2" value="18"/> 
  9. </bean> 

3. 注解式屬性注入

這里先說明一下,為何會有注解式屬性值注入. 細心的小伙伴可能會發現 上面我們談到的 Setter屬性注入、構造器注入 好像在只能是在使用 @Bean注解的時候時候使用, 但是 如果是通過標注 @Component注解的組件呢(像前面我們的Person類中標注了@Component注解),怎么給它設定屬性值, 該節主要就是說一下這部分

@Component 下的屬性注入

這里我們使用Dog類做為演示(這里我悄悄的添加了@Component注解 自己嘗試的小伙伴要注意哦 否則會報錯的)

  1. @Component 
  2. public class Dog { 
  3.     private Integer id; 
  4.     private String name
  5.     private Integer age; 
  6.  
  7.    ... 省略 Getter、Setter 
  8.    ... 省略 toString 
  9.  

這里要實現注解式屬性注入,可以直接在要注入的字段上標注 @Value注解 如

  1. @Value("1"
  2. private Integer id; 
  3.  
  4. @Value("wangcai"
  5. private String name
  6.  
  7. @Value("3"
  8. private Integer age; 

啟動類代碼如下

  1. public class DiApplication { 
  2.     public static void main(String[] args) { 
  3.         ApplicationContext ctx = new AnnotationConfigApplicationContext("com.huodd.bean"); 
  4.         Dog dog = ctx.getBean(Dog.class); 
  5.         System.out.println(dog); 
  6.     } 

控制臺打印結果

  1. Dog{id=1, name='wangcai', age=3} 

外部配置文件(@PropertySource)

這里主要是解決上面的@Value中注入 我們把屬性值直接固定寫死了,如果要修改 還要去Java代碼中去修改,很不符合開發規范,

SpringFramework為我們擴展了新的注解@PropertySource 主要用來導入外部配置文件

1.這里我們創建一個 dog.properties

  1. dog.id=1 
  2. dog.name=wangcai 
  3. dog.age=3 

2.引入配置文件

  1. @PropertySource("classpath:di/dog.properties"
  2. @ComponentScan("com.huodd.bean"
  3. @Configuration 
  4. public class DemoComponentScanConfiguration { 

3.Dog類中屬性注入 這里@Value需要配合占位符 來獲取properties配置文件中的內容

  1. @Value("${dog.id}"
  2. private Integer id; 
  3.  
  4. @Value("${dog.name}"
  5. private String name
  6.  
  7. @Value("${dog.age}"
  8. private Integer age; 

4.修改一下啟動類

  1. public class DiApplication { 
  2.     public static void main(String[] args) { 
  3.         ApplicationContext ctx = new AnnotationConfigApplicationContext(DemoComponentScanConfiguration.class); 
  4.         Dog dog = ctx.getBean(Dog.class); 
  5.         System.out.println(dog); 
  6.     } 

控制臺打印結果如下

  1. Dog{id=1, name='wangcai', age=3} 

此時配置文件的屬性已經注入成功

4.自動注入

在xml模式中有ref屬性 可以將一個bean注入到另外一個bean中, 注解模式中也同樣可以

@Autowired

給Dog的bean中注入 Person的Bean (即 給dog指定它的主人)

方法1 → 在屬性上標注

  1. @Component 
  2. public class Dog { 
  3.     // ...... 
  4.     @Autowired 
  5.     private Person person; 

方法2 → 使用構造器注入方式

  1. @Component 
  2. public class Dog { 
  3.     // ...... 
  4.     private Person person; 
  5.  
  6.     @Autowired 
  7.     public Dog(Person person) { 
  8.      this.person = person; 
  9.     } 

方法3 → 使用setter方法注入

  1. @Component 
  2. public class Dog { 
  3.     // ...... 
  4.     private Person person; 
  5.      
  6.     @Autowired 
  7.     public void setPerson(Person person) { 
  8.         this.person = person; 
  9.     } 

JSR250規范下的@Resource

@Resource也是用來屬性注入的注解

它與@Autowired的區別是:

  • @Autowired是按照類型注入
  • @Resource是按照屬性名(也就是bean的名稱)注入

@Resource 注解相當于標注 @Autowired @Qualifier

@Qualifier這里簡要說明下,為指定bean的名稱而存在,如果存在多個相同的bean,而bean的名稱不同,我們可以使用@Autowired 配置 @Qualifier注解

如: 下面表示該Dog類注入的主人Bean是名稱為 xiaowang的, 而當前容器內可能存在多個 主人bean對象 比如 xiaoli、xiaoming ....

  1. @Component 
  2. public class Dog { 
  3.     // ...... 
  4.     @Autowired 
  5.     @Qualifier("xiaowang"
  6.     private Person person; 

下面如果使用@Resource 可以更方便些 代碼如下

  1. @Component 
  2. public class Dog { 
  3.     // ...... 
  4.     @Resource(name="xiaowang"
  5.     private Person person; 

JSR330規范下的@Inject

@Inject注解也是按照類型注入,與@Autowire的策略一樣, 不過如要使用@Inject 需要額外的導入依賴

  1. <!-- jsr330 --> 
  2. <dependency> 
  3.     <groupId>javax.inject</groupId> 
  4.     <artifactId>javax.inject</artifactId> 
  5.     <version>1</version> 
  6. </dependency> 

后面的使用方法就與SpringFramework 原生的 @Autowire + @Qualifier 相同了

  1. @Component 
  2. public class Dog { 
  3.      
  4.     @Inject // 等同于@Autowired 
  5.     @Named("xiaowang") // 等同于@Qualifier 
  6.     private Person person; 

它與@Autowired的區別是:

  • @Autowired所在的包為 org.springframework.beans.factory.annotation.Autowired 即為 SpringFramework 提供的
  • @Inject所在的包為 javax.inject.Inject 屬于JSR的規范 也就是說如果不使用SpringFramework時可以使用該注解

5. 復雜類型注入

Array注入

  1. <property name="names"
  2.     <array> 
  3.         <value>PoXing</value> 
  4.         <value>LaoWang</value> 
  5.     </array> 
  6. </property> 

List注入

  1. <property name="tels"
  2.     <list> 
  3.         <value>13000000000</value> 
  4.         <value>13000000001</value> 
  5.     </list> 
  6. </property> 

Set注入-

  1. <!-- 已經提前聲明好的Dog --> 
  2. <bean id="wangcai" class="com.huodd.bean.ext.Dog"/> 
  3. --- 
  4.  
  5. <property name="dogs"
  6.     <set
  7.         <bean class="com.huodd.bean.Dog"/> 
  8.         <ref bean="wangcai"/> 
  9.     </set
  10. </property> 

Map注入

  1. <property name="homesMap"
  2.     <map> 
  3.         <entry key="1" value="main"
  4.          <ref bean="myHome1" /> 
  5.         </entry> 
  6.         <entry key="2" value="other"
  7.              <ref bean="myHome2" /> 
  8.         </entry> 
  9.     </map> 
  10. </property> 

Properties注入

  1. <property name="props"
  2.     <props> 
  3.         <prop key="sex">男</prop> 
  4.         <prop key="age">18</prop> 
  5.     </props> 
  6. </property> 

面試題

1.@Autowired注入原理是什么?

  1. 先拿屬性對應的類型,去IoC容器中找相應的Bean
  2. 如果沒有找到 直接拋出NoUniqueBeanDefinitionException異常
  3. 如果找到一個 直接返回
  4. 如果找到多個相同類型的bean 再拿屬性名去與這多個bean的id進行對比
  5. 如果有多個或者沒有 則會拋出NoUniqueBeanDefinitionException異常
  6. 如果只有一個 直接返回

2.依賴注入的方式有哪些,都有什么區別

3.自動注入的注解對比

@Qualifier :如果被標注的成員/方法在根據類型注入時發現有多個相同類型的 Bean ,則會根據該注解聲明的 name 尋找特定的 bean

@Primary :如果有多個相同類型的 Bean 同時注冊到 IOC 容器中,使用 “根據類型注入” 的注解時會注入標注 @Primary 注解的 bean 即默認策略

4.使用依賴注入有什么優缺點

依賴注入作為 IOC 的實現方式之一,目的就是解耦,我們不需要直接去 new 那些依賴的類對象就可以直接從容器中去取來使用, 如果組件存在多級依賴,依賴注入可以將這些依賴的關系簡化。

依賴對象的可配置:通過 xml 或者注解聲明,可以指定和調整組件注入的對象,借助 Java 的多態特性,可以不需要大批量的修改就完成依賴注入的對象替換

 

責任編輯:姜華 來源: PoXing
相關推薦

2024-06-05 11:43:10

2021-10-20 08:49:30

Vuexvue.js狀態管理模式

2022-03-24 08:51:48

Redis互聯網NoSQL

2024-04-12 12:19:08

語言模型AI

2023-09-08 08:20:46

ThreadLoca多線程工具

2021-03-22 10:05:59

netstat命令Linux

2023-09-15 12:00:01

API應用程序接口

2020-05-15 16:37:13

PowerBI數據分析

2023-07-04 08:56:07

指針類型Golang

2023-09-02 21:27:09

2021-03-04 00:09:31

MySQL體系架構

2021-07-08 10:08:03

DvaJS前端Dva

2020-09-03 06:35:44

Linux權限文件

2023-05-22 13:27:17

2021-02-28 20:53:37

Cookie存儲瀏覽器

2024-07-12 14:46:20

2023-03-06 21:29:41

mmap技術操作系統

2020-12-07 06:19:50

監控前端用戶

2022-07-15 08:16:56

Stream函數式編程

2022-05-05 16:47:24

Docker網絡空間容器
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 激情久久av一区av二区av三区 | 久久久亚洲 | 亚洲一区二区视频在线播放 | 国产亚洲人成a在线v网站 | 久久黄视频 | 亚洲第一天堂无码专区 | 精品一区二区三区不卡 | 国产一区二区精品在线观看 | 成年人在线播放 | 色999视频| www.47久久青青 | 亚洲第一视频网 | 国产精品久久久久影院色老大 | 欧美一区在线视频 | 国产综合精品 | 亚洲 欧美 激情 另类 校园 | 国产在视频一区二区三区吞精 | 欧美日韩国产在线观看 | 国产精品一区二区在线 | 2一3sex性hd | 中国一级大黄大片 | 亚洲激精日韩激精欧美精品 | 免费在线精品视频 | 亚洲欧美日韩在线 | 久久91av | 国产精品一区2区 | 99综合网 | 黄色a三级 | www.日本在线 | 中文字幕亚洲一区二区三区 | 国产精品久久久久久婷婷天堂 | 91免费电影 | 美女视频三区 | 欧美激情一区二区 | 亚洲五码久久 | 成人精品视频99在线观看免费 | 四虎永久在线精品免费一区二 | 久久国产精品视频 | 久久精品网 | 亚洲美女一区 | 国产色片 |