<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5161303005915990938</id><updated>2011-04-21T16:55:29.618-07:00</updated><category term='grails'/><title type='text'>Java tips</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javabuzzer.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://javabuzzer.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Cloogy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5161303005915990938.post-4024665599708614313</id><published>2007-11-29T16:17:00.000-08:00</published><updated>2007-11-29T16:18:27.702-08:00</updated><title type='text'>Spring 2.5 - Too much auto-wired?</title><content type='html'>Concept to auto-wire relationships among spring enabled beans has always been there. The idea with auto-wiring is to get away from the tedious task of specifying and more importantly maintaining explicit wiring. Originally it was supported to be done by name, by type, by constructors or auto-detect and then you had the option to auto-wire all or specific beans within a context. But now with Spring 2.5 the auto wiring concept has taken a whole new meaning and so is the debate if we really want to do auto-wiring.

Spring 2.5 has a new @Autowire annotation. @Autowire annotation let us do much fine grained auto-wiring then was possible before and also it make us much more explicit then was possible in pre Spring 2.5 times

Lets consider few examples
#1
&lt;pre class="prettyprint"&gt;
@Autowired
public void init(AccountDao accountDao, CustomerDao customerDao) {
   this.accountDao = accountDao;
   this.customerDao = customerDao;
}
&lt;/pre&gt;

Do not need to have setters with one parameter to inject dependencies, any method with any name and any number of parameters will do. Can be applied to fields and constructors as well and obviously to the favorite setters :)

#2
&lt;pre class="prettyprint"&gt;
@Autowired
private BaseDao[] daos;
&lt;/pre&gt;

or
&lt;pre class="prettyprint"&gt;
@Autowired
private Set&lt;basedao&gt; daos;
&lt;/basedao&gt;&lt;/pre&gt;

Create an array or collection with all the possible beans available in the context..

#3
&lt;pre class="prettyprint"&gt;
@Autowired(required=false)
private AccountDao accountDao = new AccountDao();
&lt;/pre&gt;

Find it do it, if not leave it.

Moving on we can even fine control this by using another annotation @Qualifier. Again lets see some more examples
#4
&lt;pre class="prettyprint"&gt;
@Autowired
@Qualifier("bankService")
private BankService bkService;
&lt;/pre&gt;

We are doing by-name auto-wiring by providing a bean name within the annotation. This might help by letting you declare the name of the property different from the name of the bean.

#5 – Example of custom qualifier annotations to take care of the case when we have more then one implementation which we want to auto-wire
Define an annotation like this
&lt;pre class="prettyprint"&gt;
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Category {
   String value();
}
&lt;/pre&gt;
Custom qualifier on autowired fields can be applied like this
&lt;pre class="prettyprint"&gt;
public class BookList {
   @Autowired
   @Category("Technical")
   private Book technicalBook;

   @Autowired
   @Category("Management")
   private Book managementBook;

}
&lt;/pre&gt;

and then do the bean definitions like this
&lt;pre class="prettyprint"&gt;
&amp;lt;bean name="book" class="example.Book"&amp;gt;
    &amp;lt;qualifier type="Category" value="Technical"/&amp;gt;
    &amp;lt;!-- This implementation specific properties --&amp;gt;
&amp;lt;/bean&amp;gt;
&amp;lt;bean name="book" class="example.Book"&amp;gt;
    &amp;lt;qualifier type="Category" value="Management"/&amp;gt;
    &amp;lt;!-- This implementation specific properties --&amp;gt;
&amp;lt;/bean&amp;gt;
&lt;/pre&gt;

And if this is not enough we can always create a custom auto-wire configurer or may be use the new @Resource annotation
for further auto-wiring stuff.

That is that for auto-wiring but Spring 2.5 introduces a concept of auto-detection of spring components on the classpath where we do not have to even define the beans (with the name and bean class) in the context. Spring does a component scanning on the classpath (which can be configured using filters) by detecting classes which are stereotyped and then registering them as spring enabled beans.

