
Interview site design model awareness: the interviewer wanted to hear not just a few of your models, but also to know if you could match the design models to spring's concrete realization, rather than general. Source-reading depth: to be able to specify the corresponding components of each design mode in spring, you do read the source code, not just the eight parts. Interviewers are particularly concerned about this. Theoretically linked: see if you understand why these design models are used, what problems are solved, and whether you can use them in your own project. Core answer
The design models used in the spring framework are very numerous, with the nine most frequently examined in interviews:
Design mode
Typical application in spring
A statement
Plant mode
Beanfactory
Applicationcontext
Cover the creation of objects
Single case mode
The default field for bean
There's only one example of the entire container
Proxy mode
Aop (jdk dynamic agent / cglib)
You can do better without changing the source code
Template method mode
Jdbctemplate
Rettemplate
Fixed skeleton. Variable steps left to the subclass
Observer mode
Eventsevent
One release, multiple listening
Adapter mode
Handler adapter
Messageconvert
Let incompatible interfaces work together
Policy mode
Resources
Interface, instantiationstrategy
Same interface, different implementation strategies
Chain of responsibility model
Filter
Chain, interceptor
Request sequenced along the chain
Decorator mode
Beanwrapper
Htprequestdecorator
Dynamically enhanced object function
Here are some of the core opening remarks。
Depth resolution
I. The plant model (the heart of spring)
The plant model is the cornerstone of spring. You write @component, @service every day, do you think who created these objects? Beanfactory。

