Spring Framework Tutorial-Basic concepts

Source: Internet
Author: User
Tags aop exception handling implement interface log return
Concept

One, what spring is

1 is an application framework that provides strong support for the development of applications, such as support for transaction processing and persistence, and so on.
2 is a bean container that manages the entire lifecycle of the Bean object, maintaining the various existing state of the bean, such as instantiating the Bean object, destroying it, single instance of the bean and multiple instance states, and so on.
3, a lightweight framework and container, compared to EJB

Second, the basic concept

1, lightweight container
1 can manage application code, but not intrusive or intrusive to the original code. For example, you do not need to implement a specific interface (EJB provides a cumbersome interface)
2 Boot Fast (EJB container startup is too slow).
3 When deploying a business object, no special steps are required (much more cumbersome when deploying EJBS).
4 managed applications can be ported to other environments with only a small amount of code, such as porting to a Web container, stand-alone client applications, and so on (the application implemented under the EJB model does not work at all in the EJB environment).

2,ioc

1 The IOC (inversion of Control), which is controlled inversion, refers to the control of the relationship between programs transferred from the application code to the external container.
In previous programs, the acquisition of these resources, such as data sources, was mostly done by the program itself, such as the following code might exist in a DAO:
Context Context=new InitialContext ();
DataSource ds= (DataSource) context.lookup ("java:/myds");
This.datasource=ds;
After the IOC is used, the acquisition of DataSource like the above is obtained by the IOC container and injected into the corresponding object.
2 the way in which a container injects an object into another object is usually two kinds: set value injection and construct child injection
* Set value injection, which means to inject an object into another object through the setter and getter methods, for example:
public class userdao{
Private DataSource DS;
Public Userdao () {}
public void Setdatasource (DataSource ds) {
This.ds=ds;
}
Public DataSource Getdatasource () {
return DS;
}
}
* Construct a child injection, which means to inject an object into another object through a constructor, for example:
public class userdao{
Private DataSource DS;
Public Userdao (DataSource DS) {
This.ds=ds;
}
}

3,aop

1 AOP (Aspect oriented programming), that is, aspect-oriented programming.
AOP is the extraction of the section in the process of business processing, which is faced with a step or a stage in the process of processing, to obtain the isolation effect of the low coupling between the parts of the logic process.
OOP (object-oriented programming) encapsulates the entities and their attributes and behaviors of the business process to achieve clearer and more efficient logical unit partitioning.

(2) As an example of a user logon log record, the user is required to log in the log when logging in the system (assuming the login method is login).
OOP approach:
Public Booean Login () {
......
Logger.info (username+ "Login time is" +new Date ());
......
}
AOP approach: Intercepting the process of user login (need to define interceptors), that is, when the login method executes, automatically triggers the interceptor, executing the interceptor's method, such as the Interceptor class that defines the login method
public class Loginintercepter implements methodintercepter{
Public Object Invoke (...) {
Logger.info (...);
}
}

3 Pointcut (PointCut) is an important concept in AOP that indicates in what circumstances the interception will be triggered.
<bean id= "Mypointcutadvisor"
class= "Org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
<property name= "Advice" >
<ref local= "Myinterceptor"/>
</property>
<property name= "Patterns" >
<list>
<value>.*do.*</value>
<value>.*execute.*</value>
</list>
</property>
</bean>

4) Processing mode (Advise) is also an important concept in AOP, which indicates the interception mode of interceptor.
Interception around advice provides preprocessing, post process support for Pointcut
Before advice, only for pointcut preprocessing.
Throws advice, only for exception handling during pointcut processing.
After returning advice, only pointcut after the return process.
Introduction Advice,spring is a special kind of advice, which is only oriented to the class level (unlike the above advice approach-oriented level). Through introduction advice we can implement class locking in multithreaded access.

5 The process p is a point described in the Pointcut (pointcuts)



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.