{"id":1031,"date":"2009-07-20T15:08:07","date_gmt":"2009-07-20T14:08:07","guid":{"rendered":"http:\/\/fiber-space.de\/wordpress\/?p=1031"},"modified":"2009-07-20T15:08:07","modified_gmt":"2009-07-20T14:08:07","slug":"a-simple-spring-challenge","status":"publish","type":"post","link":"http:\/\/fiber-space.de\/wordpress\/2009\/07\/20\/a-simple-spring-challenge\/","title":{"rendered":"A simple Spring challenge"},"content":{"rendered":"<p>I got some comments on my <a href=\"http:\/\/fiber-space.de\/wordpress\/?p=1016\">Biggus Dickus article<\/a> in its own comments section as well as on  <a href=\"http:\/\/www.reddit.com\/r\/programming\/comments\/92nyw\/java_spring_or_the_biggus_dickus_effect\/\">programming.reddit<\/a>. Many people defended Spring on grounds of its usability, whereas others identified the author of this lines as completely clueless. I don&#8217;t want to argue against the latter and they are certainly right that Spring is the way enterprise software shall be written to eliminate Java complexity.<\/p>\n<p>SpringSource made just the mistake of supporting dynamic languages but omitted Jython which wasn&#8217;t hip for a while and now countless amateurs, Java mavericks and inevitable crackpots feel attracted by Spring and seek their luck. Once you open the door to this folks they want to feel comfortable in their own way which means they want to get rid of XML configuration files and enable self-management for dynamic languages.<\/p>\n<h3>The problem description<\/h3>\n<p>It is not much effort to use Springs dependency injection (DI) machinery. The Spring user can follow a Bean creation protocol which looks cumbersome on the first sight but one gets used to it very soon.<\/p>\n<p>Spring defines a `BeanReaderInterface` implemented by the `PropertyBeanDefinitionReader` and `XmlBeanDefinitionReader` classes. So what about adding a `JythonBeanDefinitionReader` along with a `JythonBeanFactory` replacing the `DefaultListableBeanFactory` or another factory of this kind which is typically used? The following protocol shows how to tangle both types and how to create new bean instances without letting the application know anything about configuration logics:<\/p>\n<pre lang=\"java\">JythonBeanFactory factory = new JythonBeanFactory();\r\nJythonBeanDefinitionReader reader = new JythonBeanDefinitionReader(factory);\r\nreader.loadBeanDefinitions(path);\r\nSomeBean obj = (SomeBean)factory.getBean(name);<\/pre>\n<p>How can  `JythonBeanFactory` and `JythonBeanDefinitionReader` possibly work ? In a simple case the Reader uses the Jython API and imports a Python module which defines parameter-less functions like the following:<\/p>\n<pre lang=\"python\">def source():\r\n    # creates SomeBean object and returns it<\/pre>\n<p>Calling `factory.getBean(&#8220;source&#8221;)` will invoke the `source()` function which returns a `SomeBean` object. Eventually the object has been cached but at this stage I do not want to complicate the design if it can be avoided.<\/p>\n<p>Both classes can be implemented on an elementary level as a simple exercise of embedding Jython in Java and using the Jython API.<\/p>\n<h3>The Challenge<\/h3>\n<p>Now try to write both classes s.t. they do fit into the Spring framework. As I said above it is a basic exercise to write them but side stepping the Spring interface hierarchy would just mean to create another DI framework which is off topic here. They shall implement Spring interfaces and they shall replace existing bean readers and factories. This is not hackish and Spring itself has foreseen such extensions as use cases of the framework.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I got some comments on my Biggus Dickus article in its own comments section as well as on programming.reddit. Many people defended Spring on grounds of its usability, whereas others identified the author of this lines as completely clueless. I &hellip; <a href=\"http:\/\/fiber-space.de\/wordpress\/2009\/07\/20\/a-simple-spring-challenge\/\">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":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/1031"}],"collection":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/comments?post=1031"}],"version-history":[{"count":6,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/1031\/revisions"}],"predecessor-version":[{"id":1037,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/posts\/1031\/revisions\/1037"}],"wp:attachment":[{"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/media?parent=1031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/categories?post=1031"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fiber-space.de\/wordpress\/wp-json\/wp\/v2\/tags?post=1031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}