国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

〔SPRING FRAMEWORK〕Dependencies

MonoLog / 1283人閱讀

摘要:每個(gè)屬性參數(shù)構(gòu)造函數(shù)中值的類型都能夠被成字符串類型。對(duì)比上文給出的個(gè)代碼片段,可發(fā)現(xiàn)皆在用不同的方法進(jìn)行元數(shù)據(jù)配置,并且被配置的具體對(duì)象是數(shù)據(jù)庫(kù)驅(qū)動(dòng)。

@(SPRING FRAMEWORK)

〔4〕7.4 Dependencies

聲明:

斜體字:《官檔》原文

斜體加粗字:《官檔》原文的重點(diǎn)字、詞、句

正體字+前置〔〕:個(gè)人表述行為,如:〔總結(jié)〕、〔分析〕等

灰體字:生詞

粉體字:疑問(wèn)

[TOC]

A typical enterprise application does not consist of a single object
〔總結(jié)〕一個(gè)典型的程序,是需要多個(gè)對(duì)象相互協(xié)作

how you go from defining a number of bean definitions that stand alone to a fully realized application where objects collaborate to achieve a goal.
〔總結(jié)〕定義一群具備相互協(xié)作能力的對(duì)象,以實(shí)現(xiàn)目標(biāo)程序

1. Dependency Injection

Dependency injection (DI):is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern.

1.1 Constructor-based dependency injection

關(guān)鍵詞:

Constructor-based DI is accomplished by the container invoking a constructor with a number of arguments, each representing a dependency.

Calling a static factory method with specific arguments to construct the bean is nearly equivalent, and this discussion treats arguments to a constructor and to a static factory method similarly.

示例代碼

public class SimpleMovieLister {

    // the SimpleMovieLister has a dependency on a MovieFinder
    private MovieFinder movieFinder;

    // a constructor so that the Spring container can inject a MovieFinder
    public SimpleMovieLister(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // business logic that actually uses the injected MovieFinder is omitted...

}

〔總結(jié)〕基于構(gòu)造函數(shù)的依賴注入,其核心在構(gòu)造函數(shù)上,即重點(diǎn)需要放在一個(gè)疑問(wèn)點(diǎn)上:構(gòu)造函數(shù)如何影響依賴注入(依賴注入的成功標(biāo)準(zhǔn):一個(gè)bean對(duì)象已完成實(shí)例化)?

一個(gè)規(guī)范:順序

示例代碼

package x.y;

public class Foo {

    public Foo(Bar bar, Baz baz) {
        // ...
    }

}

第1種bean引用方式:引用引用類型

〔注意〕bean引用方式是指:容器在實(shí)例化化一個(gè)bean時(shí),需要讀取配置元數(shù)據(jù)。因?yàn)榕渲迷獢?shù)據(jù)中包含了可實(shí)例化一個(gè)POJO對(duì)象的所有必要數(shù)據(jù)



    
    
        
        
    

    

    

第2種bean引用方式:引用基本數(shù)據(jù)類型

package examples;

public class ExampleBean {

    // Number of years to calculate the Ultimate Answer
    private int years;

    // The Answer to Life, the Universe, and Everything
    private String ultimateAnswer;

    public ExampleBean(int years, String ultimateAnswer) {
        this.years = years;
        this.ultimateAnswer = ultimateAnswer;
    }

}

基于type屬性



    
    

基于name屬性



    
    

基于index屬性



    
    
1.2 Setter-based dependency injection

關(guān)鍵詞: no-argument

〔總結(jié)〕符合以下任一點(diǎn),可選擇基于setter的依賴注入

無(wú)參構(gòu)造函數(shù)

無(wú)參工廠方法

public class SimpleMovieLister {

    // the SimpleMovieLister has a dependency on the MovieFinder
    private MovieFinder movieFinder;

    // a setter method so that the Spring container can inject a MovieFinder
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // business logic that actually uses the injected MovieFinder is omitted...

}

〔總結(jié)〕對(duì)比基于帶參構(gòu)造函數(shù)依賴注入和基于setter依賴注入,可以發(fā)現(xiàn):基于構(gòu)造函數(shù)的不需要提供setter/getter,基于setter的不需要提供帶參構(gòu)造函數(shù)

Q: Constructor-based or setter-based DI?

Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies. Note that use of the @Required annotation on a setter method can be used to make the property a required dependency.

可以混搭一起用。根據(jù)實(shí)際開(kāi)發(fā)經(jīng)驗(yàn),以Constructor-based DI為主,以setter-based DI為輔。主是強(qiáng)制,輔是可選。

