Spring Learning (5): Configuration of Di

Source: Internet
Author: User

I. Some concepts

The dependencies that are said in the application generally refer to the relationships between classes.

Generalization: represents the inheritance relationship between a class and a class, and the inheritance between interfaces and interfaces;

implementation: represents the implementation of class-to-interface;

dependency: When a relationship between a class and a class is a dependency, unlike an association, a dependency does not have an "owning relationship", but an "acquaintance" that has a relationship only in a particular place (such as a method body).

Association: A representation of a dependency between a class and a class or class and an interface, expressed as an "owning relationship", where the code can be represented by an instance variable;

  Aggregation: belonging to the special Case of association, embodying part-whole relationship, is a weak ownership relationship, the whole and the part can have different life cycle, is a weak association;

  combination: belong to the special case of the Association, also embodies the partial-overall relationship, is a strong "ownership relationship", the whole and part have the same life cycle, is a strong association;

  The Spring IOC container relies on two layers of meaning: The bean relies on the container management lifecycle and the bean's dependency on container injection bean dependency resources. Bean dependency Container: The bean relies on the IOC container to create the bean and manage the bean's life cycle. The bean relies on the container to inject the bean's dependent resources: the container is responsible for injecting the bean's dependent resources, and the dependent resources allow the bean, external files, constants, and so on to react as objects in Java, and the container is responsible for assembling the dependencies between the beans.

  the benefits of Dependency Injection:(1) Better practice interface-oriented programming, code clarity, and the ability to dynamically replace bean dependent objects (external resources). (2) Increase the reuse of beans and reduce the coupling between beans.

Two. Inject

There are two ways to inject: constructor injection and setter injection.

Constructor injection:

 <!--constructor injection static factory instantiation injection, instance factory instantiation injection omitted--<bean id= "DIHelloService1" class= "Self.springmvc.diConfig.service.impl.HelloServiceImpl" > <!--parameter index injection--<constructor-arg index= "0" value= "Hello world,di"/> <constructor-arg index= "1" value= "1"/> </bean> <bean id= "Dihel        LoService2 "class=" Self.springmvc.diConfig.service.impl.HelloServiceImpl "> <!--parameter name into the bean constructor injected-- <constructor-arg name= "msg" value= "Hello world,di"/> <constructor-arg name= "Size" value= "2"/> &L T;constructor-arg name= "City" ref= "City"/> </bean> <bean id= "DIHelloService3" class= "Self.springmvc.diCo Nfig.service.impl.HelloServiceImpl "> <!--parameter Type--<constructor-arg type=" java.lang.String "value = "Hello world,di"/> <constructor-arg type= "Java.lang.Integer" value= "3"/> </bean> 

Setter injection, which contains some demonstration of data type injection (constructor injection is also possible):

<!--setter method to inject bean setter Method--<bean id="DIHelloService4" class="Self.springmvc.diConfig.service.impl.HelloServiceImpl"> <!--inject different data type instances--<!--injection constants--<property Name="msg"Value="Hello World,di"/> <property name="size"Value="4"/> <!--injection Bean setter injected--<property name=" City" ref=" City"/> <property name="City2"> <refbean=" City"/> </property> <!--injection list--> <property name="List"> <list value-type="java.lang.String"> <value> Zhang San </value> <value> John Doe </value> <value&gt ; Harry </value> </list> </property> <!--injection set--> <property name="Set"> <SetValue-type="java.lang.String"> <value> Zhang San s</value> <value> John Doe s</value> <value& gt; Harry S</value> </Set> </property> <!--two-dimensional array injection--<property name="twodilist"> <array> <array> <value>1</value> <value>2</value> </array> <array> <value>5</value> </array> </array> </property> <!--map injection--&GT        ; <property name="Map"> <map key-type="java.lang.String"Value-type="java.lang.String"> <entry> <key> <value> xiamen </value> </key> <value>0592</value> </entry> <entry> <key> <value> Quanzhou </value> </key> <value>05995</value> </entry> </map> </property> <!--injection JAVA.UTIL.P Roperties--> <property name="Properties"> <props> <prop key="1"> John Doe </prop> <prop key="2"> Zhang San </prop> </props> </property> <!--null value---<property NA Me=" Times"> <NULL/> </property> </bean> <bean id=" City" class="self.springmvc.diConfig.entity.City"> <property name="Code"Value="350300"></property> <property name="name"Value="Xiamen"></property> </bean>

Bean Injection:

    <!--Bean Injection--    <!--constructor injection--    <bean id= "DIHelloService5" class= " Self.springmvc.diConfig.service.impl.HelloServiceImpl ">        <constructor-arg ref=" City "name=" City "> </constructor-arg>    </bean>    <!--setter injection--    <bean id= "DIHelloService6" class= " Self.springmvc.diConfig.service.impl.HelloServiceImpl ">        <property name=" City ">            <ref bean=" City ></ref>        </property>    </bean>    <!--internal Bean definition, externally invisible-    <bean Id= "DIHelloService7" class= "Self.springmvc.diConfig.service.impl.HelloServiceImpl" >        <property name= " City ">            <bean class=" self.springmvc.diConfig.entity.City "/>        </property>    </bean >

  

Spring Learning (5): Configuration of Di

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.