Discussions

Blogs: Prototypes and Java Config with Spring

  1. Prototypes and Java Config with Spring (4 messages)

    The following entry addresses the issues in around Prototypes and Java Config with Spring
    Sometimes in Spring, you need to create a one-time use instance that has state (a.k.a a prototype bean) from a Singleton object. Essentially, one would need to access a prototype for a number of cases, namely those where the Gang of Four Abstract Factory can be applied : You have an object that requires complex construction You have many potential implementations of an interface, and generic logic that applies to all of those implementations. I've been thinking about and even working on Spring JavaConfig (SJC) for a while. Take a look at this SJC bug that talks about lookup methods. The way you'd implement "lookup methods" in a java oriented environment is quite straight forward. The bean stays the same: package fiona.apple; // no more Spring imports! public abstract class CommandManager { public Object process(Object commandState) { // grab a new instance of the appropriate Command interface Command command = createCommand(); // set the state on the (hopefully brand new) Command instance command.setState(commandState); return command.execute(); } // okay... but where is the implementation of this method? protected abstract Command createCommand(); } The configuration code is surprisingly simple: package fiona.apple; // a whole bunch of spring and business object imports @Configuration public class CommandManagerConfiguration { @Bean public CommandManager commandManager(){ return new CommandManager(){ protected Command createCommand(){ /* you can add your dependencies here, if you have any */ return new AsyncCommand(); } }; } } A Java based configuration is, IMHO, a much more natural implementation than the XML approach. This is a surprisingly trivial implementation of a relatively complicated scenario. The slickness here comes from writing simple Java code SJC beyond prototypes The prototype slickness lead me down a path that helped me shed the preconceived notions that came from XML-based development. I started thinking about where I could use java to make my application configuration process even better. Now, I definitely thought of some cool infrastructure stuff that I could add to Spring JavaConfig, but that's not the application configuration ingredients that I find most interesting. The most interesting techniques a programmatic environment can add to application construction come from really simple day-to-day java code. For example, you can create conditional beans using a simple "if" statement. You can concatenate Lists and Maps. Those examples, plus other simple programming techniques that we've come to depend on as second nature, are now available. Spring doesn't dictate what I can do when constructing an application. You can now use Plain Old Java to configure your Plain Old Java Objects.
    Read the entire post: http://www.jroller.com/Solomon/entry/spring_prototype_scope_lookup_methods

    Threaded Messages (4)

  2. This is a copy of my blog[ Go to top ]

    Daniel, ripping of someone's intellectual property isn't good for business... you're bound to be found out eventually... I posted this exact blog entry first at http://www.jroller.com/Solomon/entry/spring_prototype_scope_lookup_methods Chris Beams, the SJC lead developer, told me about this blatant rip off. I encourage you to at least give credit to the original writer if you're going to copy their work. Once exposed, the kind of behavior you've engaged in encourages onlookers to seriously doubt your legitimate work. Please be more considerate of others (and yourself); don't take credit for other people's work.
  3. Note taken[ Go to top ]

    Solomon, My apologies if you felt this was a blatant rip-off of your original post. But please note I did not take credit for your work or that of others, I am simply the editor of this section quoting and pointing readers of the site to other's work. Granted I may have over-quoted on this post, if you look toward the end it indicates, read the entire post at http://springtips.blogspot.com/2008/02/prototypes-lookup-methods-and-java.html Unfortunately, since one of my sources for finding blogs on Spring and Java is Google, this particular entry came up first as posted on springtips.blogpost.com, I was not aware this blog was a re-post of your blog which was the original. But note taken, as far as TSS blog editorial is concerned, I will provide less quoted material from the actual posts from here onward given you felt this was a rip-off, though I cannot control if someone else in the blog-sphere has made a re-post of someone else's material, as this was unfortunately also the case.
  4. IP Ripoffs[ Go to top ]

    Daniel, I may have confused the messenger (you) with the duplication of the message.... Sorry for that. My tirade was definitely meant to be toward the springtips guy, which I thought you were... One easy way to find duplicate posts is by doing another Google query for the first couple of sentences. You (and TSS) definitely should re-post interesting blog posts with full accreditation. Previous TSS references did wonders for my blog stats. Can you please change the accreditation on this post to be to my blog instead of spring tips? I've been playing around with Lucene's MoreLikeThis feature, which could theoretically be used for this type of IP rip-off situation. That will probably be a post for another time ;).
  5. I am sure this article can give some related facts: http://question2answer.googlepages.com/jboss-transaction-cmt-spring-hibernate Thanks, Guddu