Spring Learning (1) -- Introduction to dependency injection in Spring

Source: Internet
Author: User

Spring is too important to me. I need to use Spring for a web-related project. Every time I look at Spring-related knowledge, I always feel a little confused. I don't have a good system to study, now, take some time to learn about Spring. I don't know why I have an inexplicable liking for Spring, maybe because of his name and hope. For more information about Spring learning, see. This article was first published by livestock TT in the blog Park. Please keep the integrity of the article and note: Author: livestock TT. Link: http://www.cnblogs.com/xt0810/p/3594253.html#spring] Spring Framework is an open-source Java/Java EE full-stack application Framework released in the form of an Apache license.. NET platform. The framework is based on the Code in the book "Expert One-on-One Java EE Design and Development (ISBN 0-7645-4385-7)", initially developed by Rod Johnson and Juergen Hoeller. Spring Framework provides a simple development method that avoids a large number of attribute files and helper classes that may cause complex and chaotic underlying code. Inversion of Control (IoC) is a design principle in object-oriented programming. It can be used to reduce the coupling between computer code. The most common method is Dependency Injection (DI), and Dependency Lookup ). When an object is created, the external entity of all objects in a control system transmits the reference of the object to it. It can also be said that the dependency is injected into the object. [Dependency injection] When learning Spring, we always encounter this term, that is, dependency injection. So we first learn what dependency injection is. Several examples are provided to illustrate what is dependency injection and what is dependency injection in spring. Let's take a Person car-based example. We hope that we can better learn and understand what dependency injection is. Of course, the example is very simple, and it may be much more complicated in actual projects. I. First case: tightly coupled, open BMW. 1. The analysis is as follows: Define three classes: Person. java, BaoMa. java, and MainTest. java. The following code creates a Baoma object in the Person constructor, which tightly binds the Person class and the Baoma class. Now, the Person class can only start BMW, if you want to launch Audi now, the Person class cannot be processed. Unless you modify the Person class, this makes the logic of the two classes not clear enough in the case of tight coupling, if the business logic is complex, these tightly coupled scenarios will lead to many drawbacks, such as difficult code testing, reuse, and comprehension. In addition, it is unreasonable to let the Person class manage the creation of objects in the Baoma class, because the Person class does not have the responsibility to manage objects in the Baoma class. 2. the Code is as follows: 1) Person. java copy code 1 public class Person {2 private BaoMa baoma; 3 public Person () {4 baoma = new BaoMa (); // The Person and Baoma classes are tightly coupled. 5} 6 public void driver () {7 baoma. guaDang (); 8 baoma. caiYouMen (); 9 baoma. daFangXiang (); 10} 11} copy Code 2) BaoMa. java copy code public class BaoMa {public void GuaDang () {System. out. println ("I'm a BMW, I'm on the hook");} public void CaiYouMen () {System. out. println ("I'm a BMW, I'm stepping on the accelerator ");} Public void DaFangXiang () {System. out. println ("I'm a BMW, I'm playing the steering wheel") ;}} copy the Code 3) MainTest. java public class MainTest {public static void main (String [] args) {Person boy = new Person (); boy. driver () ;}} 3. running result: I am a BMW, I am mounting my archive, I am a BMW, I am stepping on the accelerator, I am a BMW, I am playing the steering wheel 2, the first situation: in the case of tight coupling, open Audi. 1. the analysis is as follows: from the code above, if we want to drive Audi, we can only modify the Person class, and so on. If the driver wants to drive a different car each time, you need to modify the Person class every time. This is unreasonable for the Person class. The Person class only cares about driving. I have a driver method, I call the driver method every time I want to drive, so this tightly coupled encoding is not ideal. 2. the Code is as follows: 1) Person. java copy code 1 public class Person {2 private AuDi audi; 3 public Person () {4 audi = new AuDi (); 5} 6 public void driver () {7 audi. guaDang (); 8 audi. caiYouMen (); 9 audi. daFangXiang (); 10} 11} copy Code 2) AuDi. java copy code 1 public class AuDi {2 public void GuaDang () {3 System. out. println ("I'm an Audi car, I am mounting a file"); 4} 5 public void CaiYouMen () {6 System. out. println ("I'm Audi, I'm stepping on the accelerator"); 7} 8 public voi D DaFangXiang () {9 System. out. println ("I'm Audi, I'm playing the steering wheel"); 10} 11} copy code 3) MainTest. java 1 public class MainTest {2 public static void main (String [] args) {3 Person boy = new Person (); 4 boy. driver (); 5} 6} 3. running results: 1. I'm Audi, I am in the file, 2. I'm Audi, I'm stepping on the accelerator, 3. I'm Audi, And I'm playing the steering wheel. 3. 2: simple dependency injection. 1. the analysis is as follows: in java's design philosophy, the core idea of dependency injection is the dependency between specific classes, which should be converted to abstract dependencies as much as possible, that is, the following code is the same, the Person class depends on the abstract Car class, rather than the specific Baoma class or the specific AuDi class. for analysis, see the code comment. 2. the Code is as follows: 1) Person. java copy code 1 public class Person {2 private Car car; 3 public Person (Car car) {// constructor injection, passed in is car, that is, an interface that all models must implement. 4 this. car = car; // here, we can respond to the implementation of any vehicle, such as Audi and BMW. 5} // here, the Person class is not coupled with any specific type of Car. For Person, any specific Car only needs to implement the Car interface. The specific model is irrelevant to the Person. 6 public void driver () {7 car. guaDang (); 8 car. caiYouMen (); 9 car. daFangXiang (); 10} 11} copy Code 2) Car. java 1 public interface Car {2 public abstract void GuaDang (); 3 public abstract void CaiYouMen (); 4 public abstract void DaFangXiang (); 5} 3) AuDi. java copy code 1 public class AuDi implements Car {2 public void GuaDang () {3 System. out. println ("I'm an Audi car, I am mounting a file"); 4} 5 public void CaiYouMen () {6 System. out. pr Intln ("I'm Audi, I'm stepping on the accelerator"); 7} 8 public void DaFangXiang () {9 System. out. println ("I'm Audi, I'm playing the steering wheel"); 10} 11} copy code 4) MainTest. java copy code 1 public class MainTest {2 public static void main (String [] args) {3 AuDi audi = new AuDi (); 4 Person boy = new Person (audi ); 5 boy. driver (); 6} 7} copy code 3. running results: 1. I'm Audi, I am in the archive 2. I'm Audi, I'm on the accelerator 3. I'm Audi, I'm on the steering wheel 4. The third scenario is: spring dependency injection. 1. The analysis is as follows: as described above, we should have a better understanding of dependency injection. How does Spring implement IOC? Through dependency injection, the object dependency is set by the third-party components responsible for coordinating objects in the system when creating objects. Objects do not need to create or manage their dependencies themselves-dependencies are automatically injected into the desired image. Spring is such a third-party component. By using the Spring framework, the dependencies between classes are managed through Spring. The specific methods and methods for Spring dependency injection are time-consuming to learn and summarize. Today, we only learn how to understand Spring dependency injection. 2. the Code is as follows: 1) Person. java copy code 1 public class Person {2 private Car car; 3 public Person (Car car) {// constructor injection, passed in is car, that is, an interface that all models must implement. 4 this. car = car; // here, we can respond to the implementation of any vehicle, such as Audi and BMW. 5} // here, the Person class is not coupled with any specific type of Car. For Person, any specific Car only needs to implement the Car interface. The specific model is irrelevant to the Person. 6 public void driver () {7 car. guaDang (); 8 car. caiYouMen (); 9 car. daFangXiang (); 10} 11} copy Code 2) Car. java 1 public interface Car {2 public abstract void GuaDang (); 3 public abstract void CaiYouMen (); 4 public abstract void DaFangXiang (); 5} 3) AuDi. java copy code 1 public class AuDi implements Car {2 public void GuaDang () {3 System. out. println ("I'm an Audi car, I am mounting a file"); 4} 5 public void CaiYouMen () {6 System. out. pr Intln ("I'm Audi, I'm stepping on the accelerator"); 7} 8 public void DaFangXiang () {9 System. out. println ("I'm Audi, I'm playing the steering wheel"); 10} 11 12} copy code 4) cartest. xml copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <beans xmlns = "http://www.springframework.org/schema/beans" 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 4 xmlns: p = "http://www.springframework.org/schema/p" 5 xmlns: context = "http://www.springframework.org/schema/context" 6 xsi: schemaLocation = "http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 10 <bean id = "cartest" class = "Person"> 11 <constructor-arg ref = "Car"/> 12 </bean> 13 <bean id = "Car" class = "AuDi"/> 14 </beans> copy Code 5) mainTest. java copy code 1 import org. springframework. context. applicationContext; 2 import org. springframework. context. support. classPathXmlApplicationContext; 3 public class MainTest {4 public static void main (String [] args) {5 ApplicationContext context = new ClassPathXmlApplicationContext ("cartest. xml "); 6 Person boy = (Person) context. getBean ("cartest"); 7 boy. driver (); 8} 9}

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.