[Spring Framework] learning notes -- Dependency injection (DI), dependencyspring

Source: Internet
Author: User

[Spring Framework] learning notes -- Dependency injection (DI), dependencyspring

1. Implement DI through Constructor

Simple Type instance

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;
  
// If you cannot compile in debug mode, add the following line. For method 3 below, the parameter name is used.
@ ConstructorProperties ({"years", "ultimateAnswer "})
public ExampleBean(int years, String ultimateAnswer) {        this.years = years;        this.ultimateAnswer = ultimateAnswer;    }}

The corresponding xml configuration is as follows:

<Bean id = "exampleBean" class = "examples. ExampleBean">

// Method 1: pass type. If two parameters of the same type exist, no. <Constructor-arg type = "int" value = "7500000"/> <constructor-arg type = "java. lang. String" value = "42"/>

// Method 2, through the parameter location.
    <constructor-arg index="0" value="7500000"/>    <constructor-arg index="1" value="42"/>

// Method 3, with the parameter name.
    <constructor-arg name="years" value="7500000"/>    <constructor-arg name="ultimateAnswer" value="42"/>
</bean>

Object Type instance

package x.y;public class Foo {    public Foo(Bar bar, Baz baz) {        // ...    }}

Xml configuration

<beans>    <bean id="foo" class="x.y.Foo">        <constructor-arg ref="bar"/>
        <constructor-arg ref="baz"/>    </bean>    <bean id="bar" class="x.y.Bar"/>    <bean id="baz" class="x.y.Baz"/></beans>

You can also specify parameters as follows:

    <constructor-arg>        <ref bean="bar"/>    </constructor-arg>

If you use the factory model, you can use the following method:

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">    <constructor-arg ref="anotherExampleBean"/>    <constructor-arg ref="yetAnotherBean"/>    <constructor-arg value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {    // a private constructor    private ExampleBean(...) {        ...    }    // a static factory method; the arguments to this method can be    // considered the dependencies of the bean that is returned,    // regardless of how those arguments are actually used.    public static ExampleBean createInstance (        AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {        ExampleBean eb = new ExampleBean (...);        // some other operations...        return eb;    }}

 

2. Implement DI through the set Method

<bean id="exampleBean" class="examples.ExampleBean">    <!-- setter injection using the nested ref element -->    <property name="beanOne">        <ref bean="anotherExampleBean"/>    </property>    <!-- setter injection using the neater ref attribute -->    <property name="beanTwo" ref="yetAnotherBean"/>    <property name="integerProperty" value="1"/></bean><bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

Note: The property name is the same as the name in the set method.

public class ExampleBean {    private AnotherBean beanOne;    private YetAnotherBean beanTwo;    private int i;    public void setBeanOne(AnotherBean beanOne) {        this.beanOne = beanOne;    }    public void setBeanTwo(YetAnotherBean beanTwo) {        this.beanTwo = beanTwo;    }    public void setIntegerProperty(int i) {        this.i = i;    }}

 

Conclusion: In our daily project, how can we use the above two methods?

It can be seen from the need that if this attribute is required, it should be placed in the constructor; if it is optional, it should be set.


What does IoC/dependency injection (DI) in Spring mean? Is the name different? Which one is in Spring 3?

The content of the same thing is different. IoC controls inversion, emphasizing the role of the container and is used to organize or control the bean running in the container. DI dependency injection. It is emphasized that the Bean needs external injection for normal operation. Comparatively speaking, the container framework (such as Spring) emphasizes control and how to better control the running of other beans. On the contrary, the module emphasizes injection, what exactly do I need dependency injection.
In essence, the main advantage is to decouple through interfaces, and then use the container configuration file to organize bean operation, which is more scalable and can be flexibly used for Large-scale Module and component-level programming. Spring is already a mature container framework. Therefore, most people mainly consider what dependency injection is required.

The IOC of Spring is different from that of DI.

There is no big difference, but the expression is different. One is dependency injection, and the other is control inversion,

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.