Spring Framework
The Spring Framework is created due to the complexity of software development。 Spring is using thethe basic JavaBeanTo accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. Fromsimplicity, testability and loose couplingPerspective, most Java applications can benefit from spring. Objective: To solve the complexity function of enterprise application development: Use basic JavaBean instead of EJB and provide more enterprise application function scope: Any Java application spring is a lightweightControl Reversal(IoC) andFace Tangent(AOP) Container framework.
Spring IOC:
Control inversion (inversion of CONTROL,IOC), also known as Dependency Injection (Dependency Injection,di), is a design concept in object-oriented programming
Dependency Injection (spring has only set accessor injection and construct injection ):
1.set accessor injection (printer example)
01. Architecture
02. The introduced Jar package
03. Interface and Implementation Class code
Package Cn.ink; /* */Publicinterface Ink {public String getColor () ;}
package Cn.ink; /* * Gray Cartridge class */ public class Grayink implements ink{@Override public String GetColor () { //
TODO auto-generated Method stub
return "Gray cartridge"
; }}
package Cn.ink; /* * Color Cartridge class */ public class Colorink implements ink{@Override public String GetColor () { //
TODO auto-generated Method stub
return "Color cartridge"
; }}
package Cn.paper; /* * paper interface */ public interface Paper { public String getcontent (); }
package Cn.paper; /* * A4 paper */ public class A4paper implements paper{@Override Span style= "color: #0000ff;" >public String GetContent () { // Span style= "color: #008000;" > TODO auto-generated Method stub return "a calm A4 paper" ; }}
Package Cn.paper; /* */Publicclassimplements paper{ @Override public String getcontent () { // TODO auto-generated method stub return " A calm B5 paper "; }}
PackageCn.printer;/** Printer Class*/ImportCn.ink.Ink;ImportCn.paper.Paper; Public classPrint {Privateink Ink; PrivatePaper Paper; Public voidprint () {System.out.println ("Use" +ink.getcolor () + "Print content on" +paper.getcontent () + "); } PublicInk Getink () {returnInk; } Public voidsetink (Ink ink) { This. Ink =Ink; } PublicPaper Getpaper () {returnpaper; } Public voidSetpaper (Paper Paper) { This. paper =paper; } }
04.application.xml Configuration
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "Colorink"class= "Cn.ink.ColorInk"/> <BeanID= "Grayink"class= "Cn.ink.GrayInk"/> <!--initialization of the paper - <BeanID= "paper"class= "Cn.paper.A4Paper"/> <!--Configure the printer - <BeanID= "printer"class= "Cn.printer.Print"> < Propertyname= "Ink"ref= "Colorink"></ Property> < Propertyname= "paper"ref= "paper"></ Property> </Bean> </Beans>
05. Test Class
Packagecn.test;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCn.printer.Print; Public classPrintexam {@Test Public voidTestprint () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Print Printer= (Print) context.getbean ("printer"); Printer.print (); } }
2. Interface Injection
3. Construction Injection
IOC Definitions and benefits :
The IOC (inverse of control) inversion is the kernel of the spring container , and the functions of AOP, declarative transactions, and so on are based on this. The so-called IOC, through the container to control the dependencies between business objects, rather than traditional implementations, directly controlled by the code . This is the concept of "inversion of Control", where control is transferred from the application code to the external container, and the transfer of control is reversed. The benefit of control transfer is to reduce the degree of dependence between business objects .
Two. Spring AOP:
The following example is a good explanation of why AOP is to be used. First, according to the idea of software refactoring, if the same code appears in multiple classes, consider defining a common abstract class that extracts the same code into the abstract class. For example, Horse, pig, camel these objects all have the run (), eat () method, by introducing a animal parent class that contains these two methods abstractions, Horse, pig, camel can be multiplexed into the run () and animal () methods by integrating eat. The way to eliminate duplicate code in multiple classes by introducing a parent class is feasible in most cases, but the world is not always as simple as the scenic management business class shown below.
Guo No link: http://www.zhihu.com/question/19993030/answer/77125144Source: Copyright belongs to the author, please contact the author for authorization. ImportCom.smart.dao.ViewPointDao;ImportCom.smart.dao.ViewSpaceDao;ImportCom.smart.domain.ViewSpace;/*** Service category of scenic spot management*/ Public classViewspaceservice {PrivateTransactionManager Transmanager; PrivatePerformanceMonitor Pmonitor; PrivateViewspacedao Viewspacedao; PrivateViewpointdao Viewpointdao; /*** Add a tourist attraction * *@paramViewspace*/ Public voidaddviewspace (Viewspace viewspace) {Pmonitor.start (); Transmanager.begintransaction (); Viewspacedao.addviewspace (Viewspace); Transmanager.endtransaction (); Pmonitor.end (); } /*** Delete an attraction * *@paramPointid*/ Public voidDeleteviewpoint (intPointid) {Pmonitor.start (); Transmanager.begintransaction (); Viewpointdao.deleteviewpoint (Pointid); Transmanager.endtransaction (); Pmonitor.end (); }}
Where Pmonitor is the method performance monitoring code that starts before the method call, ends before the method call returns, and internally records the result information for performance monitoring. Where Transmanager code is the code for Transaction initiation and transaction submission, we find that our business code is submerged in repetitive non-business code, and performance monitoring and transaction management of these non-business code Pueraria surround the business code. At this point we cannot eliminate the repetitive code by abstracting the parent class, because the logic is attached to the process of the business class method and they cannot be moved elsewhere. So the idea of AOP was created.
Advantages: AOP is mainly to provide another kind of programming idea, can pull out similar behavior to unify processing.
Summary supplement:
The IOC is decoupled and provides a way to construct objects so that a function can be implemented in many ways and does not constitute a dependency. The disadvantages of Spring's IOC: more memory, slower construction, slower startup. SPRINGAOP is an enterprise-class solution for cutting-plane programming based on the spring framework, although this is already powerful, but still not AspectJ strong, such as: currently does not support process slices. Weaving into slow execution is inefficient and does not support the cut-in of objects created by the new keyword, and must be woven using bean factory.
Spring in-depth understanding of AOP and IOC