The Spring team generally advocates constructor injection as it enables one to implement application components as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.

Constructor-based DI能確保

Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection.

Use the DI style that makes the most sense for a particular class. Sometimes, when dealing with third-party classes for which you do not have the source, the choice is made for you. For example, if a third-party class does not expose any setter methods, then constructor injection may be the only available form of DI.

〔總結(jié)〕懵逼?_?

1.3 Dependency resolution process

〔總結(jié)〕執(zhí)行注入的步驟(詳見(jiàn)《官檔》):

ApplicationContext根據(jù)配置元數(shù)據(jù)裝配所有bean對(duì)象

當(dāng)依賴的具體表現(xiàn)形式為以下時(shí):

properties

constructor arguments

arguments to the static-factory method
那么ApplicationContext會(huì)根據(jù)配置元數(shù)據(jù)的信息為POJO對(duì)象引用到對(duì)應(yīng)的bean對(duì)象上。

要保證在每個(gè)屬性,參數(shù)構(gòu)造函數(shù)能設(shè)值的時(shí)候,必須提供值,這個(gè)值:可以是基本值,也可以是引用值。

每個(gè)屬性、參數(shù)構(gòu)造函數(shù)中值的類型都能夠被Spring convert成字符串類型。

如何解決Circular dependencies ?

Circular dependencies場(chǎng)景

Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection.

If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference at runtime, and throws a BeanCurrentlyInCreationException.

Circular dependencies異常解決方案:
you can configure circular dependencies with setter injection.

1.4 Examples of dependency injection

代碼案例:參見(jiàn)《官檔》

2. Dependencies and configuration in detail 2.1 Straight values

觀察以下三個(gè)代碼片段:

代碼片段 1 使用最原始的方式


    
    
    
    
    

代碼片段 2 : 使用p-namespace配置元數(shù)據(jù)



    

代碼片段 3:使用java.util.Properties



    
    
        
            jdbc.driver.className=com.mysql.jdbc.Driver
            jdbc.url=jdbc:mysql://localhost:3306/mydb
        
    

〔總結(jié)〕
1. 閱讀對(duì)應(yīng)《官檔》后,依舊對(duì)小標(biāo)題#Straight values#的具體含義模糊不清,不清楚其牽引下文的意圖是什么。
2. 對(duì)比上文給出的3個(gè)代碼片段,可發(fā)現(xiàn):皆在用不同的方法進(jìn)行元數(shù)據(jù)配置,并且被配置的具體對(duì)象是數(shù)據(jù)庫(kù)驅(qū)動(dòng)

2.2 References to other beans

關(guān)鍵詞:ref

The ref element is the final element inside a or definition element.Here you set the value of the specified property of ③ a bean to be a reference to another bean (a collaborator) managed by the container. The referenced bean is a dependency of the bean whose property will be set, and it is initialized on demand as needed before the property is set. (If the collaborator is a singleton bean, it may be initialized already by the container.) All references are ultimately a reference to another object. Scoping and validation depend on whether you specify the id/name of the other object through the bean, local, or parent attributes.
〔總結(jié)〕從上文可得

ref屬性在哪用,有什么用

略提bean作用域、bean驗(yàn)證對(duì)bean對(duì)象的影響。

示例代碼



    


    class="org.springframework.aop.framework.ProxyFactoryBean">
    
         
    
    
2.3 Inner beans

    
    
         
            
            
        
    

〔總結(jié)〕inner bean類似于Java的內(nèi)部類

2.4 Collections

Collection merging

Limitations of collection merging

Strongly-typed collection

Spring Collections 映射 Java Collections
—— List
—— Set
—— Map
—— Properties

示例代碼


    
    
        
            administrator@example.org
            support@example.org
            development@example.org
        
    
    
    
        
            a list element followed by a reference
            
        
    
    
    
        
            
            
        
    
    
    
        
            just some string
            
        
    

〔總結(jié)〕根據(jù)示例代碼,得以下結(jié)論:
keyvalue;value也可以是用以下屬性作為key或value值:

bean | ref | idref | list | set | map | props | value | null
2.5 Null and empty string values

示例代碼:2種方式為字符串設(shè)置NULL或空值

設(shè)空值時(shí), 第1種方式:


    

設(shè)空值時(shí), 第2種方式:

exampleBean.setEmail("")

設(shè)NULL時(shí), 第1種方式:


    
        
    

設(shè)NULL時(shí), 第2種方式:

exampleBean.setEmail(null)
2.6 XML shortcut with the p-namespace

代碼片段


    
    
    
        
    
    
    
    

