<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>public static final</title>
	<atom:link href="http://www.publicstaticfinal.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.publicstaticfinal.de</link>
	<description>the constant Java blog</description>
	<lastBuildDate>Sat, 21 Aug 2010 12:09:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>String concatenation</title>
		<link>http://www.publicstaticfinal.de/2010/08/21/string-concatenation/</link>
		<comments>http://www.publicstaticfinal.de/2010/08/21/string-concatenation/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 12:09:05 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=148</guid>
		<description><![CDATA[One of the first things developers learn about String concatenation in Java is that the &#8220;+&#8221; operation does the magic. In this case the magician is the compiler. Lets look how the following code snippet gets translated by the compiler. // Sample.java String buffer = &#34;&#34;; for (String string : listOfStrings) { buffer += string; [...]]]></description>
			<content:encoded><![CDATA[<p>One of the first things developers learn about String concatenation in Java is that the &#8220;+&#8221; operation does the magic. In this case the magician is the compiler. Lets look how the following code snippet gets translated by the compiler.</p>
<pre class="brush: java;">
// Sample.java
String buffer = &quot;&quot;;
for (String string : listOfStrings)
{
  buffer += string;
}
</pre>
<pre class="brush: java;">
// Sample.class
String buffer = &quot;&quot;;
for (String string : listOfString)
{
  buffer = new StringBuilder(buffer).append(string).toString();
}
</pre>
</pre>
<p>Every time the String concatenation is invoked a new StringBuilder instance is created and but never assigned. Hence it is immediately eligible for Garbace Collection. What a waste of Memory. If you really need to concat Strings in a loop you better do it directly with a StringBuilder.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/08/21/string-concatenation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RIP Sun</title>
		<link>http://www.publicstaticfinal.de/2010/01/22/rip-sun/</link>
		<comments>http://www.publicstaticfinal.de/2010/01/22/rip-sun/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 11:49:32 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Sun]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=141</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.publicstaticfinal.de/wp-content/uploads/2010/01/SunRIPsmall.jpg" rel="thumbnail"><img class="alignnone size-medium wp-image-140" title="SunRIPsmall" src="http://www.publicstaticfinal.de/wp-content/uploads/2010/01/SunRIPsmall-300x234.jpg" alt="" width="581" height="452" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/01/22/rip-sun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One cold day in hell</title>
		<link>http://www.publicstaticfinal.de/2010/01/12/one-cold-day-in-hell/</link>
		<comments>http://www.publicstaticfinal.de/2010/01/12/one-cold-day-in-hell/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 10:05:03 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=137</guid>
		<description><![CDATA[Chemistry class at Maynooth University in Kildare, Ireland. The answer a professor got from one of his students was so &#8220;deep&#8221; he had to pass it to his colleages via internet: Bonus-question: &#8220;Is hell &#8220;exotherm&#8221; (gives off heat), or &#8220;endotherm&#8221;(absorbs heat)? Most students assumed with the help of Boyles law that gas cools down while [...]]]></description>
			<content:encoded><![CDATA[<p>Chemistry class at Maynooth University in Kildare, Ireland.</p>
<p>The answer a professor got from one of his students was so &#8220;deep&#8221; he had to pass it to his colleages via internet:</p>
<p>Bonus-question:</p>
<p>&#8220;Is hell &#8220;exotherm&#8221; (gives off heat), or &#8220;endotherm&#8221;(absorbs heat)?</p>
<p><span id="more-137"></span> Most students assumed with the help of Boyles law that gas cools down while expanding. One student however came up with this answer:</p>
<p>&#8220;First we need to find out how the mass of hell changes over time.<br />
To be able to do this we need to first find out how many souls venture into hell and out of it.</p>
<p>It is my opinion, that souls , once they&#8217;ve entered into hell, won&#8217;t leave it again.</p>
<p>That&#8217;s why no soul ever leaves hell again.</p>
<p>Concerning the question how many souls go to hell, the opinions of the religions existing today, can give us an answer.</p>
<p>Most religions assert that you go to hell when you&#8217;re not one of them.<br />
Since there is more than one religion and because you can&#8217;t belong to more than one of them you can assume further that all souls go to hell.</p>
<p>Wíth birth- and deathrates of today we can further assume that the amount of souls will exponentially increase.</p>
<p>Let&#8217;s now take a look at the size of hell.<br />
According to Boyles law the size of hell must increase proportional to the arriving souls or else we have two options:</p>
<p>1.: In case hell expands slower than the number of souls arriving, both temperature and pressure inside of hell will increase so much that the whole hell falls apart.</p>
<p>2. In case the hell expands faster then the amount of new guests then temperature and pressure will fall causing the hell to freeze up.</p>
<p>Now which is it?</p>
<p>When we take into consideration Sandra&#8217;s statement to me in our first year here, that it will be a cold day in hell before she&#8217;d go to bed with me as well as the fact that I slept with her last night only number 2 can be valid.</p>
<p>That&#8217;s why I&#8217;m convinced that hell is endotherm and must be already frozen up.</p>
<p>My conclusion is, that hell can not accept any further souls and is therefore extinct&#8230;.and so only heaven is still existing. This however, proves the existance of God, which explains why Sandra screamed: °Oh, my God!!!° the whole last evening.&#8221;</p>
<p>This student was the only one who got an &#8220;A&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/01/12/one-cold-day-in-hell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Collections Library 1.0 available</title>
		<link>http://www.publicstaticfinal.de/2010/01/04/google-collections-library-1-0-available/</link>
		<comments>http://www.publicstaticfinal.de/2010/01/04/google-collections-library-1-0-available/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 08:56:12 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Framework]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=128</guid>
		<description><![CDATA[Last year on Dec, 30th Google released version 1.0 of it&#8217;s Collections framework. Although Apache Commons Collections provide a rich set of enhancements to the standard Collections framework Google&#8217;s implementation has two big advantages: Based on Java 1.5 and hence using Generics More standard compliant (Commons Collections occasionally break standard behavior) Go to their website [...]]]></description>
			<content:encoded><![CDATA[<p>Last year on Dec, 30th Google released version 1.0 of it&#8217;s Collections framework. Although Apache Commons Collections provide a rich set of enhancements to the standard Collections framework Google&#8217;s implementation has two big advantages:</p>
<ul>
<li>Based on Java 1.5 and hence using Generics</li>
<li>More standard compliant (Commons Collections occasionally break standard behavior)</li>
</ul>
<p>Go to their <a title="Google Collections" href="http://code.google.com/p/google-collections/" target="_blank">website</a> and make sure to read the <a title="Google Collections FAQ" href="http://code.google.com/p/google-collections/wiki/Faq" target="_blank">FAQ</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2010/01/04/google-collections-library-1-0-available/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Save MySQL! campaign</title>
		<link>http://www.publicstaticfinal.de/2009/12/30/save-mysql-campaign/</link>
		<comments>http://www.publicstaticfinal.de/2009/12/30/save-mysql-campaign/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 09:34:32 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[OpenSource]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=123</guid>
		<description><![CDATA[Michael &#8220;Monty&#8221; Widenius, the man who brough us joy and pain by creating MySQL, has started a campaign for saving MySQL. He demands that that the European Commission should only approve the Oracle/Sun deal if MySQL will be sold to a competitor or Oralce guarantees (by signing some paper) that all previous version of MySQL [...]]]></description>
			<content:encoded><![CDATA[<p>Michael &#8220;Monty&#8221; Widenius, the man who brough us joy and pain by creating MySQL, has started a campaign for saving MySQL. He demands that that the European Commission should only approve the Oracle/Sun deal if</p>
<ul>
<li>MySQL will be sold to a competitor or</li>
<li>Oralce guarantees (by signing some paper) that all previous version of MySQL will still be freely available and additionly will be released as free years for the next three years.</li>
</ul>
<p>I&#8217;m not giving anyone an advice whether to sign the petition or not but everyone should take a look at the <a title="Help MySQL" href="http://www.helpmysql.org" target="_blank">campaign </a>and decide on which side he or she stands. More information about the topic and a little Q &amp;A can be found at <a title="Monty Says" href="http://monty-says.blogspot.com/2009/12/help-keep-internet-free.html" target="_blank">Monty&#8217;s blog</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/12/30/save-mysql-campaign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Closures in Java</title>
		<link>http://www.publicstaticfinal.de/2009/12/14/closures-in-java/</link>
		<comments>http://www.publicstaticfinal.de/2009/12/14/closures-in-java/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 10:37:27 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Closures]]></category>
		<category><![CDATA[JSE7]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=117</guid>
		<description><![CDATA[One of the most discussed (and most controversial) topics in the last years concerning the future of Java was the introduction of Closures. After 2008&#8242;s Devoxx Closures were off the Java7 release. One year later at the same event Mark Reinhold revoked it and announced that Closures will be in Java7 (and hence delaying Java [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most discussed (and most controversial) topics in the last years concerning the future of Java was the introduction of <a title="Wikipedia: Closures" href="http://en.wikipedia.org/wiki/Closure_(computer_science)" target="_blank">Closures</a>. After 2008&#8242;s Devoxx Closures were off the Java7 release. One year later at the same event Mark Reinhold revoked it and announced that Closures will be in Java7 (and hence <a title="Java7 will be delayed" href="http://mail.openjdk.java.net/pipermail/jdk7-dev/2009-November/001054.html" target="_blank">delaying Java 7 for a few months</a>).<span id="more-117"></span></p>
<p>For days ago, Reinhold published a<a href="http://cr.openjdk.java.net/~mr/lambda/straw-man/" target="_blank"> straw-man proposal</a> of how closures (or more formal: lambda expressions) could look like in Java. I&#8217;m still not sure whether I&#8217;m happy about closures in Java or not. On the one hand it definitely helps to reduce the number of lines of code since it drops redundant information (e.g. the decleration of a Runnable).</p>
<pre class="brush: java;">
Thread th = new Thread(new Runnable() {
                         public void run() {
                            doSomeStuff();
                            doMoreStuff();
                         }
                       });

Thread th = new Thread(#(){ doSomeStuff(); doMoreStuff(); } )
</pre>
<p>On the other hand it increases the complexity of the JVM itself and makes maintenance a bit harder. This will affect only few developers but it&#8217;s another stop to the grave of Java (death by non-maintainability).</p>
<p>I think time will tell how Closures will affect the Java world and if it&#8217;s a good idea to include it or not.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/12/14/closures-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaEE 6 has been approved</title>
		<link>http://www.publicstaticfinal.de/2009/12/02/javaee-6-has-been-approved/</link>
		<comments>http://www.publicstaticfinal.de/2009/12/02/javaee-6-has-been-approved/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 08:32:54 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JavaEE 6]]></category>
		<category><![CDATA[Web Profile]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=114</guid>
		<description><![CDATA[On Monday the final votes for &#8220;JSR 316: Java Platform, Enterprise Edition 6 (Java EE 6) Specification&#8221; have been submitted and with little surprise it has been approved. JavaEE6 marks a major milestone in enterprise Java with the arrival of subspecs such as CDI, JPA2, EJB 3.1 (with EJB Lite) or Servlet 3.0. But the [...]]]></description>
			<content:encoded><![CDATA[<p>On Monday the final votes for &#8220;JSR 316: Java Platform, Enterprise Edition 6 (Java EE 6) Specification&#8221; have been submitted and with little surprise it has been approved.</p>
<p>JavaEE6 marks a major milestone in enterprise Java with the arrival of subspecs such as CDI, JPA2, EJB 3.1 (with EJB Lite) or Servlet 3.0. But the most important part for me is the new web profile which was really overdue.<span id="more-114"></span>JavaEE5 left web developers somewhere between all or nothing. POSCs (Plain Old Servlet Containers) proveded not all of the JEE stack that might be needed while full blown application servers like JBoss were too heavy (although JBoss did a&lt; good job with the different profiles).</p>
<p>Integrating different Frameworks into POSCs can be really a pain in the ass. Take JPA for example. JPA in a POSC is nothing more than a JavaSE usage of the JPA. Which means there are no manged transactions no container managed persistence contexts etc. Everything had to be custom made which is not the desired way.</p>
<p>With the new web profile you only get what you really need: Servlets, JPA2, JTA, CDI, EJB Lite which makes web development a lot easier. But not only the developer benefits but also the vendors since they don&#8217;t have to implement the full stack.</p>
<p>Hopefully vendors will jump on the JEE6 train and there will be a vibrant market of implementations.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/12/02/javaee-6-has-been-approved/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>It&#8217;s /WEB-INF/classes/META-INF</title>
		<link>http://www.publicstaticfinal.de/2009/11/10/its-web-infclassesmeta-inf/</link>
		<comments>http://www.publicstaticfinal.de/2009/11/10/its-web-infclassesmeta-inf/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 09:42:54 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=110</guid>
		<description><![CDATA[Note to myself (which might help others): In a web application always put the stuff (which is not packaged a single JARs)  in /WEB-INF/classes/META-INF (e.g. persistence.xml or service files) and not in /META-INF. Otherwise the various applications will not be able to locate them.]]></description>
			<content:encoded><![CDATA[<p>Note to myself (which might help others):</p>
<p>In a web application always put the stuff (which is not packaged a single JARs)  in <strong>/WEB-INF/classes/META-INF</strong> (e.g. persistence.xml or service files) and not in /META-INF. Otherwise the various applications will not be able to locate them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/11/10/its-web-infclassesmeta-inf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weld tutorial &#8211; Part 3</title>
		<link>http://www.publicstaticfinal.de/2009/11/04/weld-tutorial-part-3/</link>
		<comments>http://www.publicstaticfinal.de/2009/11/04/weld-tutorial-part-3/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 15:52:55 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JavaEE 6]]></category>
		<category><![CDATA[jsr-299]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=103</guid>
		<description><![CDATA[In part 1 and part 2 of this series of articles I talked about how to use the simple injection provided by Weld. What I&#8217;ve done so far was to inject full blown beans which get instanciated by its constructor. But there is another way: a producer-consumer-relationship. With this way of injection it is possible [...]]]></description>
			<content:encoded><![CDATA[<p>In part 1 and part 2 of this series of articles I talked about how to use the simple injection provided by Weld. What I&#8217;ve done so far was to inject full blown beans which get instanciated by its constructor. But there is another way: a producer-consumer-relationship.</p>
<p>With this way of injection it is possible to really inject everything. POJOs, DB resoures like ResultSets even primitives can be injected.<span id="more-103"></span></p>
<p>The following two code listings show how to produce and how to consume. I&#8217;m going to explain what&#8217;s really happening here afterwards:</p>
<pre class="brush: java;">
@Produces @Random int next() {
 return getRandom().nextInt(maxNumber);
}
</pre>
<pre class="brush: java;">
@Inject @Random
private int number;
</pre>
<p>Okay, as promised here are some explanations:</p>
<p>@Produces tells Weld that something that might get injected somewhere is being produced here. What is specified by the next annotation. Although @Random might look like a built-in annotation it is not. I named it Random to point out that the int is generated (pseudo) randomly. This is a socalled qualifier annotation which tells the dependency injection framework where to look for the producer.</p>
<pre class="brush: java;">
@Target( { TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Random
{
}
</pre>
<p>The only new thing is the @Qualifier annotation which is contained in the javax.inject package and defines qualifier annotations. As you can see in the samples above it is really possible to inject everything. There thousands of scenarios in which this might be useful. Just imagine a producer which generates a unique ID fo every instance.</p>
<p><strong>Event / Observer concept</strong></p>
<p>So far I just talked about the DI features of JSR-299. Althought the official name of this JRS-299 might say otherwise Weld is not limited to dependecy injection. One thing I really love is the event &#8220;bus&#8221; which basically an implementation of the observer pattern with lots of tuning and simplification.</p>
<p>The greatest simplification ist that there is no need to explicitly register an observer and the observable knows nothing about its observers. If you look a the &#8220;standard&#8221; observer pattern used in Swing you&#8217;ll find a method JButton.addActionListener(ActionListener l). It&#8217;s clear what&#8217;s happening here: an ActionListener is explicitly registered at one button. That means that the Button knows its listeners and the listener knows exactly to what component he or she is listening to.</p>
<p>The following annotation defines an observable (Standard Java: implements Observable):</p>
<pre class="brush: java;">
@Qualifier
@Target( { PARAMETER })
@Retention(RUNTIME)
public @interface Event {
 String source();
}
</pre>
<p>The next part might seem a litte strange since it implements an annotation but later it will be clear why this is done.</p>
<pre class="brush: java;">
package de.publicstaticfinal.web.jsf;
import javax.enterprise.inject.AnnotationLiteral;
public abstract class EventImpl extends AnnotationLiteral&lt;Event&gt; implements Event
{
}
</pre>
<p>Next we declare the concrete event we&#8217;re listening to. This is just a simple POJO. IF you look at the Java API this Event is equivalent to the second argument of Observer.update(Oberservable o, Object argument). For better clarification I name it HelloWeldEvent.</p>
<pre class="brush: java;">
package de.publicstaticfinal.web.jsf;
public class HelloWeldEvent
{
 private String source;

 public HelloWeldEvent(String source)
 {
 this.source = source;
 }

 public String getSource()
 {
 return source;
 }
}
</pre>
<p>A simple event which contains the name of its source. What&#8217;s left is the event listener itself but that&#8217;s not that many code:</p>
<pre class="brush: java;">
public void handleEvent(@Observes @Any HelloWeldEvent hwe)
{
 System.out.println(hwe.getSource());
}
</pre>
<p>@Observes is the annotations which tells Weld that this method listens to ALL (@Any) HelloWeldEvent which are raised in this application. This might be useful in some circumstances (imagine a logging facility which gets called every time a button is clicked) but on other occasions this is to bloated. Therefore I added the &#8220;method&#8221; source() to the Event annotation. With that it is possible to just listen to a special HelloWeldEvent.</p>
<pre class="brush: java;">
public void handleEvent(@Observes @Event(&quot;button1&quot;) HelloWeldEvent hwe)
{
 System.out.println(hwe.getSource());
}
</pre>
<p>This observer method just gets called when the raised event&#8217;s source is &#8220;button1&#8243;. So far so good but how do you raise an event? The famous BeanManager provides a method for doing this: public void fireEvent(Object event, Annotation&#8230; bindings).</p>
<p>The first argument is the event itself. It is of type Object since there is no special interface an event has to implement which means that every Object could be an event. The next arguments are the anootation bindings which are used to resolve the listeners. In this tutorial&#8217;s case this would be Event. And now it might be clear why we need an implementation if Event. I used @Event(&#8220;button1&#8243;) to specify on which concrete event the listener should be called. Weld needs a mechanism to read this information and to decide whether the method should be called or not.</p>
<p>Written in Java it looks like this:</p>
<pre class="brush: java;">
private void fireEvent(){
 beanManager.fireEvent(new HelloWeldEvent(&quot;Test&quot;), new EventImpl(){
 public String source()
 {
 return &quot;button1&quot;;
 }});
 }
</pre>
<p>When calling fireEvent a concrete implementation of Event is provided and Weld can use the source() method to retrieve the source. After an check whith equals to the value of @Event(&#8230;) Weld knows what methods to call. Be aware that we talk about synchronous events here. JSR-299 does not define asynchronous events.</p>
<p>This was the last part of my tutorial series. As I stated before JSR-299 is way to extensive to be completely covered by a tutorial. I just wanted to give people who are interested in working with it a litte help to get started. I hope that I achieved this goal a little bit and the ones who read the article are goind to use Weld (or other implementations) since it will boost your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/11/04/weld-tutorial-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weld tutorial &#8211; Part 2</title>
		<link>http://www.publicstaticfinal.de/2009/10/28/weld-tutorial-part-2/</link>
		<comments>http://www.publicstaticfinal.de/2009/10/28/weld-tutorial-part-2/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 18:00:57 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JavaEE 6]]></category>
		<category><![CDATA[jsr-299]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[Weld]]></category>

		<guid isPermaLink="false">http://www.publicstaticfinal.de/?p=89</guid>
		<description><![CDATA[Welcome to part 2 of my litte Weld tutorial. Before I start I have to point you to the PFD2 of Weld. Why? Well, the whole JSR-299 is very extensive. It is IMO nearly impossible to cover all parts of it in a few lines. Hence I&#8217;m not going into much details of Weld but [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to part 2 of my litte Weld tutorial. Before I start I have to point you to the PFD2 of Weld. Why? Well, the whole JSR-299 is very extensive. It is IMO nearly impossible to cover all parts of it in a few lines. Hence I&#8217;m not going into much details of Weld but just show you how to get started.</p>
<p>Having said that lets go to&#8230;<span id="more-89"></span><strong> </strong></p>
<p><strong>Obtaining the BeanManager</strong></p>
<p>In your web.xml/context.xml you declared a managed resource called BeanManager which is main interface to all Bean related stuff (adding/removing beans from a context etc.). There are two ways for obtaining the Bean Manager. The first way is to use dependey injection with the @Resource annotation:</p>
<pre class="brush: java;">
@Resource(mappedName=&quot;java:/comp/env/BeanManager&quot;)
BeanManager beanManager;
</pre>
<p>Here you let the container do all the work. Or you cann ask Weld to inject the BeanManager by using @Inject:</p>
<pre class="brush: java;">
@Inject
BeanManager beanManager;
</pre>
<p><strong>Simple Dependency Injection</strong></p>
<p>In the previous section I made a little trip to the future by using the @Inject annotation. As you might have already guessed this annotation tells Weld that the field/parameter should be injected. This is the simplest dependency injection available. You just tell Weld that you need something injected and Weld takes care of it. A good example for this would be a Cache which is typically application scoped (although every scoped bean can be injected):</p>
<pre class="brush: java;">
package de.publicstaticfinal.web.jsf;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named
@ApplicationScoped
public class Cache {...}
</pre>
<p>As you learned already this managed bean can be accessed via EL using #{cache}. But most often you&#8217;re not only using the Cache in the frontend but in the business logic too. The extenden HelloWeld bean would look like this:</p>
<pre class="brush: java;">
package de.publicstaticfinal.web.jsf;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named(&quot;helloWeld&quot;)
@SessionScoped
public class HelloWeld implements Serializable {
  private String message = &quot;Hello Weld&quot;;

  @Inject
  private transient Cache cache;

  public String getMessage() {
    return message;
  }
}
</pre>
<p>The injected Cache object has to be marked as transient since HelloWeld is serializable while the Cache is not. If the cache field is not marked as transient Weld will throw an Exception since it enforces serializability. This is really a good feature in Weld since it leads to consistent data.</p>
<p>Another good point is that you can use loose coupling without much effort. If the Cache class above would be &#8220;CacheImpl&#8221; and implementing a Cache interface you don&#8217;t have to change your code that much. The Cache implementation class would look like this:</p>
<pre class="brush: java;">
package de.publicstaticfinal.web.jsf;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

@Named(&quot;cache&quot;)
@ApplicationScoped
public class CacheImpl implements Cache
{

}
</pre>
<p>And the CacheImpl would be injected like this:</p>
<pre class="brush: java;">
@Inject @Named(&quot;cache&quot;)
private transient Cache cache;
</pre>
<p>What if you need mor than one Caches? I&#8217;m too lazy at the moment to think of a scenario where you might need this but of course this is possible. Although with the restriction that this cannot be achieved directly by dependency injection. If you asked yourself why I explained how to obtain the BeanManager here&#8217;s the answer: the BeanManager helps you to create new instances of an injectable resource. The following mehtod gets called with getNewCache(&#8220;cache&#8221;):</p>
<pre class="brush: java;">
private Cache getNewCache(String elName) {
  Set&lt;Bean&lt;?&gt;&gt; beans = beanManager.getBeans(elName);
  for (Bean&lt;?&gt; bean : beans) {
    Set&lt;Type&gt; beanTypes = bean.getTypes();
    for (Type type : beanTypes) {
      if (type.equals(Cache.class)) {
        Bean&lt;Cache&gt; tmp = (Bean&lt;Cache&gt;) bean;
        Cache newCache = tmp.create(beanManager.createCreationalContext(tmp));
        return newCache;
      }
    }
 }
 return null;
}
</pre>
<ul>
<li>2: In this line all beans which can be accessed by #{cache} are retrieved</li>
<li>4: all types of the bean are read</li>
<li>6: Check if the bean is of type Cache</li>
<li>7: cast to the proper type</li>
<li>8: retrieval of the proxy object by api calls</li>
</ul>
<p>I&#8217;m not going into detail of every single API call. If you&#8217;re interested in more information please refer to the Javadoc (link: http://docs.jboss.org/cdi/api/1.0-CR1/).</p>
<p>I think that&#8217;s enough for the second part of my tutorial. This part just covered injection of scoped beans. While this is a pretty powerful tool JSR-299 doesn&#8217;t limit injection to scoped beans. Generally speaking you can <span style="text-decoration: line-through;">blow</span> inject your foot away using Weld.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.publicstaticfinal.de/2009/10/28/weld-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
