Sut dibyniaeth pigiad yn gweithio yn y Gwanwyn?

Dependency Injection

Trosolwg

pigiad ffa Gwanwyn hefyd yn cael ei alw yn '' Chwistrellu Dibyniaeth (DI)’. Mae calon fframwaith Gwanwyn yw 'Inversion Of Rheoli (IOC)’ a DI yw'r rhan gweithrediad IOC. Byddwn yn dechrau ein trafodaeth gyda'r dybiaeth fod y cysyniad a phwrpas IOC a DI yn ei wybod yn barod.

In this article, byddwn yn canolbwyntio ar wahanol fathau o bigiadau ffa Gwanwyn gyda rhai enghreifftiau codio ymarferol.








dibyniaeth Chwistrellu

Gall pigiadau ffa gwanwyn yn cael ei berfformio mewn dwy ffordd fawr, un yn cael ei adnabod fel DI-seiliedig Constructor a'r un arall yn cael ei adnabod fel DI-seiliedig Setter. Yn yr is-adrannau canlynol byddwn yn gwirio sut y gall y rhain gael eu cyflawni.

DI Seiliedig Constructor

Mewn DI seiliedig Constructor, dibyniaethau yn cael eu chwistrellu drwy adeiladwyr. Each argument of the constructor is a dependency object/bean. The Spring container invokes the constructors with the arguments.

Let us check one example. There are two classes, one is the Country class and the other one is the State dosbarth. The Country class has a dependency on the State dosbarth. So, we will pass the dependency to the Country class through its constructor (as shown below).

Listing 1: Country class

package com.techalpine.spring;

public class Country {

private State state;

public Country(State state) {

System.out.println(“Inside Country constructor.” );

this.state = state;

}

public void stateName() {

state.stateName();

}

}








Now, this dependency can be used to call different methods.

Following is the State class code snippets.

Listing 2: State class

package com.techalpine.spring;

public class State {

public State(){

System.out.println(“Inside State constructor.” );

}

public void stateName() {

System.out.println(“Inside State name: West Bengal”);

}

}

Following is the XML configuration file to wire those beans and their dependencies. The bean id (‘stateBean’) of the State dosbarth yn cael ei ddefnyddio fel cyfeiriad dadl Constructor yn y Country diffiniad ffa dosbarth. Now, y ffa yn cael eu diffinio a gwifrau gyda'i gilydd.

Listing 3: ffeil ffurfweddu XML

<?xml version=”1.0″ encoding=”UTF-8″?>

<ffa xmlns =”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd”>

<!– Diffiniad ar gyfer ffa Gwlad –>

<ffa id =”countryBean” class =”com.techalpine.spring.Country”>

<Constructor-ARG cyf =”stateBean”/>

</ffa>

<!– Diffiniad ar gyfer ffa Wladwriaeth –>

<ffa id =”stateBean” class =”com.techalpine.spring.State”>

</ffa>

</ffa>

Yn dilyn yw'r brif dosbarth i brofi'r pigiad ffa seiliedig Constructor-.

Listing 4: Prif ffeil y cais

package com.techalpine.spring;

import org.springframework.context.ApplicationContext;

mewnforio org.springframework.context.support.FileSystemXmlApplicationContext;

TestBeanInjection dosbarth gyhoeddus {

gwag statig gyhoeddus prif(Llinyn[] args) {

ApplicationContext cyd-destun = FileSystemXmlApplicationContext newydd(

“WebContent / WEB-INF / beanConfig.xml”);

Gwlad countryobj = (Country)context.getBean(“countryBean”);

countryobj.stateName();

}

}








Now, rhedeg y dosbarth fel cais sy'n sefyll ar ei ben ei hun a bydd yn dangos yr allbwn canlynol ar y consol.

Inside State constructor
Y tu mewn Constructor Gwlad
Inside State name: West Bengal

Setiau Canolfannau DI

Mewn DI seiliedig Setter, dibyniaethau yn cael eu pasio drwy ddulliau Rhai sy'n Cynnau. cynhwysydd Gwanwyn ennyn dulliau gosodwr rhai enghreifftio y ffa. Here, y mecanwaith a'r cydrannau yn cael eu bron yn debyg i'r DI seiliedig Constructor. Yr unig wahaniaeth yw, yn pasio dibyniaethau a cyfluniad XML. Mewn pigiad seiliedig meddyg, <eiddo> tagiau yn cael eu defnyddio yn lle <Constructor-ARG> tag.








Casgliad

Yn yr erthygl hon rydym wedi esbonio'r ffyrdd sut gall ffa gwanwyn yn cael ei chwistrellu. Nid oes rheol bendant pryd i ddefnyddio DI seiliedig gosodwr neu Constructor seiliedig. Rydym yn gyffredinol yn mynd am DI seiliedig Constructor pan fydd y dibyniaethau yn orfodol, a dewis DI seiliedig osodwr-pan fydd y dibyniaethau yn ddewisol. Ond mae cyfuniadau o ddau hefyd yn cael eu caniatáu.

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share