HelloWorld Spring Dependency Injection/control inversion (DI/IOC) version

Source: Internet
Author: User
Tags getmessage

HelloWorld Spring Dependency Injection/control inversion (DI/IOC) versionRain, Date: 2014-10-29
Summary:This article is mainly used to train beginners to understand the basic concepts of dependency injection in spring. The basic concept of dependency injection is introduced first, and then the HelloWorld instance is implemented with constructor injection as an example.
Spring Dependency Injection/control inversionIn our usual programming, if Class A relies on Class B, it is usually a to create an instance of B. Instead, spring gives the work of creating an instance of B to the spring container to complete, then injects a, so called Dependency injection (DI, Dependency injection). This creation of the callee's work is no longer done by the caller, and is therefore referred to as control inversion (IoC, inversion of controls). Dependency Injection organizes the bean as a file, reducing the coupling of the program. Dependencies between instances are managed by the IOC container.
Dependency Injection HelloWorld instancesThere are many ways to inject, such as constructor injection, set value injection, and so on, with constructor injection as an example.
Step One: Build the spring environmentJoin the project by directly downloading the jar package or introduce spring's dependent jar package via Maven/gradle. Specific reference: http://spring.io/
Step Two: Write a specific class1. Define the interface class Messageservice.java
Package Cn.dennishucd;public interface Messageservice {public String getMessage ();}

2. Write the implementation class for the interface Messageserviceimpl.java
Package Cn.dennishucd;public class Messageserviceimpl implements Messageservice {@Overridepublic String getMessage () { Return "Hello world!";}}

3. Writing service consumption class Messageprinter.java
Package Cn.dennishucd;public class Messageprinter {final private messageservice service;public Messageprinter ( Messageservice service) {this.service = service;} public void Printmessage () {System.out.println (This.service.getMessage ());}}

4. Write the main program class Helloworldspring.java
Package Cn.dennishucd;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Helloworldspring {public static void Main (string[] args) {ApplicationContext context = new Classpathxmlapplicationcontext ("Helloworld.xml"); Messageprinter printer = (messageprinter) context.getbean ("printer");p rinter.printmessage ();(( Classpathxmlapplicationcontext). Close ();}}

Step Three: Write the spring configuration fileThe file name can be arbitrary, here named Helloworld.xml,the file is placed in the root directory of SRC.
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"    Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"    xmlns:context= "Http://www.springframework.org/schema /context "    xsi:schemalocation="        Http://www.springframework.org/schema/beans/                http Www.springframework.org/schema/beans/spring-beans-3.0.xsd        Http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-3.0.xsd ">        <bean id=" service "class=" Cn.dennishucd.MessageServiceImpl "/>        <bean id=" printer "class=" Cn.dennishucd.MessagePrinter ">     <constructor-arg ref= "service"/>    </bean></beans>

Fourth Step: Run the programRun program, display: "Hello world!"
Program Explanation:(1) Messageprinter to use Messageservice to get the message service, it is not directly in the class new messageservice of the specific implementation class, but instead provides an injection interface, that is, the construction injection here. (2) Specific injection of which Messageservice implementation can be configured in the spring configuration file, that is, <constructor-arg ref= "service"/> to specify. And the instantiation of this concrete implementation class is also implemented by the spring container. (3) Here is just a Hello message service example, imagine if the data access service is used as a system. The consumer of the data access service only needs to define an interface and provide an injection method, which can be configured in the configuration file using the data access implementation. If the system needs to replace the data access implementation, only the specific data access implementation to be injected in the configuration file needs to be modified. This implements the loosely coupled interface-oriented programming. (4) The main program creates a spring application context that takes an instance of the Messageprinter consumer directly from the application context and then invokes the appropriate method.
References:1. Http://spring.io/Spring official website 1. http://www.cnblogs.com/linjiqin/archive/2013/11/04/3407126.html Spring IOC Container Fundamentals 2. http://blog.csdn.net/a906998248/article/details/7514085 Spring's core mechanism: Dependency Injection (inversion of control) 3. Http://outofmemory.cn/code-snippet/3670/spring-inject-by-annotation Spring Dependency Injection: Annotation Injection Summary 4. http://www.yiibai.com/spring/spring_hello_world_example.html Spring Hello World instance

HelloWorld Spring Dependency Injection/control inversion (DI/IOC) version

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.