Beginner Java SSH Spring third

Source: Internet
Author: User
Tags lenovo

In this article, I learned two ways of dependency injection: Set value injection and construct injection.

In our previous thinking, if we were to invoke a class, we would need to instantiate it manually, and when we created the called work that did not require us to complete, this is the control reversal, which is the dependency injection when the instance process that will be invoked is completed by spring and injected into the caller.

First, to learn to learn to set value injection:

Let's start with the new two interface specification, named Computer.java and Key.java, with the paths all under the Com.sep.basic.service package:

The Computer.java code is as follows:

1  Package Com.sep.basic.service; 2 3  Public Interface Computer {4      Public void Usekey (); 5 }

The Key.java code is as follows:

1  Package Com.sep.basic.service; 2 3  Public Interface Key {4     // a method 5      Public String print (); 6 }

Next write two implementation classes, named Lenovo.java and Logitech.java, respectively, implement the computer and key interface, the path is placed under the Com.sep.basic.service.impl package.

The Lenovo.java code is as follows:

1  PackageCom.sep.basic.service.impl;2 3 ImportCom.sep.basic.service.Computer;4 ImportCom.sep.basic.service.Key;5 6  Public classLenovoImplementscomputer{7     Privatekey key;8     9      PublicKey GetKey () {Ten         returnkey; One     } A      -     //Setter method required to set value injection -      Public voidSetkey (key key) { the          This. Key =key; -     } -      -     //implementing the properties of the keyboard using the computer interface +      Public voidUsekey () { - System.out.println (Key.print ()); +     } A  at}

The code for Logictech.java is as follows:

1  PackageCom.sep.basic.service.impl;2 3 ImportCom.sep.basic.service.Key;4 5  Public classLogiTechImplementskey{6     //implementing a printing method in the key interface7      PublicString Print () {8         return"Typing with the keyboard";9     }Ten  One}

Next we configure the Applicationcontext.xml file, where the following code is added:

1     <!--will configure the Lenovo instance -2     <BeanID= "Lenovo"class= "Com.sep.basic.service.impl.Lenovo">3         <!--injecting an Logictech instance into the key property -4         < Propertyname= "Key"ref= "Logictech"></ Property>5     </Bean>6     <!--configures the Logictech instance -7     <BeanID= "Logictech"class= "Com.sep.basic.service.impl.LogicTech"></Bean>

OK, then go back to the main method that we tested before, springtest, and add the following code to it:

// through spring to complete the injection Lenovo Lenovo = Ctx.getbean ("Lenovo", Lenovo.  Class); Lenovo.usekey ();

Now run, you can see the console output "typing with the keyboard."

The ID in the <bean> tag in Applicationcontext.xml is the bean's unique identity, and class is the address of the class that needs to be instantiated.

By writing in the Springtest code can see that we call the Lenovo class, we do not manually instantiate, and where the key is called, we have not done superfluous operation, all the work is spring to help us do, it through the configuration file, Not only did we create our Lenovo instances, but we also built the key instances where we injected the values.

If we need to change the value of key, we just have to write the corresponding key implementation class, in the configuration file will

1 <!--  - 2  <   id = "Logictech" Class = "Com.sep.basic.service.impl.LogicTech" ></ Bean >

Switch

<!---7     < id = "Logictech"  Class= "Corresponding implementation class full name"></bean>

Can.

Second, I am now starting to learn construction injection:

In simple terms, construct injection is a way of using constructors to set dependencies.

First of all we need to modify the previous Lenovo.class, the revised code is as follows:

1  PackageCom.sep.basic.service.impl;2 3 ImportCom.sep.basic.service.Computer;4 ImportCom.sep.basic.service.Key;5 6  Public classLenovoImplementscomputer{7     Privatekey key;8     9     //default constructorTen      PublicLenovo () {}; One      A     //Constructs a constructor that injects parameters -      PublicLenovo (key key) { -          This. Key =key; the     } -  -     //Setter method required to set value injection -      Public voidSetkey (key key) { +          This. Key =key; -     } +      A     //implementing the properties of the keyboard using the computer interface at      Public voidUsekey () { - System.out.println (Key.print ()); -     } -  -}

Similarly, the configuration file needs to make corresponding changes, the construction injection needs to use the <constructor-arg ' ' > tag to complete, the modified code is as follows:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beans3     xmlns= "Http://www.springframework.org/schema/beans"4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5 xmlns:p= "http://www.springframework.org/schema/p"6 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">7     <!--will configure the Lenovo instance -8     <BeanID= "Lenovo"class= "Com.sep.basic.service.impl.Lenovo">9         <Constructor-argref= "Logictech"></Constructor-arg>Ten     </Bean> One     <!--configures the Logictech instance - A     <BeanID= "Logictech"class= "Com.sep.basic.service.impl.LogicTech"></Bean> - </Beans>

Run, you can also see the statement output: "Typing with the keyboard."

OK, the two methods of dependency injection I have achieved, need to digest a lot of things ah, I also have to ponder, say dozen so many words very tired ah, but not recently work not how busy, I estimate to typing to vomit blood ah!

Beginner Java SSH Spring third

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.