Spring的主要特性包括IOC和DI,其中DI是IOC的基礎。在以前的Spring使用過程中大部分都是使用XML配置文件顯式配置spring組件,導致大量的XML配置文件以及冗余的XML配置代碼。閱讀《Spring in Action》后總結Spring的DI功能的三種主要裝配方式以及混合裝配方式
根據注解自動裝配
Spring中有非常豐富的注解,通過這些注解可以方便地配置Spring容器,使得Spring容器可以自動識別相關Bean并做自動注入裝配。
使用注解
- @Component注解:標注一個類,標識此類為一個Java Bean。
- @Configuration注解:標注一個類,標識此類為一個Java配置類
- @ComponentScan注解:標注一個類,用以配置掃描的包名稱
示例代碼
這里使用一個最簡單的例子來展示自動裝配的配置,代碼結構如圖所示。
ActionConfig類用于做全局配置,我們知道,一名騎士一般都會有一匹馬,所以這里Horse和Knight類作為Java Bean被Spring容器創建,并將Horse類的一個bean注入到Knight中。所以在Horse和Knight中必須使用@Component注解進行修飾。
Knight代碼:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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代碼:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
|
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代碼:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
|
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注解內的屬性用以注明Knight類所在的包為掃描的基礎包。凡是這個基礎包及其子包內的標注了@Component的類都將被視為Java Bean,這些Bean被Spring容器創建和管理。
Java配置類裝配
從自動裝配的代碼中,可以看出自動裝配是非常簡單方便的。Java配置類進行裝配的方式是采用對配置類中的方法進行注解標注,實現bean的創建和管理。
還是采用以上的例子,現在我們將上面的自動裝配改成Java配置類進行裝配。
首先,刪掉Knight和Horse中的@Component注解,刪掉ActionConfig中的@ComponentScn注解,因為這些注解都是自動裝配才需要的。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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類:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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就可以知道調用這些方法來構建相應的bean,并注入到依賴的對象中。
XML配置文件裝配
使用XML配置文件來裝配組件是最為繁瑣的,需要對各個bean做一一配置,特別是需要配置構造器或者setter的參數。
1
|
2
3
4
5
6
7
|
<? 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真實復雜啊...
1
|
2
3
4
5
6
7
8
9
10
11
|
<? 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元素來配置構造函數注入的bean,達到依賴注入的目的。
導入和混合配置
有時候單純的XML配置或者Java配置不滿足我們的需求怎么辦呢?Spring支持混合配置的方式來管理bean,通過Java和XML配置的混合,達到你想要的效果。
Java配置中引用Java配置
在配置類中使用@Import注解進行引入另一個Java配置類。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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代碼:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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就可以獲取到所有的上下文對象。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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文件,就實現了將多個bean分別配置到多個Java配置類中。
Java配置中引入XML配置
在Java中引入xml配置的基本方法與引入Java代碼配置類似,只需要將@Import改為@ImportResource,配上xml的文件地址即可。
1
|
|
@ImportResource ( "classpath:spring.xml" ) |
以上,就是Java配置spring依賴注入的方法。
XML配置中引入XML配置
同理,在XML配置中引入XML也需要使用Import,但是在XML中是import標簽,通過使用import標簽導入另一個xml文件。
這里我新建了一個another.xml的文件,用以作為另一個配置文件示例。在原有的spring.xml中加入以下代碼:
1
|
|
< import resource = "another.xml" ></ import > |
這樣就簡單導入了anoter.xml配置文件。
XML配置中引入Java配置
乍看之下,在XML中似乎沒有標簽可以導入Java配置類的,不過任何Java配置類都是一個bean,所以可以通過配置bean標簽來導入Java配置類!
1
|
|
< bean class = "config.BeautyConfig" ></ bean > |
通過在XML中配置這個Java配置類的bean,就直接導入了在Java配置類中的所有bean,真是神奇!
以下是配置XML的全部代碼:
1
|
2
3
4
5
6
7
8
9
10
11
12
13
|
<? 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 > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.jianshu.com/p/b6286cfb3f34?utm_source=tuicool&utm_medium=referral