The chart above shows the hierarchical structure of the spring factory system:
This is the core idea of the plant model: to encapsulate the creation logic of the object inside the plant, and the caller does not need to care about how the target came out。
Ii. Single case mode (the default domain for spring bean)
Spring's single example is not the traditional one of "writing a private constructor + example" but a single example of container level - a container that ensures that the same id bean is created only once。
/ springNbeanregistry source simplified)
i'm not sure if i'm going to be able to do thisNo, no, no, no.
/ first class cache: store a fully initialized single case, bean
i don't knowSingleto*objects = new concurent hashmap();
public object get singleton
subject singapore = singaporeobjects. Get (beanname);
if (singleton = null)
singleton = cratebean (beanname);
singletonobjects. Put (beanname, singapore);
♪ i'm sorry ♪
i don't know.
♪ i'm sorry ♪
♪ i'm sorry ♪
Spring uses a level-iii cache to solve the problem of single-case bean loop dependence, which is frequently followed up in interviews with individual models. You can see if you're interested
DefaultsingletoN bean registry's source code, three maps in there, clearly。
Additional sentence: the single case of spring is non-linear security. Because there is only one example of bean in the container, if there is a variable state in bean, there is a problem with multiple threads combined with access. So spring officially recommends bean as a stateless design。
Iii. Proxy mode (aop soul)
This should be the most familiar. Two ways to achieve spring aop - jdk dynamic agent and cglib agent - are essentially proxy models。
/ / jdk dynamic agent example (simplified rationale for spring aop)
public class jdkproxydemo addresses invocatioNhandler
i'm sorry. Public object
this. Target =target;
i'm sorry, sir.
@taget. Getclass(). Getclassloader()
i'm sorry. This
;
♪ i'm sorry ♪
@override
subject program, method method, object [ ] args] throws throwable
/ pre-notification
system. Out. Println("before: "+ method. Getname());
/ approach to the implementation of the objectives
subject result = method. Infoke (target,args);
// back notice
system. Out. Println ("after: "+ method. Getname());
i'm sorry.
♪ i'm sorry ♪
♪ i'm sorry ♪
The essence of the proxy model is that the function is enhanced without modifying the original code. Whether it be a journal, a matter, a control of authority, it can all be woven through agents, which is the basis on which aop can be built。
Iv. Template methodology model (jdbctemplate)
The template method mode is very useful in spring, but it's basically the template。
// jdbctemplate
jdbctemplate. Query?
(rs, row num) - {/ ← you just need to focus on this part. User user = new user();
our. Setid (rs. Getlong("id"));
user. Setname (rs. Getstring("name"));
i'm sorry.
♪ i don't know ♪
1l / / parameter
;
/ / connection access, statement creation, anomaly processing, resource shutdown -- all done by templates
The template method model is designed to define the algorithm skeleton (fixed process) in the parent category and the variable steps in the subcategory. Jdbtemplate has all the cumbersome try-catch-finally process of jdbc sealed, and all you have to do is tell it what sql is and how the results are mapped, and nothing else matters。
The same is true of resttemplate, jmstemplate, redistemplate。
V. Observation methodology (incident mechanism)
The spring event mechanism is the standard observer model: an event publisher and an n event listener。
Defining events
public class ordercreatedevent excepts applicatioEvent
i don't know. Public ordercreateevent
suber(source);
this. Order = order;
♪ i'm sorry ♪
♪ i'm sorry ♪
publication of events
@service
i'm sorry, public class orderservice
@autowired
i don't knowI don't know. Public void createorder
/ business logic...
(a) public. Publicevent (new ordercreateevent, this, order);
♪ i'm sorry ♪
♪ i'm sorry ♪
/ 3. Interceptions (can have more than one monitor)
@compoI don't know. This post is part of our special coverage syria protests 2011.
@eventlistener
i don't knowOther organiser
// mail notification
♪ i'm sorry ♪
♪ i'm sorry ♪
@compoI don't know. This post is part of our special coverage global voices 2011.
@eventlistener
i don't knowOther organiser
/ point incentive
♪ i'm sorry ♪
♪ i'm sorry ♪
This model is very useful in practical projects. The next order is followed by texting, sending points, updating inventory... If it's all written in one way or another, it explodes. Once decoupling with the event mechanism, it's all different, and it doesn't affect each other。
Vi. Adaptor mode (spring mvc core)
Handler adapter is the typical adapter mode in spring mvc。

The logic of the above figure is:
This is the value of the adapter model: to allow the previously incompatible interface to work together。
Vii. Strategic modalities
There are many applications of strategic models in spring, such as resource loading:
/ /resource interfaces have multiple implementation strategies
resources source1 = new classpathresource("config. Xml");/ class path
resources source2 = new filesystemresource("/etc/config. Xml"); // filesystem
resources source3 = new urlresource("https://example. Com/config. Xml"); ///
/ abstractapplicatioResource loading strategy in ncontext
public information resources loader
resources get resources;
♪ i'm sorry ♪
The caller is programmed only for the resource interface, and the bottom is determined by the specific strategy, whether it is a class path, a disk or a web-based one。
Hf interviews
Nope. Spring ensures only one example in the container, but no thread is safe. Bean needs to deal with co-opt problems if it contains a variable state (e. G. A member variable). So spring officially recommends bean as a stateless design。
Singletonobjects (class i), earlysingletonobjects (class ii), singleton factories (class iii). Level 3 caches are primarily designed to address the creation of agents in a circular dependency scenario. Not all cycling dependence can be solved — the tectonic injection of cycling dependence can be compromised。
The simple factory decided which product to create through parameters, in violation of the closure principle. Spring uses the plant methodology model (each beandefinition corresponds to a creation logic) and the abstract plant model (the beanfactory interface has multiple realizations), which is more extensive。
Common interview mutation memory
• “systems of work, suitable for handling” - factories, individual cases, agents, templates, observers, adapters, strategies, liability chains, decorators. 9 large pattern swallows。
Summary
The spring framework uses almost every design model of the gof 23. The interview focused on the plant model (beanfactory), the single model (bean domain), the proxy model (aop), the template methodology model (jdbctemplate) and the observer model (incident mechanism) would be sufficient to satisfy the interviewer. The key is that each model responds to the specific realization components of spring, not the dryback concept。










