Spring / Spring-Boot learn to get started

On the entry-Spring / Spring-Boot me around a lot of detours, a typical detour to bypass me is:

Find a Spring-Boot entry case, download the code ran again, it does not understand the code after running through, in particular the various configuration files and notes so I can not understand exactly how this program to run up. @Autowired, @Controller these annotations in the end what it meant, its work process like I suspect that they are no notes relevant knowledge to learn, so dig down notes works -?> A dynamic proxy -> Reflection - -> class load -> How class loader. After such a set to go down, even though I understand the muddle class loader, reflection, dynamic proxy, annotations, I still can not understand is how these annotations spring of work, do not know how to actually use them.

Many introductory tutorial, numerous specific operation, but does not tell us why the spring to use this annotation, this annotation with him after what role, as well as the workflow of these annotations (I am referring to is the use of the process not the principle underlying).

Here are some of my own learning reflection.

First, we start from the most basic needs.

java is object-oriented language, each object can be bound to a certain operation, through multiple objects, we can complete all the operations required for a task.

For example, assume that our task is to receive a user localhost: 8080 / user get this link request, to return json data string containing the user's browser a user data "{" name ":" bob "}". We refer to this task is divided into a plurality of objects to accomplish: Data Access Objects DAO used to query the database to obtain the user name Bob; controller receives the control object request to the outside; Model objects are used to assemble data; such a discrete object with To complete this task, we need to be combined to complete the transfer of data. We need to be included in the controller object model object, because the object model that contains the data to be returned to the front desk; objects in the model, we need to include the DAO object, because the data needed to generate the model required to check the database. By combining the object is actually a combination of various operations, we have completed the transfer of data, complete the task.

In the above process, we found that we need an object contains objects provided downstream of the present operation of the object class. Such as DAO object model is a downstream target object, the object of the operation model data is needed to provide support DAO object, so we need to include a DAO object in the object model, the DAO object model as a member of. The problem is that the DAO object is from where?

A straightforward idea is to provide direct DAO objects in the constructor when constructing model object, so the object model will be able to get a reference to the DAO object. How DAO object to it? Of course, also constructed of a (new), that if the DAO object in downstream objects also need to include how to do it? As a member of the DAO objects necessarily requires a database connection object connector, straightforward ideas like model objects, providing direct connector at construction time. However, it may require additional connector members .... thus forming layers of the recursive call. When constructing top-level object, the inclusion of the layers of relationship, we need to construct all the objects downstream layers of recursion. Recursion in the production of harm is self-evident, that is easy OOM. Then how to solve this problem? A natural idea is that if we know in advance which downstream objects have, we will start before the environment downstream of these objects are constructed well in advance, and then when the top of an object to be used directly take over the use of like, do not have to own recursive construction. If the practice before the "lazy" type (at the last minute to prepare before going to a subject in need), then the present approach is "hard" type, no matter what you want to back, all I have downstream target ahead of you get ready. Well, since ideas there, so how do we want it? First of all, how do you know which objects are downstream? Second, you put these downstream object construction, temporarily do, where to put it? Finally, when you use these objects in the upstream, how to get to use it?

This problem leads to three spring / spring-boot solutions. Dependency Injection .

The first question: How do you know which objects are downstream?

Spring / Spring-Boot solution in two ways: xml configuration or annotation configuration.

 

The second question problem: These downstream objects created, to put where?

bean management

 

The third question: how to pick up objects upstream with downstream objects?

@Autowired

@Resouce

Guess you like

Origin www.cnblogs.com/greatLong/p/11896911.html