在上篇博客中个,咱们一起组建了一个容器,里面封装了业务,这样,咱们就将业务和服务的组装放到了客户端,而客户端就相当于咱们的开发中使用到的配置文件,大家发现问题了吗?就是我不能动态改动了?业务容器在代理完成后,重新添加一个容器,这时候,是不被允许的,那这个容器就是个编译时起作用的容器,他就丧失了很多的灵活性! 那怎么办呢?实际就是调换一下调用顺序就好,大家看看结果:
1, class diagram comparison
改造后类图
2, Code implementation: 2.1 Proxy class changes:
/** * Hello Dynamic proxy class, add function to business class * Previous version for JDK proxy implementation * This add execution method before printing to console ' befor ' * This time add execution method after printing to console ' after ' * This version is dglib proxy * For proxy class reasons, J The DK proxy requires that the proxy class must implement an interface, because its underlying implementation is to create a new class that implements the same interface as the proxy class * Replace the original business class with the new business class of the proxy class * Cglib Proxy is a new class, inherit from the proxy class, replace the original business class with the new proxy class, do not need interface * Version 5.0 adds the Service assembly container, removes the service from the proxy class, and our proxy class becomes a bean *6.0 defines the service container as an interface *7.0 increases the business container *8.0 the business container is not flexible, will get the agent to the business container side, realize the dynamic increment element, also can get the agent * @author Xu Yu * @version 3.0.0, June 16, 2015 PM 3:20:13 * * Public class cglibdynamicproxy implements methodinterceptor { //Service class container PrivateIproxymehds Proxymehds;//Agent factory Class: Single mode, optimized memory overhead Private StaticCglibdynamicproxy instance =NewCglibdynamicproxy ();//Constructors Private Cglibdynamicproxy() { }//Get Cglib Agent factory class Public StaticCglibdynamicproxygetinstance() {returnInstance }/** * Using a proxy factory to generate a proxy for a class * * @param CLS to proxy class * @return return a class that has already been proxied */ @SuppressWarnings("Unchecked") Public<T> TGetProxy(class<t> CLS) {return(T) enhancer.create (CLS, This); }//override method Execution of proxied object //All method execution, to the level of reflection is invoke, override this method, rewrite all method execution, implement the agent @Override PublicObjectIntercept(Object target, method method, object[] args, Methodproxy proxy)throwsThrowable {//Important improvements: Execute the method from the service container, no longer write dead! Proxymehds.beforebean ();//Method Normal execution of the statementObject result = Proxy.invokesuper (target, args);//Important improvements: Execute the method from the service container, no longer write dead! Proxymehds.afterbean ();returnResult }//Service container get method PublicIproxymehdsGetproxymehds() {returnProxymehds; }set method for the//service container Public void Setproxymehds(Iproxymehds Proxymehds) { This. Proxymehds = Proxymehds; }}
2.2 Business Container
/** * Business class Container: * Business class to cut into service with map * * @author Xu Yu * @version 3.0.0, 2015-6-29 14:25:21 * Public class Domehdsimplements Idomehds { //A map of the execution business PrivateHashmap<string,object> Dobeans;//proxy class PrivateCglibdynamicproxy proxy;/** * Encapsulates the method that gets the element, each time the fetch element encapsulates a proxy * * @param element name * @return proxy good element */ PublicObjectGetbean(String beanname) {returnProxy.getproxy (Dobeans.get (beanname). GetClass ()); }//Get Business map PublicHashmap<string, object>Getdobeans() {returnDobeans; }//Set up a business map Public void Setdobeans(hashmap<string, object> Dobeans) {Dobeans = Dobeans; }//Get proxy class PublicCglibdynamicproxyGetProxy() {returnProxy }//Set proxy class Public void SetProxy(Cglibdynamicproxy proxy) { This. Proxy = proxy; }}
2.3 Client
/** * Client Executing agent * * @author Xu Yu * @version 3.0.0, June 16, 2015 PM 3:18:42 */ Public class Client { //Client Execution Main method: System Regulation Public Static void Main(string[] args) {//The container of the object before the method is executedHashmap<string,object> Beforbeans; Hashmap<string,object> Afterbeans;what to do before the preparation method is executedHashmap<string,string> Beformethods; Hashmap<string,string> Aftermethods;service class map defined above//instancebeformethods=NewHashMap (); beforbeans=NewHashMap (); aftermethods=NewHashMap (); afterbeans=NewHashMap ();//Add the service class AspectClass1 to the method execution service class to be loadedBeforbeans.put ("AspectClass1",NewAspectClass1 ()); Beforbeans.put ("AspectClass2",NewAspectClass2 ()); Afterbeans.put ("ASPECTCLASS3",NewASPECTCLASS3 ()); Afterbeans.put ("ASPECTCLASS4",NewASPECTCLASS4 ());//Specify the method to be executed for each service classBeformethods.put ("AspectClass1","SayHello"); Beformethods.put ("AspectClass2","Saygoodbye"); Aftermethods.put ("ASPECTCLASS3","Sayhi"); Aftermethods.put ("ASPECTCLASS4","Eat");//Instance service class container, four maps into the containerProxymehds Proxymehds =NewProxymehds (); Proxymehds.setbeforbeans (Beforbeans); Proxymehds.setbeformethods (Beformethods); Proxymehds.setafterbeans (Afterbeans); Proxymehds.setaftermethods (Aftermethods);//Instance Business mapHashmap<string,object> Dobeansmap =NewHashmap<string,object> ();//load the business into mapDobeansmap.put ("Dobeans1",NewGreetingimpl ()); Dobeansmap.put ("Dobeans2",NewEatclass ());//Load a business map into a containerDomehds Domehds =NewDomehds (); Domehds.setdobeans (DOBEANSMAP);//instance proxy classCglibdynamicproxy cglib =cglibdynamicproxy.getinstance ();//Accept facetsCglib.setproxymehds (PROXYMEHDS); Domehds.setproxy (Cglib);//Accept the object to be proxiedGreeting greeting = (greeting) Domehds.getbean ("Dobeans1"); Eatclass Eatobject = (eatclass) Domehds.getbean ("Dobeans2");//Execute a method of an objectGreeting.sayhello ("Jack"); Eatobject.eat (); } }
3, Summary
代码的灵活,就像生活一般,在代码灵活的过程中,我们就会对比下生活,生活中,我们每个人,事,物都是独立的对象,而在生活中的时间轴中,我们也是过程化的组织,我们的代码和生活,在思想上都是相同的,优化的不仅仅是代码,还有思想啊!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Architecture Decryption-real-time dynamic AOP