Use AOP to write 2 PC frameworks (I), and use aop to write 2 PC frameworks
I don't really want to write this series, because this 2 PC is a little tricky to write a small architecture separately. But I don't know what to write. Let's write it first.
The entire process is as follows:
There are many articles about the AOP series. I will repeat them here.
First, we define an IAopProxy interface used to create a Proxy instance for AopProxyFactory. The Code is as follows:
Public interface IAopProxy {AopProxyBase CreateAopProxyInstance (externalbyrefobject obj, Type type );}View Code
The most important part of AOP interception is the RealProxy class. We write an AopProxyBase abstract class that inherits from RealProxy. The Code is as follows:
Public abstract class AopProxyBase: RealProxy {private readonly extends albyrefobject target; // default transparent proxy public AopProxyBase (export albyrefobject obj, Type type): base (type) {this.tar get = obj ;} public override IMessage Invoke (IMessage msg) {IMethodCallMessage call = (IMethodCallMessage) msg; bool isIntercept = false; var attrs = call. methodBase. getCustomAttributes (typeof (aopmethodattrites), false) As AopMethodAttribute []; // records only when AopMethodAttribute is marked. If (attrs. length> 0) {isIntercept = true;} if (isIntercept) {this. before (msg, attrs);} // if the constructor is triggered, IConstructionCallMessage ctor = call as IConstructionCallMessage is not started for target construction; if (ctor! = Null) {// obtain the underlying default real proxy RealProxy default_proxy = publish get); default_proxy.InitializeServerObject (ctor); MarshalByRefObject tp = (MarshalByRefObject) this. getTransparentProxy (); return response iseserviceshelper. createConstructionReturnMessage (ctor, tp);} IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.tar get, call); if (isIntercept) {this. after (msg, result_msg, attrs);} return result_msg;} public abstract void Before (IMessage requestMsg, AopMethodAttribute [] attrs); public abstract void After (IMessage requestMsg, IMessage Respond, aopmethodattris [] attrs );}View Code
At the same time, we define AopAttribute: ProxyAttribute. The Code is as follows:
[AttributeUsage (AttributeTargets. class)] public class AopAttribute: ProxyAttribute {IAopProxy proxy; public AopAttribute (Type factoryType) {this. proxy = (IAopProxy) AopProxyFactory. createInstance (factoryType);} public override implements albyrefobject CreateInstance (Type serverType) {export albyrefobject target = base. createInstance (serverType); AopProxyBase rp = this. proxy. createAopProxyInstance (target, serverType); return (export albyrefobject) rp. getTransparentProxy ();}}View Code
We can see that in the AopAttribute constructor, an instance of the intercepted Class is created through the Factory to avoid being created every time. I added a Dictionary as the Cache and the code is as follows:
Public class AopProxyFactory {private static AopProxyCache _ proxyCollection; private static readonly object _ syncObject = new object (); static AopProxyFactory () {lock (_ syncObject) {if (_ proxyCollection = null) {_ proxyCollection = new AopProxyCache () ;}} public static IAopProxy CreateInstance (Type type) {return _ proxyCollection [type] ;}} public class AopProxyCache {public Dictionary <Type, IAopProxy> _ proxys; private static readonly object _ syncObject = new object (); public AopProxyCache () {lock (this) {if (_ proxys = null) {_ proxys = new Dictionary <Type, IAopProxy> () ;}} public void Add (Type type, IAopProxy proxy) {if (_ proxys = null) throw new ArgumentNullException ("proxys is not init"); lock (_ syncObject) {this. _ proxys [type] = proxy;} public IAopProxy Get (Type type) {IAopProxy proxy; if (this. _ proxys. ContainsKey (type) {proxy = this. _ proxys [type];} else {lock (_ syncObject) {if (! This. _ proxys. containsKey (type) {proxy = (IAopProxy) Activator. createInstance (type); this. add (type, proxy);} else {proxy = this. _ proxys [type] ;}}return proxy;} public IAopProxy this [Type type] {get {return this. get (type) ;}set {this. add (type, value );}}}View Code
Here, the basic classes of Aop are set up. After the specific interception, what to do should be inherited from our abstract class AopProxyBase, and then the After and Before should be rewritten to implement some interception, recorded work.
Implement permission Control Using Spring AOP
You can use the java Dynamic proxy.
We recommend that you have a good look at the proxy mode.
What is AOP programming?
Aspect-Oriented Programming (also called Aspect-Oriented): Aspect Oriented Programming (AOP) is a hot topic in software development and an important part of Spring framework. AOP can be used to isolate all parts of the business logic, thus reducing the Coupling Degree between each part of the business logic, improving the reusability of the program, and improving the development efficiency. AOP is a continuation of OOP. It is short for Aspect Oriented Programming. It refers to Aspect-Oriented Programming.
The pre-compilation method and runtime dynamic proxy can be used to dynamically and uniformly add functions to the program without modifying the source code. AOP is actually a continuation of the GoF design model. The design model tirelessly pursues decoupling between callers and callers. AOP can be said to be an implementation of this goal.