Yes it provides a lot of flexibility and a much greater control as can be seen from all of the above examples but still I feel it should be done with a lot of caution and I-am-going-to-be-consistent attitude.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5161303005915990938-4024665599708614313?l=javabuzzer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabuzzer.blogspot.com/feeds/4024665599708614313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5161303005915990938&amp;postID=4024665599708614313' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default/4024665599708614313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default/4024665599708614313'/><link rel='alternate' type='text/html' href='http://javabuzzer.blogspot.com/2007/11/spring-25-too-much-auto-wired.html' title='Spring 2.5 - Too much auto-wired?'/><author><name>Cloogy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5161303005915990938.post-2782556890132100523</id><published>2007-11-23T06:29:00.001-08:00</published><updated>2007-11-23T16:04:22.178-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='grails'/><title type='text'>Ten Common Misconceptions about Grails</title><content type='html'>&lt;p&gt;As is usually the case with anything &amp;quot;new&amp;quot; there&amp;#8217;s a lot of FUD and confusion out there with people who have not used Grails yet, that may be stopping them using it. Here&amp;#8217;s a quick list of some of the more common falsehoods being bandied about:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;Grails is just a clone of Rails&amp;quot;&lt;/b&gt;. Ruby On Rails introduced and unified some great ideas. Grails applies some of them to the Groovy/Java world but adds many features and concepts that don&amp;#8217;texist in Ruby, all in a way that makes sense to Groovy/Java programmers.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;Grails is not mature enough for me&amp;quot;&lt;/b&gt;. The increasing number of live commercial sites is the best answer to that. Its also built on Hibernate, Spring and SiteMesh which are well-established technologies, not to mention the Java JDK which is as old as the hills. Groovy is over three years old.&lt;/li&gt;&lt;br&gt;

&lt;li&gt;&lt;b&gt;&amp;quot;Grails uses an interpreted language (Groovy)&amp;quot;&lt;/b&gt;. Groovy compiles to Java VM bytecode at runtime. It is never, ever, ever interpreted. Period. Never. Did I say never ever? Really.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;Grails needs its own runtime environment&amp;quot;&lt;/b&gt;. Nope, you produce good old WAR files with &amp;quot;grails war&amp;quot; and deploy on your favourite app container. During development Grails uses the bundled Jetty just so you have zero configuration and dynamic reloading without container restarts.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;My manager won&amp;#8217;t let me use Grails because it isn&amp;#8217;t Java&amp;quot;&lt;/b&gt;. Smack him/her upside the head then!** Grails code is approximately 85% Java. It runs on the Java VM. It runs in your existing servlet container. Groovy is the greatest complement to Java, and many times more productive. You can also write POJOs for persistence to databases in Java and include Java src and any JARs you like in a Grails application, including EJBs, Spring beans etc. Any new tech can be a hard sell in a cold grey institution, but there&amp;#8217;s rarely a more convincing argument than &amp;quot;Hey Jim, I knocked up our new application prototype in 1hr in my lunch break with Grails - here&amp;#8217;s the URL&amp;quot;. [** comedy violence kids, not the real kind]&lt;/li&gt;&lt;br&gt;

&lt;li&gt;&lt;b&gt;&amp;quot;Grails is only for CRUD applications&amp;quot;&lt;/b&gt;. Many demos focus on CRUD scaffolding, but that is purely because of the instant gratification factor. Grails is an all purpose web framework.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;Scaffolding needs to be regenerated after every change&amp;quot;&lt;/b&gt;. Scaffolding is what we call the automatically generated boilerplate controller and view code for CRUD operations. Explicit regeneration is never required unless you are not using dynamic scaffolding. &amp;quot;def scaffold = Classname&amp;quot; is all you need in a controller and Grails will magic everything else and handle reloads during development. You can then, if you want, generate the controller and view code prior to release for full customisation.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;Grails is like other frameworks, ultimately limiting&amp;quot;&lt;/b&gt;. All Grails applications have a Spring bean context to which you can add absolutely any Java beans you like and access them from your application. Grails also has a sophisticated plugin architecture, and eminently flexible custom taglibs that are a refreshing change from JSP taglib.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;I can&amp;#8217;t find Grails programmers&amp;quot;&lt;/b&gt;. Any Java developer is easily a Grails developer. Plus there are far fewer lines of code in a Grails application than a standard Java web application, so getting up to speed will be much quicker.&lt;/li&gt;&lt;br&gt;
&lt;li&gt;&lt;b&gt;&amp;quot;Grails will make you popular with women&amp;quot;&lt;/b&gt;. Sorry quite the opposite, you will be enjoying coding so much you won&amp;#8217;t be chasing any women for a while. We should put this as a warning in the README actually, along with a disclaimer about any potential divorce that might result from hours spent playing with your Grails webapps.&lt;/li&gt;
&lt;/ol&gt;

