Spring依賴注入的三種方式小結(jié)
Spring的主要特性包括IOC和DI,其中DI是IOC的基礎。在以前的Spring使用過程中大部分都是使用XML配置文件顯式配置spring組件,導致大量的XML配置文件以及冗余的XML配置代碼。閱讀《Spring in Action》后總結(jié)Spring的DI功能的三種主要裝配方式以及混合裝配方式
根據(jù)注解自動裝配
Spring中有非常豐富的注解,通過這些注解可以方便地配置Spring容器,使得Spring容器可以自動識別相關Bean并做自動注入裝配。
使用注解
- @Component注解:標注一個類,標識此類為一個Java Bean。
- @Configuration注解:標注一個類,標識此類為一個Java配置類
- @ComponentScan注解:標注一個類,用以配置掃描的包名稱
示例代碼
這里使用一個最簡單的例子來展示自動裝配的配置,代碼結(jié)構(gòu)如圖所示。
ActionConfig類用于做全局配置,我們知道,一名騎士一般都會有一匹馬,所以這里Horse和Knight類作為Java Bean被Spring容器創(chuàng)建,并將Horse類的一個bean注入到Knight中。所以在Horse和Knight中必須使用@Component注解進行修飾。
Knight代碼:
package entities; import org.springframework.stereotype.Component; /** * Created by Administrator on 2017/8/3 0003. */ @Component public class Knight { private Horse horse; Knight(Horse horse){ this.horse = horse; } public void ride(){ horse.bark(); } }
Horse代碼:
package entities; import org.springframework.stereotype.Component; /** * Created by Administrator on 2017/8/3 0003. */ @Component public class Horse { void bark(){ System.out.println("Horse!!!!"); } }
ActionConfig代碼:
package config; import entities.Knight; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * Created by Administrator on 2017/8/3 0003. */ @Configuration @ComponentScan(basePackageClasses = {Knight.class}) public class ActionConfig { }
這里的ComponentScan注解內(nèi)的屬性用以注明Knight類所在的包為掃描的基礎包。凡是這個基礎包及其子包內(nèi)的標注了@Component的類都將被視為Java Bean,這些Bean被Spring容器創(chuàng)建和管理。
Java配置類裝配
從自動裝配的代碼中,可以看出自動裝配是非常簡單方便的。Java配置類進行裝配的方式是采用對配置類中的方法進行注解標注,實現(xiàn)bean的創(chuàng)建和管理。
還是采用以上的例子,現(xiàn)在我們將上面的自動裝配改成Java配置類進行裝配。
首先,刪掉Knight和Horse中的@Component注解,刪掉ActionConfig中的@ComponentScn注解,因為這些注解都是自動裝配才需要的。
package entities; /** * Created by Administrator on 2017/8/3 0003. */ public class Knight { private Horse horse; public Knight(Horse horse){ this.horse = horse; } public void ride(){ horse.bark(); } } package entities; /** * Created by Administrator on 2017/8/3 0003. */ public class Horse { void bark(){ System.out.println("Horse!!!!"); } }
ActionConfig類:
package config; import entities.Horse; import entities.Knight; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by Administrator on 2017/8/3 0003. */ @Configuration public class ActionConfig { @Bean public Knight getKnight(){ return new Knight(getHorse()); } @Bean public Horse getHorse(){ return new Horse(); } }
通過@Bean注解對相關的方法進行修飾,Spring就可以知道調(diào)用這些方法來構(gòu)建相應的bean,并注入到依賴的對象中。
XML配置文件裝配
使用XML配置文件來裝配組件是最為繁瑣的,需要對各個bean做一一配置,特別是需要配置構(gòu)造器或者setter的參數(shù)。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
以上是一個空的XML配置文件,看一下就知道,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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="knight" class="entities.Knight"> <constructor-arg ref="horse"></constructor-arg> </bean> <bean id="horse" class="entities.Horse"></bean> </beans>
通過配置兩個bean元素,并且通過constructor-arg元素來配置構(gòu)造函數(shù)注入的bean,達到依賴注入的目的。
導入和混合配置
有時候單純的XML配置或者Java配置不滿足我們的需求怎么辦呢?Spring支持混合配置的方式來管理bean,通過Java和XML配置的混合,達到你想要的效果。
Java配置中引用Java配置
在配置類中使用@Import注解進行引入另一個Java配置類。
package config; import entities.Horse; import entities.Knight; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; /** * Created by Administrator on 2017/8/3 0003. */ @Configuration @Import(BeautyConfig.class) public class ActionConfig { @Bean public Knight getKnight(){ return new Knight(getHorse()); } @Bean(name = "horse") public Horse getHorse(){ return new Horse(); } }
BeautyConfig代碼:
package config; import entities.Beauty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by Administrator on 2017/8/3 0003. */ @Configuration public class BeautyConfig { @Bean Beauty getBeauty(){ return new Beauty(); } }
在Main中,只需要通過ActionConfig就可以獲取到所有的上下文對象。
package main; import config.ActionConfig; import entities.Beauty; import entities.Knight; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by Administrator on 2017/8/3 0003. */ public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ActionConfig.class); Knight knight = context.getBean(Knight.class); knight.ride(); Beauty beauty = context.getBean(Beauty.class); beauty.sleep(); } }
這里簡單使用Import引入了另一個配置的Java文件,就實現(xiàn)了將多個bean分別配置到多個Java配置類中。
Java配置中引入XML配置
在Java中引入xml配置的基本方法與引入Java代碼配置類似,只需要將@Import改為@ImportResource,配上xml的文件地址即可。
@ImportResource("classpath:spring.xml")
以上,就是Java配置spring依賴注入的方法。
XML配置中引入XML配置
同理,在XML配置中引入XML也需要使用Import,但是在XML中是import標簽,通過使用import標簽導入另一個xml文件。
這里我新建了一個another.xml的文件,用以作為另一個配置文件示例。在原有的spring.xml中加入以下代碼:
<import resource="another.xml"></import>
這樣就簡單導入了anoter.xml配置文件。
XML配置中引入Java配置
乍看之下,在XML中似乎沒有標簽可以導入Java配置類的,不過任何Java配置類都是一個bean,所以可以通過配置bean標簽來導入Java配置類!
<bean class="config.BeautyConfig"></bean>
通過在XML中配置這個Java配置類的bean,就直接導入了在Java配置類中的所有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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="another.xml"></import> <bean class="config.BeautyConfig"></bean> <bean id="knight" class="entities.Knight"> <constructor-arg ref="horse"></constructor-arg> </bean> <bean id="horse" class="entities.Horse"></bean> </beans>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java線程基本使用之如何實現(xiàn)Runnable接口
這篇文章主要介紹了Java線程基本使用之如何實現(xiàn)Runnable接口問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01SpringBoot Actuator監(jiān)控的項目實踐
本文主要結(jié)合 Spring Boot Actuator,跟大家一起分享微服務Spring Boot Actuator 的常見用法,方便我們在日常中對我們的微服務進行監(jiān)控治理,感興趣的可以了解一下2024-01-01Java中實現(xiàn)在一個方法中調(diào)用另一個方法
下面小編就為大家分享一篇Java中實現(xiàn)在一個方法中調(diào)用另一個方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02IntellJ idea使用FileWatch實時編譯less文件的方法
這篇文章主要介紹了IntellJ idea使用FileWatch實時編譯less文件的相關資料,需要的朋友可以參考下2018-02-02