The core Spring Framework

Dependency Injection

DI inversion same as the control (IoC) meaning, but this is the same concept called two from two angles described.

IoC

After using the Spring framework, it is no longer an object instance is created by the caller, but to create a Spring container, Spring containers will be responsible for relations between the process control, rather than code directly controlled by the caller program. Such control is transferred to the Spring container, control reversal took place, this is the inversion of control.

OF

From the perspective of Spring container point of view, Spring container will be responsible for dependent objects assigned to the member variables of the caller, which is equivalent to inject an instance of it depends on the caller, this is Spring's dependency injection

bean in scope

Here Insert Picture Description
Which singleton and prototype are the two most common scope (scope)

singleton

singleton Spring container is the default scope. When Bean of scope for Singleton, Spring container will only exist Bean instance a share. In this role under, spring can accurately know when the Bean is created, when initialization is complete, and when it was destroyed.

prototype

The need to maintain session state Bean should use the prototype. When using the prototype scope, Spring container will have to create a new instance for each request of the Bean. Examples of Bean's on to the client code to manage, Spring container will no longer keep track of their life cycle.

Bean's life cycleHere Insert Picture Description

Spring commonly used annotation

@Component: Spring Bean is used to describe, it is a generalization of the concept, represent only one component
@Repository: for identifying the data access layer class (Dao) of the Bean Spirng
@Service: for traffic type layer is denoted as the S weekday Bean cabinet
@Controller: for the category control layer (Controller) identified as in Spring Bean.
@Autowired: Bean for property variables, the method and structure property setter methods for labeling, with the processor corresponding to the annotation work done automatically configure the Bean.
@Resource: its role and Autowired the same. @Resource There are two important attributes: name and type. Spring will be resolved to the name attribute Bean instance name, type attribute resolves to the Bean instance type.
@Qualifier: @Autowired conjunction with annotation, will by default as modified by fitting Bean type Bean assembly instance name, the name of the instance Bean @Qualifier annotation specified by the parameters.

Spring AOP

AOP

Oriented Programming, the log records, transaction management, and the like detached from the business operation code.
1.Pointcut: cut point of intersection process sequence, i.e., a connection point which need to be addressed
2.Aspect: package for laterally inserting system functions (e.g., transaction log, etc.) classes
3.Joinpoint: during program execution of a tangent point
4.Proxy (agent): after the notification to the target object is dynamically created objects
5.Weaving (weaving): the section code into a target object, a process to generate a proxy object

JDK dynamic proxies

JDK dynamic proxy is achieved by java.lang.reflect.Proxy class, we can call newProxyInstance Proxy class () method to create a proxy object. For business class use interface, Spring will use the default JDK dynamic proxies to implement AOP.
The target object must implement the appropriate interface

CGLIB agent

CGLIB (Code Generation Library) is a high performance package open source code generator, which uses very bottom of the byte code technology to generate a subclass of the specified target class and subclass enhanced.

Spring support for transactions

In the actual development, database operation will involve the management of affairs, aims to provide a special API for transaction processing Spring.

Spring MVC workflow

Here Insert Picture Description
1. The user sends a request to the server through the browser, the request is intercepted Spring MVC front controller DispathcerServlet;
after 2.DispatcherServlet intercepting the request, it calls the mapper HandlerMapping processor
3. The processor according to the request URL mapping to locate specific a processor, an object processor, and a processor generates blocker (if any is generated) collectively returned to the DispatcherServlet
4.DispatcherServlet selects the appropriate HandlerAdapter is (processor adapter) by returning information;
5.HandlerAdapter Handler will be invoked and executed ( processor), where the processor means is a program written controller
class, also known as the rear end of the controller;
after 6.Controller executed, returns a ModelAndView object that will contain the name of the view or a view name and model;
7.HandlerAdapter ModelAndView object is returned to the DispatcherServlet;
8.DispatcherServlet will choose a suitable ViewReslover (solver view) according ModelAndView object;
after 9.ViewReslover resolved, returns the specific view (view) in the DispatcherServlet ;
10.DispatcherServ View let to render (model data coming filled view);
11. The view rendering results are returned to the client browser.

MVC commonly used annotation

@RequestMapping
Here Insert Picture Description

MVC data binding

Spring MVC requests according to different parameters of the client, the request information message is converted in a manner and to bind the controller class method parameters. Such a data request message and a background method parameters during connection establishment data is bound in Spring MVC

Simple Data Binding

The default data type binding
HttpServletRequest: acquiring request information by the request object;
the HttpServletResponse: response information in response to the processing;
the HttpSession: to give the object stored in the session by session object;
the Model / a ModelMap: the Model is an interface, a ModelMap an interface, role model is filled data fields to the request

@RequestMapping("/getDataByRedirect") 
public String getdataByRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException { return "redirect:getData"; }

Binding Simple data types
Simple data types of binding, refers to several basic data types in Java bindings, such as type int, String, Double and so on.

简单数据绑定 @RequestMapping("/selcetUSer2") 
public String selectUser(Integer id){ 
System.out.println("id:"+id); return "success"; 
}

If the parameter name in the front and back request parameter controller class names are not the same process, this leads to the background and can not bind correctly received
parameter distal request. Consider using Spring MVC annotation types @RequestParam to provide indirect data binding.
Here Insert Picture Description
Josn interaction
Here Insert Picture Description

Published 24 original articles · won praise 1 · views 551

Guess you like

Origin blog.csdn.net/qq_45366515/article/details/105093865