Originally updated every day, I generally like to spend at 12 o ' night when the article, the result is April Fools? The school network is also very interesting, broken, I was fooled ... All right... Start today's topic AOP. AOP is so important, so in the second article, AOP is an important concept in spring. If this does not understand the web development of the three main framework of the principle, then hehe. Often hear classmates and netizens talk about the web programmer most of the time is in the test skin XML configuration, I was also drunk, so I have to study the web, in fact, the design patterns, algorithms, architectural ideas embodied in the source code in the incisively and vividly ah, a big treasure-house unexpectedly turned a blind eye pity. Here's a taste ...
1. Static Proxy
Do not be the design mode on the tall name to frighten, in fact, static agent is quite easy to understand, with the life of the scene to understand static agent is: Trustee Affairs. If a company wants to run a concert, where to find the star? Star of the agent Ah, star who have time to talk with the company AH. So this broker is a key role in static proxies. Take a look at a UML diagram and see the truth:
Where target can be understood as the star, Targetinterface can be understood as the star of the extended standard of appearances, Targetproxy can be understood as brokers. Brokers are talking about prices and other things, as well as the company's star appearances.
Use the above ideas in the Web if there is a scenario where you delete or modify user information, you need to open the transaction in this process. Simulate this scenario with a static proxy.
This time the proxy object became Userdaoimpl, and Userdaoproxy became the top broker (agent).
Specific code: GITHUB
In fact, the static agent here is still a great disadvantage is that if the star more, my agent multiplied, this time the code is too large. In order to overcome this problem, a dynamic agent is produced.
2. Dynamic Agent
Static proxy can be said to be a popular version of dynamic agents, dynamic Agent to solve the phenomenon of more stars, brokers (agents), while optimizing the efficiency of the Code. Thought is similar, the key in the implementation of the code when the gap is still quite large. In Java, dynamic agents typically have JDK agents (the functionality provided by the JDK itself), cglib agents, ASPECTJ, and so on.
Specific code implementation look here. The difference between the following and the static agent is pulled out:
Public class proxyinterceptor implements Invocationhandler { //Agent target PrivateObject Target;//Import Transactions PrivateTransaction TX; Public Proxyinterceptor(Object target,transaction TX) { This. target = target; This. tx = TX; }@Override PublicObjectInvoke(Object Proxy, Method method, object[] args)throwsThrowable {//Open transaction This. Tx.begintransaction (); Method.invoke (target, args);//Commit a transaction This. Tx.committransaction ();return NULL; }}
Where private Object target ;
declared as an object type means that you can accept any type of proxy object (a variety of stars) This solves the problem of fixed proxy objects in the dynamic agent. At the same time the agent (broker) to implement an interface InvocationHandler
this interface is why? The meaning is understood as the handler of the event, and then the Invoke method in the interface is rewritten and, regardless of the parameters in the method, is understood here as an important way for the agent to handle the transaction. The business is called in this method. can be achieved. The dynamic agent solves the problem of dealing with the diversity of proxy objects. So what is AOP?
3. AOP for slice-oriented programming
What is AOP? With the base of the above agent, an example can be used to explain what is aspect-oriented programming AOP. Case background: Now to withdraw money, the process of taking money before the ATM machine to turn on the log function, transaction functions, security checks and so on. The code is here. Here are the most important proxy objects:
PackageORG.KYLIN.AOP;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;/** * @author unikyln * @date 11:30:19 March 31, 2015 * @blog: http://blog.csdn.net/ Unikylin * @github: https://github.com/unikylin/* @description Dynamic Agent class */ Public class drawmanagerproxy implements Invocationhandler { //Agent target class PrivateObject Target;//Various facets in face-slicing programming PrivateLogger Logger;PrivateSecurity security;PrivateTransaction TX; Public Drawmanagerproxy(Object target,logger logger,security security,transaction tx) { This. target = target; This. Logger = logger; This. Security = security; This. tx = TX; }@Override PublicObjectInvoke(Object Proxy, Method method, object[] args)throwsThrowable {//Start log This. Logger.logger ();//Start transaction This. Tx.begintransaction ();//Start security check This. security.checksecurity ();//Withdraw moneyMethod.invoke (target, args);//Commit a transaction This. Tx.committransaction ();return NULL; } }
Start the log, open the transaction, and then commit the transaction before the money is taken. You can use the following diagram to understand how this process is happening?
The previous article already mentions this diagram. It can be understood that the logging, transaction, and security framework before the execution method are facets in AOP, whereas the notifications in AOP can be understood as the two methods of opening transactions and committing transactions in a transaction (the method in the slice can be understood as a notification). So you can discover that transactions, logs, security frameworks, and money fetching are completely non-coupled, and do business entirely by proxy classes. In the actual development of each person to develop the function of each person, similar to the development of security framework can be independently developed, in time to withdraw money after the need to print a small ticket, you can also add a facet to achieve this function. Then use the idea of AOP to combine these together to form a complete program.
The idea of AOP design for 4.STRUTS2 application
Let's take a look at the overall architecture diagram of STRUTS2:
A lot of interceptor can be found, and these interceptors are very important in Struts2, which can be understood as facets, and Actionproxy is the equivalent of proxies linking these interceptors with the target method. In fact, Struts2 's interceptor design is implemented using AOP, and the exception handling and interception when STRUTS2 and spring are integrated are also implemented using the AOP idea. AOP has gone deep into the core design concept of the framework. So much for today, the next section is about Servlets and how to write a small, Struts2-like framework with the Servlet native API to simplify the problems encountered in web development.
Very happy to exchange study with everybody
Free reprint, creative license, please indicate the source of the article, from here
(Http://blog.csdn.net/unikylin)
Facet-oriented programming AOP based on static agents and dynamic proxies in Web projects