{"id":706,"date":"2020-09-29T00:02:23","date_gmt":"2020-09-28T21:02:23","guid":{"rendered":"https:\/\/aliyargunes.space\/blog\/?p=706"},"modified":"2020-09-29T00:02:23","modified_gmt":"2020-09-28T21:02:23","slug":"aspect-oriented-programming-notes-4-proxy-mechanism","status":"publish","type":"post","link":"https:\/\/aliyargunes.com.tr\/blog\/aspect-oriented-programming-notes-4-proxy-mechanism\/","title":{"rendered":"Aspect Oriented Programming Notes #4 &#8211; Proxy Mechanism"},"content":{"rendered":"\n<p>In a pure object-oriented sense we are calling a method of an object (Target object). That method has some code which is already in it. Aspect code has to run whenever we call this target method. How is it possible that some other code which is in some other class in some other method runs when we called this method, how is it even possible in a pure object-oriented paradigm. <\/p>\n\n\n\n<p>It&#8217;s not actually possible. Aspect oriented programming is a completely different type of programming and what we do in Spring is we use some of the object oriented concepts to achieve aspect oriented programming. The way Spring uses these concepts is using a proxy.<\/p>\n\n\n\n<p>We know proxy as a design pattern so what we do is instead of making a call to Class A, we make a call Class B and Class B internally makes a call to Class A but then it also adds some additional functionalities. This is something that Spring is doing behind the scenes for us. <\/p>\n\n\n\n<p>Let&#8217;s try creating our own FactoryService class for having  our own getBean() method:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class FactoryService {\n\t\n\tpublic Object getBean(String beanType) {\n\t\tif(beanType.equals(&quot;Bicycle&quot;)) return new Bicycle();\n\t\tif(beanType.equals(&quot;Car&quot;)) return new Car();\n\t\tif(beanType.equals(&quot;Truck&quot;)) return new Truck();\n\t\treturn null;\n\t}\n}\n<\/pre><\/div>\n\n\n<p>Instead of using the:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nApplicationContext ctx = new AnnotationConfigApplicationContext(SomeConfiguration.class);\nSomeClass sc = (SomeClass) ctx.getBean(&quot;someClass&quot;);\n<\/pre><\/div>\n\n\n<p>we are going to create a FactoryService instance and use that to get a bean. <\/p>\n\n\n\n<!--more-->\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nFactoryService fs = new FactoryService();\nBicycle b = (Bicycle) fs.getBean(&quot;Bicycle&quot;);\n<\/pre><\/div>\n\n\n<p>What we are seeing here is instantiation of an object that we have done ourselves. We are not using Spring so none of the aspects or annotations are coming into play here. It&#8217;s our own factory service which initializing a bean and then we are calling a method of that bean. Basically we have an implementation of a factory service. We have hard-coded the instantiation but this is it.<\/p>\n\n\n\n<p>Having implemented a factory service we will have a look at a primitive implementation of aspect oriented programming. <\/p>\n\n\n\n<p>Let&#8217;s say we have a <code>getBicycleName()<\/code> method as a target and we want a separate aspect method to be called whenever a <code>getBicycleName()<\/code> (target) method needs to be run. How would we do that in our primitive implementation of a factory bean.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class LoggingAspect {\n\n        public void loggingAdvice() {\n          System.out.println(&quot;Logging from the advice&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<p>We want the above <code>loggingAdvice()<\/code> method to be executed whenever we run <code>getBicycleName() <\/code>on our <code>Bicycle <\/code>object that we have got from the <code>FactoryService<\/code>. The way Spring works is by creating a proxy object. Also, we know that bean that we need to pass back whenever a <code>fs.getBean(\"Bicycle\") <\/code>called has to be of type <code>Bicycle<\/code>, because we are casting it to <code>Bicycle<\/code> and we are assigning it to a local variable over here. So, the return type  of this <code>getBean(\"Bicycle\")<\/code> has to be type of <code>Bicycle<\/code>. <\/p>\n\n\n\n<p>What we do is to add extra functionality instead of returning an instance of Bicycle class, we extend this class and then return an instance of that subclass which will be an extension of Bicycle class.<\/p>\n\n\n\n<p>We need to create a new class that extends this <code>Bicycle<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class BicycleProxy extends Bicycle {\n    return super.getBicycleName();\n}\n<\/pre><\/div>\n\n\n<p>instead of returning an instance of <code>Bicycle<\/code> class, we had return an instance of the <code>BicycleProxy<\/code>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class FactoryService {\n\t\n\tpublic Object getBean(String beanType) {\n\t\tif(beanType.equals(&quot;Bicycle&quot;)) return new BicycleProxy();\n\t\tif(beanType.equals(&quot;Car&quot;)) return new Car();\n\t\tif(beanType.equals(&quot;Truck&quot;)) return new Truck();\n\t\treturn null;\n\t}\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">What is the advantage of doing this, not passing the Bicycle itself? <\/h2>\n\n\n\n<p>Since we have a new method here, we can make additional calls before we call the target method. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class BicycleProxy extends Bicycle {\n    new LoggingAspect().loggingAdvice();\n    return super.getBicycleName();\n}\n<\/pre><\/div>\n\n\n<p>This acts as a <code>@Before<\/code> advice and gets executed before the actual call to the target method. This is our own implementation of <code>@Before<\/code> advice. Of course we have taken away lots of complexity, this happens dynamically in Spring, but we have statically write the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a pure object-oriented sense we are calling a method of an object (Target object). That method has some code which is already in it. Aspect code has to run whenever we call this target method. How is it possible &hellip; <a href=\"https:\/\/aliyargunes.com.tr\/blog\/aspect-oriented-programming-notes-4-proxy-mechanism\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,8],"tags":[97,110,96],"class_list":["post-706","post","type-post","status-publish","format-standard","hentry","category-spring-framework","category-works","tag-aspect-oriented-programming","tag-dynamic-proxy","tag-spring-framework"],"_links":{"self":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/706"}],"collection":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=706"}],"version-history":[{"count":34,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/706\/revisions"}],"predecessor-version":[{"id":740,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/posts\/706\/revisions\/740"}],"wp:attachment":[{"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aliyargunes.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}