By &lt;i&gt;&lt;a href="http://www.anyware.co.uk/2005/2007/07/02/10-common-misconceptions-about-grails/" rel="nofollow" &gt;Marc&lt;/a&gt;&lt;/i&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5161303005915990938-2782556890132100523?l=javabuzzer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabuzzer.blogspot.com/feeds/2782556890132100523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5161303005915990938&amp;postID=2782556890132100523' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default/2782556890132100523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default/2782556890132100523'/><link rel='alternate' type='text/html' href='http://javabuzzer.blogspot.com/2007/11/ten-common-misconceptions-about-grails.html' title='Ten Common Misconceptions about Grails'/><author><name>Cloogy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5161303005915990938.post-7661444150227954506</id><published>2007-11-20T13:08:00.000-08:00</published><updated>2007-11-23T16:00:38.605-08:00</updated><title type='text'>Controlling Classloading with Spring</title><content type='html'>&lt;div class="content"&gt;     &lt;p&gt; &lt;a href="http://www.springframework.org/"&gt;The Spring Framework&lt;/a&gt; provides tons of functionality for someone wanting to use dependency injection; so much so that it can be difficult to know everything you can do.&lt;/p&gt; &lt;p&gt;Many times when dealing with a framework or a web application, it becomes important to track down JAR files and class files at runtime, and load them, often times in their own ClassLoader object.&lt;/p&gt; &lt;p&gt;Just to remind everyone out there, classes loaded in one ClassLoader are not visible to a parent or sibling ClassLoader (unless you are using some nightmare ClassLoader object that breaks the hierarchical semantics of core Java classloading).&lt;/p&gt; &lt;blockquote&gt; &lt;pre&gt;ClassLoaderA
| (ClassA, ClassB, ClassC)
|
|__ ClassLoaderB (Child of A)
|   (ClassD, ClassE)
|
|__ ClassLoaderC (Child of A)
(ClassF, ClassG)
&lt;/pre&gt; &lt;/blockquote&gt; &lt;p&gt; Using this diagram as an example, &lt;code&gt;ClassLoaderB&lt;/code&gt; can see &lt;code&gt;ClassA&lt;/code&gt;, &lt;code&gt;ClassB&lt;/code&gt;, &lt;code&gt;ClassC&lt;/code&gt;, &lt;code&gt;ClassD&lt;/code&gt;, and &lt;code&gt;ClassE&lt;/code&gt;, but it cannot see &lt;code&gt;ClassF&lt;/code&gt; or &lt;code&gt;ClassG&lt;/code&gt;. Likewise, &lt;code&gt;ClassLoaderC&lt;/code&gt; cannot see the classes loaded by &lt;code&gt;ClassLoaderB&lt;/code&gt;. And finally, &lt;code&gt;ClassLoaderA&lt;/code&gt; can only see it's own classes.&lt;/p&gt;  &lt;p&gt;Thankfully, &lt;i&gt;objects&lt;/i&gt; loaded by child/sibling classloaders can be dealt with in classes loaded by parent/peer classloaders (this can be seen when you put one of your objects in to an &lt;code&gt;ArrayList&lt;/code&gt; object - the &lt;code&gt;ArrayList&lt;/code&gt; may not be able to reference your class, but it can still deal with the objects inside of that class.&lt;/p&gt; &lt;p&gt;Considering the above, what if we want to use a Spring &lt;code&gt;XMLBeanFactory&lt;/code&gt; object (created by &lt;code&gt;ClassLoaderA&lt;/code&gt;) to load a bean definition in an XML file that is referring to &lt;code&gt;ClassD&lt;/code&gt; which is inside &lt;code&gt;ClassLoaderB&lt;/code&gt;?&lt;/p&gt; &lt;p&gt;Thankfully, it is a fairly simple process; it simply involves understanding the two pieces at work. We have: &lt;/p&gt;&lt;ol&gt;&lt;li&gt;A Bean Definition Reader - This is the object that reads the bean definition from the source (redundant, aren't I?); in other words, in this case this is the XML parser and magical finder of Class objects when you type &lt;code&gt;&lt;bean class=""&gt;...&lt;/bean&gt;&lt;/code&gt;.&lt;/li&gt;&lt;li&gt;A Bean Factory - This is the implementation of the factory that handles the construction of beans after they have been defined by the reader. This is the stage in the process where AOP, autowiring, property setting, singletons, prototypes, etc is all plugged in.&lt;/li&gt;&lt;/ol&gt; So now, being infinitely more educated on the Spring bean factories, it is just a short hop to plugging in the correct class loader &lt;code&gt;ClassLoaderB&lt;/code&gt; for the task at hand. &lt;p&gt;The &lt;code&gt;BeanDefinitionReader&lt;/code&gt; is where classes are found when Spring is parsing/interpreting a bean definition source (such as a bean XML file). There is a method on the interface - &lt;code&gt;BeanDefinitionReader.getBeanClassLoader()&lt;/code&gt; that provides the correct class loader to the BeanFactory (this defaults to &lt;code&gt;Thread.currentThread().getContextClassLoader()&lt;/code&gt;). There is also a method on the abstract base implementation - &lt;code&gt;AbstractBeanDefinitionReader.setBeanClassLoader(ClassLoader)&lt;/code&gt; so we can set our class loader to the correct implementation.&lt;/p&gt; &lt;p&gt;'But wait R.J.!', you say - 'There is no &lt;code&gt;BeanDefinitionReader&lt;/code&gt; that I can see on the &lt;code&gt;XmlBeanFactory&lt;/code&gt; class - you're a liar! I'm never reading Coffee-Bytes again!'. Don't pull away from me yet! While it is true that the &lt;code&gt;XmlBeanFactory&lt;/code&gt; class doesn't provide visibility to the bean definition reader, that doesn't mean it's not there! In reality, the &lt;code&gt;XmlBeanFactory&lt;/code&gt; is just a convenience implementation of two seperate objects:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;XmlBeanFactory factory = new XmlBeanFactory([xml resource here!]);
// == to
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader();
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(reader);
reader.loadBeanDefinitions([xml resource here!]);
&lt;/pre&gt;&lt;/blockquote&gt; &lt;p&gt;Do I think it is frustrating that you can't get to the bean definition reader without forgoing the XmlBeanFactory class for it's super class? Absolutely! Do I have commit rights to Spring? Unfortunately for them, no (hint, hint). So, that's that... oh, you still don't feel that sweet cut/paste solution beckoning to you? Am I leaving you patient readers hanging without a clean solution to your problems? Ok, ok... &lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader();
&lt;strong&gt;&lt;i&gt;reader.setBeanClassLoader([Class LoaderB here!]);&lt;/i&gt;&lt;/strong&gt;
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(reader);
reader.loadBeanDefinitions([xml resource here!]);
&lt;/pre&gt;&lt;/blockquote&gt;  &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5161303005915990938-7661444150227954506?l=javabuzzer.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabuzzer.blogspot.com/feeds/7661444150227954506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5161303005915990938&amp;postID=7661444150227954506' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default/7661444150227954506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5161303005915990938/posts/default/7661444150227954506'/><link rel='alternate' type='text/html' href='http://javabuzzer.blogspot.com/2007/11/controlling-classloading-with-spring.html' title='Controlling Classloading with Spring'/><author><name>Cloogy</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