代碼片段 :使用p:spouse-ref=""



    
        
        
    

    

    
        
    
2.7 XML shortcut with the c-namespace

代碼片段



    
    

    
    
        
        
        
    

    
    

2.8 Compound property names

代碼片段:確保fred,bob屬性的值不為空,即確保不拋 NullPointerException異常


    
3. Using depends-on

depends-on的作用:
explicitly force one or more beans to be initialized before the bean using this element is initialized.

使用depends-on的場(chǎng)景:

a static initializer in a class needs to be triggered.

database driver registration

代碼片段 1


代碼片段 2


    



〔總結(jié)〕參閱現(xiàn)有博客提供的博主,及具體應(yīng)用場(chǎng)景

4. Lazy-initialized beans

延遲加載(初始化)bean對(duì)象。

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.

代碼示例


When the preceding configuration is consumed by an ApplicationContext, the bean named lazy is not eagerly pre-instantiated when the ApplicationContext is starting up, whereas the not.lazy bean is eagerly pre-instantiated.

However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContext creates the lazy-initialized bean at startup, because it must satisfy the singleton’s dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.

代碼示例: 控制懶加載級(jí)別


    

〔總結(jié)〕參閱《官檔》,多次閱讀原文旨意。

5. Autowiring collaborators

自動(dòng)裝配的優(yōu)勢(shì)?

自動(dòng)裝配的劣勢(shì)?

自動(dòng)裝配的模式?

自動(dòng)裝配的限制?

自動(dòng)裝配的應(yīng)用?

〔總結(jié)〕Spring有能力自動(dòng)裝配協(xié)作者(即對(duì)象之間存在依賴關(guān)系)。
自動(dòng)裝配的優(yōu)點(diǎn):

Autowiring can significantly reduce the need to specify properties or constructor arguments.

Autowiring can update a configuration as your objects evolve. For example, if you need to add a dependency to a class, that dependency can be satisfied automatically without you needing to modify the configuration.

〔理論總結(jié)〕使用自動(dòng)裝配后,配置文件具備自更新能力。主流開(kāi)發(fā)的趨勢(shì)之一。

autowire //基于XML
5.1 Limitations and disadvantages of autowiring

Explicit dependencies in property and constructor-arg settings always override autowiring. You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). This limitation is by-design.

Autowiring is less exact than explicit wiring. Although, as noted in the above table, Spring is careful to avoid guessing in case of ambiguity that might have unexpected results, the relationships between your Spring-managed objects are no longer documented explicitly.

Wiring information may not be available to tools that may generate documentation from a Spring container.

Multiple bean definitions within the container may match the type specified by the setter method or constructor argument to be autowired. For arrays, collections, or Maps, this is not necessarily a problem. However for dependencies that expect a single value, this ambiguity is not arbitrarily resolved. If no unique bean definition is available, an exception is thrown.

5.2 Excluding a bean from autowiring

禁用某特定bean的自動(dòng)裝配功能

用法

作用

5.2.1 用法

基于XML

autowire-candidate=false 

基于注解

@Autowired
5.2.2 作用

unavailable to the autowiring infrastructure.
only affect type-based autowiring.

6. Method injection

主要用于解決以下場(chǎng)景:
在大多數(shù)應(yīng)用場(chǎng)景中,bean對(duì)象一般都是單例對(duì)象。但在bean對(duì)象的依賴情況下:

一個(gè)單例依賴另一個(gè)單例

一個(gè)非單例依賴另一個(gè)非單例

古老的做法:是將一個(gè)bean定義為另一個(gè)bean的屬性。以此建立依賴關(guān)系。

如:A-bean 依賴 B-bean,那么POJO可定義為:

public class A{
    public B b;
}

public class B{}

bean之間的依賴關(guān)系建立后,會(huì)產(chǎn)生一個(gè)問(wèn)題: 兩個(gè)bean的生命周期不一樣,怎么辦?
古老的做法是這樣子的,見(jiàn)代碼示例:
【代碼示例說(shuō)明】:

CommandManager obejct在系統(tǒng)架構(gòu)中屬于業(yè)務(wù)邏輯層(SERVICE)

CommadManger object的開(kāi)放式方法process(Map commandState)的調(diào)用依賴Command obejct

根據(jù)【1.】和【2.】可得,兩個(gè)類之間存在密切的依賴關(guān)系。所以Spring提供一種方式:當(dāng)依賴方(CommandManager)的行為受制于被依賴方(Command)時(shí),可以在依賴方內(nèi)部實(shí)現(xiàn)被依賴方的初始化。完成上述行為的前提的是:依賴方(CommandManager)需要實(shí)現(xiàn)接口ApplicationContextAware

