Spring in Action(一) Core Spring:Advanced Wiring

Spring的核心是dependency injection (DI) and aspect-oriented programming (AOP).
java

第一章概覽一下什麼是Spring,以及DI和AOP是怎麼解耦(decoupling )應用組件的。web

第二章講模塊、bean怎麼鏈接起來,咱們會了解spring提供的三種配置方式:自動配置,基於java的配置和用xml配置。【wiring beans,不清楚標準翻譯法,暫翻譯爲鏈接,裝配,反正大概就是這個意思】算法

第三章延續第二章講一些高級的技術技巧,包括帶有特定條件的配置,自動裝配時的歧義處理,域,以及Spring表達式語言。【Spring Expression Language)】spring

第四章講Spring的AOP如何解耦全路服務(好比安全控制及審計)decouple system-wide services (such as security and auditing)  】數據庫

 


3.1 環境與配置文件:數據庫配置,加密算法,外部系統的集成等都是一些開發環境,一個可行的方案是利用好比說maven的profile,經過選用不一樣的類或xml文件進行rebuild來實現不一樣環境下的應用搭建,spring有比這個更好的方案。安全

3.1.1 java上配置@Profile來達到目的,3.2以上的spring能夠把這個註解放到method上。相應的,xml在頂級或者子集的beans上增長profile屬性來達到這個目的。session

3.1.2 profile激活方式,兩個級別:spring.profiles.active ,spring.profiles.default. 前者優先級高,後者優先級低,六種方式:初始化DispatcherServlet 的參數,web應用的上下文參數,JNDI鍵值對,環境變量,JVM系統屬性,繼承測試類的@ActiveProfiles
註解。自由配對組合。
maven

 

3.2 條件beanide

@Conditional註解,參數爲一個實現了Condition接口的類.Condition的matches方法傳入的參數有ConditionContext:測試

1 public interface ConditionContext {
2     BeanDefinitionRegistry getRegistry();
3     ConfigurableListableBeanFactory getBeanFactory();
4     Environment getEnvironment();
5     ResourceLoader getResourceLoader();
6     ClassLoader getClassLoader();
7 }

以及AnnotatedTypeMetadata :

public interface AnnotatedTypeMetadata {
    boolean isAnnotated(String var1);
    Map<String, Object> getAnnotationAttributes(String var1);
    Map<String, Object> getAnnotationAttributes(String var1, boolean var2);
    MultiValueMap<String, Object> getAllAnnotationAttributes(String var1);
    MultiValueMap<String, Object> getAllAnnotationAttributes(String var1, boolean var2);
}

事實上,Profile就是經過這種方式實現的。

1 @Target({ElementType.TYPE, ElementType.METHOD})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Documented
4 @Conditional({ProfileCondition.class})
5 public @interface Profile {
6     String[] value();
7 }
 1 class ProfileCondition implements Condition {
 2     ProfileCondition() {
 3     }
 4 
 5     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
 6         if(context.getEnvironment() != null) {
 7             MultiValueMap attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
 8             if(attrs != null) {
 9                 Iterator var4 = ((List)attrs.get("value")).iterator();
10 
11                 Object value;
12                 do {
13                     if(!var4.hasNext()) {
14                         return false;
15                     }
16 
17                     value = var4.next();
18                 } while(!context.getEnvironment().acceptsProfiles((String[])((String[])value)));
19 
20                 return true;
21             }
22         }
23 
24         return true;
25     }
26 }

3.3 若是你足夠敏銳的話,在以前的章節就應該有一個問題縈繞在你的腦海中,那就是若是同一個接口有多個實現類被放到bean聲明中,或者多個@Component註解被ComponentScan發現,這中狀況該如何處理?

3.3.1用@Primary能夠指定一個默認選擇。xml中則是primary=「true」

3.3.2 在@AutoWired或@Inject上用@Qualifier註解指定一個全限定名,bean的全限定名默認是id,id默認是首字母小寫的類名。固然,全限定名和id都是能夠指定的。另外一種方式是建立自定義的限定註解,所有註解一致時匹配。

 

3.4 bean的域

按前面說的,正常狀況下bean只會複用同一個引用,也就是說,至關於每一個bean都是單例模式。除了這種模式外還有其餘模式可供選擇,Prototype 每次都建立一個新的bean,用了就扔,Session 在web應用中沒建立一個session就會new一個,Request 在web應用中對於一個Request,new一個bean。

java註解:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) ;xml屬性:scope=「prototype」

3.4.1 request和session域.TODO:web看完後我再回頭修改。

相關文章
相關標籤/搜索