Spring Control reversal IOC

Source: Internet
Author: User

Spring's inversion of control (IOC) Idea, object instances are no longer created by the caller, but instead are created by the spring container. The spring container is responsible for the relationship between the control program, which is no longer directly controlled by the code, and the control is shifted from the application's code to the external container, the so-called control reversal. Spring has two IOC containers, and here I use the applicationcontext.


Take a class as an example:

public class UserService {public void AddUser () {System.out.println ("user Add");}}

register the class in XML, file name Beans.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"       Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"       xsi:schemalocation= "http://www.springframework.org/ Schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd "><!--production of any content--><bean Id= "Userserviceid" class= "Com.canyugan.hello.UserService" ></bean></beans>

Finally, let's take a little test:

<span style= "White-space:pre" ></span> @Testpublic  void Demo1 () {//load config file ApplicationContext Applicationcontext=new classpathxmlapplicationcontext ("Com/canyugan/hello/beans.xml");// Get Object UserService userservice= (userservice) Applicationcontext.getbean ("Userserviceid") from spring's factory; Userservice.adduser ();}


Finally, enjoy the powerful features of spring's IOC.

Spring Control reversal IOC

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.