Spring for beginners in Java ssh

Source: Internet
Author: User

Spring for beginners in Java ssh
In this article, I learned two ways of dependency injection: Set Value injection and construct injection. In our previous thinking, if we call a class, we need to manually instantiate it. When we create a called job that does not need to be completed, this is the control reversal, when the called instance process is completed by Spring and injected into the caller, this is dependency injection. I. First, let's learn how to set value injection. First, we will create two new interface specifications named Computer respectively. java and Key. java, the path is in com. sep. basic. service Package: Computer. the java code is as follows: 1 package com. sep. basic. service; 2 3 public interface Computer {4 public void useKey (); 5} Key. the java code is as follows: 1 package com. sep. basic. service; 2 3 public interface Key {4 // Method 5 public String print (); 6} write two implementation classes, respectively named Lenovo. java and LogiTech. java, which implements the Computer and Key interfaces respectively. sep. basic. service. impl package. Lenovo. the java code is as follows: Copy code 1 package com. sep. basic. service. impl; 2 3 import com. sep. basic. service. computer; 4 import com. sep. basic. service. key; 5 6 public class Lenovo implements Computer {7 private Key key; 8 9 public Key getKey () {10 return key; 11} 12 13 // setter method 14 public void setKey (Key key Key) {15 this. key = key; 16} 17 18 // implement the keyboard attribute 19 public void useKey () {20 System. out. println (ke Y. print (); 21} 22 23} copy the code LogicTech. the java code is as follows: Copy code 1 package com. sep. basic. service. impl; 2 3 import com. sep. basic. service. key; 4 5 public class LogiTech implements Key {6 // implement the print method 7 public String print () {8 return "" in the Key interface "; 9} 10 11} copy the code. Next we configure applicationContext. add the following code to the xml file: Copy code 1 <! -- Configure the Lenovo instance --> 2 <bean id = "lenovo" class = "com. sep. basic. service. impl. Lenovo"> 3 <! -- Inject a logicTech instance to the key property --> 4 <property name = "key" ref = "logicTech"> </property> 5 </bean> 6 <! -- Configure the LogicTech instance --> 7 <bean id = "logicTech" class = "com. sep. basic. service. impl. logicTech "> </bean> copy the code OK, and then return to the main method we used in the previous test. In SpringTest, add the following code: // inject Lenovo lenovo = ctx through Spring. getBean ("lenovo", Lenovo. class); lenovo. useKey (); now running, you can see that the console outputs "typing with a keyboard ". In applicationContext. xml, the id in the <bean> tag is the unique identifier of bean, and the class is the address of the class to be instantiated. By writing the code in SpringTest, we can see that the lenovo class we call is not manually instantiated, and the key called in the Code is not redundant, all the work is done by spring. Through the configuration file, we not only created our Lenovo instance, but also created the key instance for value injection. If we need to change the key value at that time, we only need to write the implementation class of the corresponding key, and set 1 in the configuration file <! -- Change <bean id = "LogicTech" class = "com. sep. basic. service. impl. logicTech"> </bean> to <! -- Configure the LogicTech instance --> 7 <bean id = "logicTech" class = "full name of the corresponding implementation class"> </bean>. 2. I am now learning about constructor injection. in simple words, constructor injection is used to set dependencies. First, we need. class. The modified code is as follows: Copy code 1 package com. sep. basic. service. impl; 2 3 import com. sep. basic. service. computer; 4 import com. sep. basic. service. key; 5 6 public class Lenovo implements Computer {7 private Key key; 8 9 // default constructor 10 public Lenovo (){}; 11 12 // constructor with Parameters 13 public Lenovo (Key key) {14 this. key = key; 15} 16 17 // setter Method 18 public void setKey (Key key) {19 this. key = key; 20} 21 22 // implement the keyboard attribute 23 public void useKey () {24 System. out. println (key. print (); 25} 26 27} copy the code. Similarly, modify the configuration file and use the <constructor-arg '''> label to construct the injection, the modified code is as follows: Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans 3 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 <! -- Configure the Lenovo instance --> 8 <bean id = "lenovo" class = "com. sep. basic. service. impl. lenovo "> 9 <constructor-arg ref =" logicTech "> </constructor-arg> 10 </bean> 11 <! -- Configure the LogicTech instance --> 12 <bean id = "logicTech" class = "com. sep. basic. service. impl. logicTech "> </bean> 13 </beans>: copy the code and run it. You can also see the statement output:" typing with a keyboard ". OK. I have implemented the dependency injection of the two methods. There are a lot of things to digest. I have to worry about it myself. It is very tiring to say so many words. If it wasn't my recent work that wasn't very busy, I guess I have to type and vomit blood!

Related Article

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.