0.3, Spring source code learning - talk from a UML model

Copyright Notice: Welcome to reprint exchange, to declare the source. Performance status prior to the state of mind, habits prior to determination, focus first on preferences --Bestcxx https://blog.csdn.net/bestcxx/article/details/90706166

Foreword

Performance status prior to the state of mind, habits prior to determination, focus first on preferences.

Inspired article

The past few weeks we have seen some "new" code written work in the development and Sping source learning, and this produced some inspiration.

Encountered in the development of the situation is such that I need for a reconciliation function to transform the existing program has two characteristics, one is the reconciliation function can not specify the time, called by a scheduled task, the task always generates a timing of yesterday; the second characteristic is due to dock a number of companies, and each company nor the same form of reconciliation. Transformation point is the need to provide an interface that allows you to generate reconciliation file in the specified date, but can not affect the original function.
Normally, I need to add a method to time as the Senate can. But to do so each company reconciliation logic I need to be change - the system existing method calls and no scheduled tasks to the Senate.
Surprisingly I found the code structure using an interface - the parent - in the form of a subclass, which provides private-modified time parameters and get, set method in the parent class, abstract method in the interface at the same time to time assigned
such a to transform my thinking becomes - the first call to the method of assigning time method, and then call the original file reconciliation, tracking code logic can be found, the system will need to get in place by the time all the modifications "get yesterday" to " If the parameter is not the time yesterday to get empty, otherwise it gets the specified time parameters "- increase in utility class in this method can, then reconstructed after the test to get

Spring viewing the source code in the process, found such a case, the top Spring is the interface, even some of the interface is not abstract methods and then implement these interfaces quilt classes, subclasses have a new subclass, which can also simplify the an interface - parent - child class structure, and the interface can use the parent class or sub-class is instantiated, and the subclass can in the parent class multiplexing. This Spring provides great convenience from different sources to load configuration file

FIG gives a UML

Based interfaces discussed above - the parent - child class model, the author made a further extension is to provide two interfaces, and these two interfaces have exactly the same abstract method.

Here Insert Picture Description

Asked the first question

InterfaceB in InterfaceA and have the same abstract method sayHello (), class FirstClass need to rewrite several sayHello () method?

The answer is a. This means that if we just want to call the sayHello () method, using InterfaceA or InterfaceB to inject Spring effect is the same.

The second issue raised

InterfaceA Sub1Class Can be instantiated (or be injected into the Spring), FirstClass whether Sub1Class can be instantiated (or be injected into Spring)?

The answer is: Yes; can. Implement interfaces and subclass inherits from a parent class is a subclass, which are reflected in Java inheritance mechanism, but one thing is to note, Spring is used by default JDK dynamic proxy, need the help of interface injection, although Spring also cglib dynamic proxy support, but may not find the problem will be reported during the proxy class FirstClass injection Sub1Class.

Interface - parent - child benefits class model

The advantage of this model is that when we add a new feature when you can go through a new interface statement, then realized in the parent class, and subclasses can be called directly, but do not need to do extra work for extensions to He said changes to the code are very friendly.

Guess you like

Origin blog.csdn.net/bestcxx/article/details/90706166