This article mainly includes the entire idea of the Spring framework.
1. IOC (Reverse control) and dependency Injection
2. Aspect-Oriented Programming
1. IOC (Reverse control) and dependency Injection
Refer to this articleArticle: Http://www.bccn.net/Article/kfyy/java/jszl/200610/4512.html
When IOC is used, the object passively accepts the dependency class, rather than actively looking for it. The container actively Injects its dependent classes into it during instantiation. It can be understood as follows: Control reversal transfers the initiative of the class to the interface, and dependency injection injects the dependent class during class instantiation through the xml configuration file.
2. Aspect-Oriented Programming
2.1 eclipse's Aspect-Oriented Programming development environment.
2.2 Introduction to Aspect-Oriented Programming
2.3 A simple AOP demo
2.1 eclipse Aspect-Oriented Programming Development Environment
Windows-> install new softwares-> Add a new website-> enter the Update site below: http://download.eclipse.org/tools/ajdt/36/update-> select all options.
Click Finish. After the installation is complete, restart eclipse and create a new Java project. The following results should be displayed:
2.2 Aspect-Oriented Introduction
In object-oriented programming, what can be solved is the relationship between a pair and a collection, which is implemented through inheritance policies.Code. However, there are also problems that OO cannot solve. When we want to introduce public behaviors to objects without class levels, the idea of OO cannot be solved, so we introduced Aspect-Oriented Programming.
Key Aspect-oriented concepts:
Join point:ProgramIn the execution process, advice is relative to this join point.
Advice: advice is the Execution Code of the join point, which is the "execution logic" of all aspects ".
Pointcut: A group of join points used to indicate where a suggestion is applied.
Introduction: add fields or methods for existing Java classes.
Before advice: Call before advice before calling the join point.
After advice: opposite to before advice.
2.3 simple AOP programming example
2.3.1 use j2se dynamic proxy to implement AOP
Client code:
/** <Br/> */<br/> package AOP. proxy; <br/> Import com.sun.org. apache. bcel. internal. generic. new; <br/>/** <br/> * traditional implementation, no AOP framework is used <br/> * @ author jefferyxu <br/> */<br/> public class businesslogicusage {<br/>/** <br /> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> ibusinesslogic businesslogic = <br/> (ibusinesslogic) loggingproxyaspect. BIND (securityproxyaspect. BIND (New businesslogiccoreconcern (); </P> <p> businesslogic. businessmethod1 (); <br/> system. out. println (); <br/> businesslogic. businessmethod2 (); <br/>}< br/>
Businesslogiccoreconcern. Java
Package AOP. proxy; <br/>/** <br/> * traditional approach, no AOP framework is used <br/> * @ author jefferyxu <br/> */<br/> public class businesslogiccoreconcern implements ibusinesslogic {<br/> @ override <br/> Public void businessmethod1 () {<br/> // todo auto-generated method stub <br/> docorebusiness1 (); <br/>}< br/> @ override <br/> Public void businessmethod2 () {<br/> // todo auto-generated method stub <br/> docorebusiness2 (); <br/>}< br/>/** <br/> * executes core business 1 <br/> */<br/> private void docorebusiness1 () {<br/> system. out. println ("docorebusiness1 "); <br/>}</P> <p>/** <br/> * perform core business 2 <br/> */<br/> private void docorebusiness2 () {<br/> system. out. println ("docorebusiness2"); <br/>}</P> <p >}< br/>
Ibusinesslogic. Java
/** <Br/> * @ author jefferyxu <br/> */<br/> package AOP. proxy; <br/> Public interface ibusinesslogic {<br/> Public void businessmethod1 (); <br/> Public void businessmethod2 (); <br/>}< br/>
Loggingproxyaspect. Java
/** <Br/> */<br/> package AOP. proxy; <br/> Import Java. lang. reflect. invocationhandler; <br/> Import Java. lang. reflect. method; <br/> Import Java. lang. reflect. proxy; <br/> import sun. AWT. geom. areaop. addop; <br/> import sun. util. logging. resources. logging; <br/>/** <br/> * @ author jefferyxu <br/> */<br/> public class loggingproxyaspect implements invocationhandler {<br/> private object proxyobj; </P> <p> @ override <br/> Public object invoke (Object proxy, method, object [] ARGs) <br/> throws throwable {<br/> // todo auto-generated method stub <br/> beforeadvice (method); </P> <p> Object object = method. invoke (proxyobj, argS); </P> <p> afteradvice (method); </P> <p> return object; <br/>}< br/> Public loggingproxyaspect (Object OBJ) {<br/> This. proxyobj = OBJ; <br/>}</P> <p>/** <br/> * generate a dynamic object through a dynamic proxy <br/> * @ Param object <br/> *@ return <br/> */<br/> Public static object BIND (Object object) {<br/> class CLS = object. getclass (); <br/> return proxy. newproxyinstance (CLS. getclassloader (), CLS. getinterfaces (), new loggingproxyaspect (object )); <br/>}</P> <p>/** <br/> * call pre-processing <br/> * @ Param method <br/> */<br/> private void beforeadvice (method) {<br/> logging ("before calling:" + method. getname ()); <br/>}</P> <p>/** <br/> * post-call processing <br/> * @ Param method <br/> */<br/> private void afteradvice (method) {<br/> logging ("after calling:" + method. getname (); <br/>}</P> <p> private void logging (string Str) {<br/> system. out. println (STR); <br/>}< br/>
securityproxyaspect. Java:
/** <Br/> */<br/> package AOP. proxy; <br/> Import Java. lang. reflect. invocationhandler; <br/> Import Java. lang. reflect. method; <br/> Import Java. lang. reflect. proxy; <br/>/** <br/> * @ author xuqiang <br/> */<br/> public class securityproxyaspect implements invocationhandler {<br/> private object proxyobj; </P> <p>/* (non-javadoc) <br/> * @ see Java. lang. reflect. invocationhandler # invoke (Java. lang. object, Java. lang. reflect. method, Java. lang. object []) <br/> */<br/> @ override <br/> Public object invoke (Object arg0, method, object [] ARGs) <br/> throws throwable {<br/> // todo auto-generated method stub <br/>/** <br/> * Only businessmethod1 is associated here <br/> */<br/> If (method. getname (). inclusignorecase ("businessmethod1") {<br/> beforeadvice (method); <br/>}</P> <p> Object object = method. invoke (proxyobj, argS); </P> <p> return object; <br/>}< br/> private void beforeadvice (method) {<br/> dosecuritycheck (); <br/>}</P> <p> private void dosecuritycheck () {<br/> system. out. println ("doing security check "); <br/>}< br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/>}</P> <p> Public securityproxyaspect (Object OBJ) {<br/> This. proxyobj = OBJ; <br/>}</P> <p>/** <br/> * generate an object proxy through a dynamic proxy <br/> * @ Param object <br/> *@ return <br/> */<br/> Public static object BIND (Object object) {<br/> class CLS = object. getclass (); </P> <p> return proxy. newproxyinstance (CLS. getclassloader (), CLS. getinterfaces (), new securityproxyaspect (object); <br/>}</P> <p >}< br/>
The above code is implemented using invocationhandler in Java. To put it simply:
The design of the proxy class uses the design idea of the proxy mode. The proxy class Object implements all interfaces of the proxy target, and replaces the target object for actual operations. However, this alternative is not a simple alternative,
This makes no sense. The proxy aims to enhance the method of the target object. The essence of this enhancement is to intercept the method of the target object. Therefore, proxy should include a method to intercept
To indicate what to do when the method call is intercepted. Invocationhandler is the interceptor interface. Http://blog.csdn.net/pizishuai2008/archive/2009/07/28/4385906.aspx ).
That is, when the interface implementation class or the interface calls the function in the interface, Java will automatically call the invoke (a function to be implemented in the invocationhandler Interface) function. By default, any function in the called interface will trigger the invoke function to be called, but the method name can be used in the function invoke to limit it, as shown in the above practice:
If (method. getname (). inclusignorecase ("businessmethod1") {<br/> beforeadvice (method); <br/>}
The following is a simple example of using invocationhandler:
Ianimal. Java:
Package proxytest; <br/> Public interface ianimal {<br/> void Info (); <br/>}
Dog. Java:
Package proxytest; <br/> public class dog implements ianimal <br/> {<br/> Public void Info () {<br/> system. out. println ("this is a dog! "); <Br/>}< br/>}
Client Program:
Package proxytest; <br/> Import Java. lang. reflect. *; <br/> public class proxytest {</P> <p> Public static void main (string [] ARGs) throws interruptedexception {<br/> final ianimal animal = new dog (); <br/> Object proxyobj = proxy. newproxyinstance (<br/> animal. getclass (). getclassloader (), <br/> animal. getclass (). getinterfaces (), </P> <p>/** <br/> * defines the method used to call the interface when the proxyobj object calls the interface <br/> */<br /> New Invocationhandler () <br/>{< br/> Public object invoke (Object proxy, method, object [] ARGs) <br/>{< br/> try {<br/> system. out. println ("blocked method:" + method. getname (); <br/> return method. invoke (animal, argS); <br/>}< br/> catch (illegalargumentexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/> return NULL; <br/>} catch (illegalaccessexception e) {<br // Todo auto-generated Catch Block <br/> E. printstacktrace (); <br/> return NULL; <br/>} catch (invocationtargetexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/> return NULL; <br/>}< br/>}); <br/> If (proxyobj instanceof ianimal) <br/>{< br/> system. out. println ("The proxyobj is an animal! "); <Br/>}< br/> else <br/> {<br/> system. Out. println (" The proxyobj isn't an animal! "); <Br/>}</P> <p> If (proxyobj instanceof dog) <br/>{< br/> system. out. println ("The proxyobj is a dog! "); <Br/>}< br/> else <br/> {<br/> system. Out. println (" The proxyobj isn't a dog! "); <Br/>}</P> <p> ianimal animalproxy = (ianimal) proxyobj; <br/> // animalproxy.info (); <br/> // animalproxy. hashcode (); </P> <p> system. out. println (animalproxy. getclass (). getname (). tostring (); <br/>}< br/>}
2.3.2 use aspectj to implement the above demo as follows:
Client Program:
/** <Br/> */<br/> package AOP. aspectj; <br/> Import AOP. proxy. *; <br/>/** <br/> * @ author jefferyxu <br/> */<br/> public class businesslogicusage {<br/> /* * <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> ibusinesslogic businesslogic = new businesslogiccoreconcern (); </P> <p> businesslogic. businessmethod1 (); </P> <p> system. out. println (); </P> <p> businesslogic. businessmethod2 (); <br/>}< br/>
Securityaspect. Java: a new aspect is added.
The Code is as follows:
/** <Br/> */<br/> package AOP. aspectj; <br/> Import AOP. proxy. *; <br/>/** <br/> * @ author jefferyxu <br/> */<br/> Public aspect securityaspect {<br/> private pointcut securityexecution (): <br/> execution (Public void ibusinesslogic. businessmethod1 (); <br/> // declare join point, define when to call the before and after functions </P> <p> // declare before advice <br/> before (): securityexecution () {<br/> dosecuritycheck (); <br/>}</P> <p> private void dosecuritycheck () {<br/> system. out. println ("doing security check"); </P> <p >}< br/>
Transcationaspect. Java:
/** <Br/> */<br/> package AOP. aspectj; <br/> Import AOP. proxy. *; </P> <p>/** <br/> * @ author xuqiang <br/> */<br/> Public aspect transcationaspect {<br/ >/** <br/> * define the start point <br/> */<br/> private pointcut transcationexecution (): <br/> execution (Public void ibusinesslogic. businessmethod1 () | <br/> execution (Public void ibusinesslogic. businessmethod2 (); </P> <p>/** <br/> * defines before advice <br/> */<br/> before (): transcationexecution () {<br/> Start (); <br/> }; </P> <p>/** <br/> * after advice <br/> */<br/> after (): transcationexecution () {<br/> commit (); <br/>}< br/>/** <br/> * simulate transacation <br/> */<br/> private void start () {<br/> system. out. println ("transcation start"); <br/>}</P> <p> private void commit () {<br/> system. out. println ("transcation commit"); <br/>}< br/>
Add the aspectj attribute to the entire project:
Run the entire project.