// a class that uses a stateful Command-style class to perform some processing
package fiona.apple;

// Spring-API imports
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

//依賴方(CommanderManager)現(xiàn)了ApplicationContextAware
public class CommandManager implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    //process()的執(zhí)行受制于Command對(duì)象
    public Object process(Map commandState) {
        
        Command command = createCommand();
       
        command.setState(commandState);
        
        return command.execute();
    }

    protected Command createCommand() {
        // notice the Spring API dependency!
        return this.applicationContext.getBean("command", Command.class);
    }

    public void setApplicationContext(
            ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

這種做法需要你放棄Spring framework的IoC特性。以上述代碼來(lái)看,bean object有兩個(gè):CommandManagerCommand。需要分析以下幾個(gè)問(wèn)題

兩個(gè)bean object中,依賴方是誰(shuí)? 被依賴方又是誰(shuí)?
answer:依賴方:CommandManager; 被依賴方:Command

被依賴方的bean obejct的生命周期如何得到保證?
answer:要保證被依賴方的生命周期隨時(shí)不死。那么依賴方實(shí)現(xiàn)接口ApplicationContextAware,目的是為了調(diào)用Spring framework的方法

這種在業(yè)務(wù)層耦合Spring framework代碼已破化IoC原則,因?yàn)樵趯?shí)現(xiàn)接口的那一刻起,IoC原則就已經(jīng)被破壞了。即這種解決方案不可取。Spring給出了2種可靠的方案:

lookup method injection

arbitrary method replacement

〔經(jīng)驗(yàn)之道〕業(yè)務(wù)層的所有代碼應(yīng)該滿足以下注釋要求

// no more Spring imports!
6.1 Lookup method injection

參閱《官檔》,思考代碼示例邏輯

6.2 Arbitrary method replacement

參閱《官檔》,思考代碼示例邏輯

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/70372.html

相關(guān)文章

  • Broadleaf概念

    摘要:本部分是可以找到有關(guān)功能和概念的大部分信息的地方。促銷系統(tǒng)包含一個(gè)高度可配置的促銷系統(tǒng)。異步消息通過(guò)與現(xiàn)代代理交互,實(shí)現(xiàn)應(yīng)用程序消息的異步處理。將智能地將自己的配置信息與實(shí)施者在運(yùn)行時(shí)提供的信息合并。添加了方法以允許包含任何符合的加密方案。 本部分是可以找到有關(guān)Broadleaf功能和概念的大部分信息的地方。我們描述了購(gòu)物車修改,定價(jià)和付款等操作的重要性,以及Broadleaf支持的其...

    peixn 評(píng)論0 收藏0
  • Spring Boot 2.1.2 & Spring Cloud Greenwich 升級(jí)

    摘要:節(jié)前沒(méi)有新業(yè)務(wù)代碼,正好剛發(fā)布,于是開(kāi)始為期四天的框架代碼升級(jí)。還好并沒(méi)有使用它的,配置上有一個(gè)小坑,的是表示而是表示,之前配置成的,如果到的里面那就要拋異常了。 節(jié)前沒(méi)有新業(yè)務(wù)代碼,正好Greenwich剛發(fā)布,于是開(kāi)始為期四天的框架代碼升級(jí)。 之前的版本是 spring boot 1.5.10 , spring cloud Edgware.SR3 依賴升級(jí) 增加依賴管理插件 ap...

    newsning 評(píng)論0 收藏0
  • Spring Boot Reference Guide Memorandum

    此文章為Spring Boot Reference Guide(2.1.5.RELEASE)的備忘錄。 Chapter 8. Introducing Spring Boot You can use Spring Boot to create a Java application that can be started by using java -jar or more traditional w...

    imccl 評(píng)論0 收藏0
  • 厲害了,Spring Cloud for Alibaba 來(lái)了!

    摘要:棧長(zhǎng)有話說(shuō)其實(shí)項(xiàng)目就是為了阿里的項(xiàng)目能很好的結(jié)合融入使用,這個(gè)項(xiàng)目目前由阿里維護(hù)。對(duì)同時(shí)使用和阿里巴巴項(xiàng)目的人來(lái)說(shuō)無(wú)疑帶來(lái)了巨大的便利,一方面能結(jié)合無(wú)縫接入,另一方面還能使用阿里巴巴的組件,也帶來(lái)了更多的可選擇性。 最近,Spring Cloud 發(fā)布了 Spring Cloud Alibaba 首個(gè)預(yù)覽版本:Spring Cloud for Alibaba 0.2.0. 大家都好奇,...

    lbool 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<