Spring Learning IOC Control inversion (1)

Source: Internet
Author: User

Before you begin:

1. This blog post is original, reproduced please indicate the source

2. The author is not a computer trained, if there are errors, please correct me.

---------------------------------------------------------------------------'s starting. La La la--------------------------------------- ----------------------------------------

From the beginning of contact with spring, the most heard is the IOC (inversion of Control) and AOP (aspect-oriented programming). Spring's official document gives such a frame diagram (>< looks so esoteric ~ ~). However, the so-called inversion of control introduced in this article is implemented through the beans inside the core and the context jar package. Other jar packages are described in later chapters.

Control inversion, also known as Dependency injection (Dependency injection), refers to when dependencies are created, or obtained from the factory, other classes can be derived from this dependency by constructing methods, factory methods, or set methods. In layman's terms, it is the creation and use of dependencies within a class that does not need to be new in the class itself, as long as it is injected. For example, I have a car, need fuel to start, but I can not go to mine fuel, I just need to go to the gas station to add oil, whether you are oil or shell oil, in short, the oil is injected in, the car can start. The fuel here is a dependency class that can be injected into the dependency class that we call the bean, and the bean is initialized, configured and integrated, which we call the IOC container (similar to a gas station). The compilation of IOC containers can be implemented using Xml,java annotations or Java programs. Although I prefer to annotate the way (especially with spring Boot), but to get started, or to use XML to grasp the basics of the mood to choose the way it ~

So the question is, how is the bean configured? A simple bean definition structure is given below.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 ">    <BeanID="..."class="...">        <!--collaborators and configuration for this bean go here -    </Bean>    <BeanID="..."class="...">        <!--collaborators and configuration for this bean go here -    </Bean>    <!--More beans definitions go here -</Beans>

This is a simple XML code, you can see that beans has a lot of beans defined, each bean is mapped to a class, these classes are the dependencies you want to use. Let's see how the container and the beans are related.

ApplicationContext context =    new classpathxmlapplicationcontext (new string[] {"Services.xml", "Daos.xml"});

The applicationcontext here is the container interface inside the context jar package, which reads two XML files as a container configuration to complete the creation and association of various beans.

Let's take a practical look at first defining two XML files

Service.xml (There is a dependency in the service is called Petstore, and Petstore inside another two dependent Accountdao and Itemdao)

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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" >    <!--Services -    <BeanID= "Petstore"class= "Com.dabingguozi.generaltest.pojo.PetStoreService">        < Propertyname= "Accountdao"ref= "Accountdao"/>        < Propertyname= "Itemdao"ref= "Itemdao"/>        <!--additional collaborators and configuration for this bean go here -    </Bean>        <!--More beans definitions for services go -</Beans>

Daos.xml (the dependencies Petstore injected in the service Accountdao and Itemdao are defined here)

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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" >    <BeanID= "Accountdao"class= "Com.dabingguozi.generaltest.pojo.AccountDAO">        <!--additional collaborators and configuration for this bean go here -    </Bean>    <BeanID= "Itemdao"class= "Com.dabingguozi.generaltest.pojo.ItemDAO">        <!--additional collaborators and configuration for this bean go here -    </Bean>    <!--More beans definitions for data access objects go -</Beans>

Finally, don't forget to define your three classes in Java (Petstoreservice.java, Itemdao.java, Accountdao.java Note to include the Set method) and your main program (below)

 Public classApp { Public Static voidMain (string[] a) {ApplicationContext context=NewClasspathxmlapplicationcontext (NewString[] {"Daos.xml", "Services.xml"});//Retrieve configured instancePetstoreservice service = Context.getbean ("Petstore", Petstoreservice.class);//Use configured instanceList<string> userlist =service.getusernamelist ();    System.out.print (userlist); }}

If there is an error, see if the jar package is not imported, I use Maven management lib, see: Https://mvnrepository.com/artifact/org.springframework

Try it in IntelliJ and see if it's the effect we want.

We succeeded in typing the dependent class name. Scattered flowers ^ ^ So, our spring tour has officially begun ~ ~

Next: Spring Learning IOC Control inversion (2) (This is not link, poke not open 2333)

The next article continues to study some of the contents of the bean definition in control reversal, Bo Master first Bo, Prudential Xie Watch.

This blog content refers to the Spring framework official documents, if there is a conflict, please refer to the original content

Http://docs.spring.io/spring/docs/current/spring-framework-reference/html/index.html

Spring Learning IOC Control inversion (1)

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.