Spring 3 for beginners of Java ssh, sshspring

Source: Internet
Author: User

Spring 3 for beginners of Java ssh, sshspring

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.

1. Learn how to set value injection first:

We will first create two new interface specifications named Computer. java and Key. java respectively, and their paths are 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 // Method 5 public String print (); 6}

Next, write two implementation classes named Lenovo. java and LogiTech. java, respectively, to implement the Computer and Key interfaces, and put the path under the com. sep. basic. service. impl package.

The Lenovo. java code is as follows:

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 (key. print (); 21} 22 23}

The Code of LogicTech. java is as follows:

1 package com. sep. basic. service. impl; 2 3 import com. sep. basic. service. key; 4 5 public class LogiTech implements Key {6 // Method 7 public String print () {8 return "typed on keyboard"; 9} 10 11}

Next we configure the applicationContext. xml file and add the following code to it:

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>

OK, then go back to the main method we used in the previous test. In SpringTest, add the following code:

// Use Spring to inject Lenovo lenovo = ctx. getBean ("lenovo", Lenovo. class); lenovo. useKey ();

Run now. 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. In the configuration file

1 <! -- Configure the LogicTech instance --> 2 <bean id = "logicTech" class = "com. sep. basic. service. impl. LogicTech"> </bean>

Change

<! -- Configure the LogicTech instance --> 7 <bean id = "logicTech" class = "full name of the corresponding implementation class"> </bean>

You can.

2. I am now learning about constructor injection:

Literally, constructor injection uses constructor to set dependencies.

First, we need to modify the previous Lenovo. class. The modified code is as follows:

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}

Similarly, the configuration file needs to be modified, and the constructor-arg '''> label must be used to construct the injection. The modified code is as follows:

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>

Run the command. 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!

 

 

 


Which one should I learn first?

Hibernate, or Struts (if you have learned servlet/jsp ),

I am a beginner of the three major java frameworks (ssh). I am sure you have recommended some books to help me learn the three major frameworks.

Lightweight Java EE Enterprise Application Practice (version 3rd): Struts 2 + Spring 3 + Hibernate Integrated Development

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.