Java Spring implements control inversion

Source: Internet
Author: User

Let's go over the polymorphism, and simply say that a reference to a class can point to itself and to the object of its subclass.

Like these:

Fatherclass a = new Fatherclass ();

Fatherclass a = new Son1class ();

Fatherclass a = new Son2class ();

Again, the interface and its implementation class, the parent class and the subclass is the inheritance relationship (parent-child relationship), the interface and the implementation class relationship is different from it;

Interfaces only define methods, there is no method body, and the interface implementation class is responsible for supplementing the method body, the method body of multiple implementation classes can be different.

Now define an interface Interfacedemo and two implementation classes DEMOIMP1, DEMOIMP2. three classes have the same name method say ();

Interfacedemo B = new Demoimp1 ();

Interfacedemo C = new DEMOIMP2 ();

Then B.say () and C.say () are called with two different methods.

Speaking of which, it's time for spring to debut!!!

1. Interface-Oriented programming

From the above we can see that we want to invoke the Say () method of an implementation class, first to instantiate an object of this class, whether it is a direct instantiation of the

or instantiate the class object in a way that references a polymorphic state.

and interface-oriented, is not explicitly instantiate this implementation class object, but define an interface class object, and then define the method to invoke the interface class to

method, and then, when the program is actually using this method, the setter () method is used to declare that the interface class object is specifically pointing to which implementation class

Object, and then when you run this method, you really call the method of the interface implementation Class (the interface and the implementation class have the same method names).

1  Public classUserService {2     //to define a reference to an interface3     PrivateUserdao Userdao;4 5     //defines the setter method, sets the interface's reference to which implementation class's Object6      Public voidSetuserdao (Userdao Userdao) {7          This. Userdao =Userdao;8     }9 Ten      Public voidUseradd (User newuser) { One         //at this point the Userdao has been implemented to point to a specific interface implementation class object after spring dependency injection A         //then the method that invokes the interface is actually called the method of the particular implementation class . - Userdao.adduser (newuser); -     } the}

2. Dependency Injection

Also called interface injection, it can be regarded as the object that specifies which implementation class the interface object Userdao to point to, and then implements the implementation class method.

of the call.

The following is the Beans.xml file:

1     id= "Todo" class= "Com.eco.daoimp.Usertodo1" ></bean>2     3     <!-- Defines a reference to the internal interface of the UserService class (Userdao) to a specific implementation class object (USERTODO1)--4     class= "  Com.eco.service.Userservice">5         <property name="userdaoref= "Todo" ></property>6     </bean>

There are three methods of dependency injection, only the most commonly used setter injection, the red Word can be seen as a connector, the second Bean tag implementation

The Declaration interface object Userdao is a pointer to Usertodo1 this implementation class, then the natural Userdao.adduser (NewUser) is actually

To instantiate a Usertodo1 object, and then call the AddUser (NewUser) method of the Usertodo1 object.

3. The test class clarifies the point of the interface object by manipulating the XML file

1  Public classTest1 {2 @Test3      Public voidAdd () {4         //Spring reads beans. XML file5ApplicationContext CTX =NewClasspathxmlapplicationcontext ("Beans.xml");6         //Parse bean tag with ID UserService, internally implemented Userdao Userdao = new Usertodo1 ()7UserService service = (userservice) ctx.getbean ("UserService");8User NewUser =NewUser ("Orange Mulberry", 31);9         //The Useradd () method called at this point is the Useradd () method of the interface implementation class USERTODO1Ten Service.useradd (newuser); One     } A}

In this way, a call to the Usertodo1 Useradd (NewUser) method via an XML file is implemented. I added a tag for each method of implementing the class,

1 // Usertodo1 class 2 System.out.println ("todo1 Create the user"); 3 4 // Usertodo2 class 5 System.out.println ("todo2 Create the user");

Above the bean file configuration, finally run the test class, console printout:

1 todo1 Create the user

Then we should have a look at the bean tag above:

1 class= "Com.eco.daoimp.Usertodo2" ></bean>

Console Print Output:

1 todo2 Create the user

This way we can simply change the bean file to implement a call to the different implementation class methods, and no longer have to instantiate the class in code specifically.

Java Spring implements control inversion

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.