Wednesday, April 25, 2012

Spring : AutoMagical to AutoLogical

First of all, I have to express that I have been having a lot of bitter sweet moments throughout my process of learning Spring. Since there are abundant information on Spring everywhere on the web, I decided not to reiterate them. However, I did come across plenty of issues where the solutions are either very hard to find, need to be extracted from multiple sources or involve coming up with a solution by obtaining deeper understand of Spring. Therefore, I decided to share these information instead to ease the pain of learning Spring. Even though Spring does a lot of things automagically, sometimes it is necessary to know what is the autologic behind the automagic. It may be the tipping point of your code functioning or otherwise

First of all. Spring is not a container where magic happens. Spring is not special. Spring is just a collection of java class that is very good at managing objects and passing it around. First of all, you need to kickstart spring using ApplicationContext. Therefore, it is just a Java object. This mindset is key to understand certain issues that I will share later.

Question #1
Can you instantiate two Spring ApplicationContext separately? Will there be any collision?
Even though the answer is obvious, the amount of magic created by Spring may mislead you. Those two ApplicationContext are two different java objects. As such they don't share anything unless, Spring for some reason, contains static objects. Therefore, you can manage more than one instance of Spring if you want to and sometimes it is useful too. (Be sure you have enough memory though :) )

Question #2
Can you instantiate another application context within Spring?
Yes, since ApplicationContext is just an object you will create, you can therefore instantiate more objects within itself. However, it will have different context altogether. Lets say A is an ApplicationContext and B is another ApplicationContext initialized by A. Therefore A and B do no share beans. However, A can of course, obtain beans from B by using getBean. In the eyes of A, B is just a bean that happens to be ApplicationContext. This distinction is important.

Question #3
So what does it matter?
I was blissfully ignorant at first when I was using Spring. I came across two interesting problems.

Problem 1
I have multiple table with the same schema. Basically the schema was divided by location. I didn't want to have them all in the same table. How do I bind an Entity and JpaRepository to all the table without duplicating the code? (FYI, there is no straightforward to do this) Well, I created a mini ApplicationContext object for each table to the context and specified the table name when I was instantiating the application context. I instantiated all the ApplicationContext in my main ApplicationContext. With this I am not bound to one entity class to one database table anymore. Because they are all running on separate ApplicationContext each instance of Entity class is bound to different table. And I have a main container that can manage these instances and retrieve the Repository for each table for me.

Problem 2
I wanted to configure Spring Batch Admin to work alongside with another application I had. So I configured my web.xml and gave /batch to spring batch and /app to my own application. For some reason, I can never seem to obtain the beans from the /app configuration files from the /batch. After digging deep, it turns out that /app and /batch are actually run using two different ApplicationContext and therefore do not share beans. It took a good few days before I realized what was going on. This may sound a bit abstract, but I will writing another post on how I solved this issue.

I hope this serves as a primer when you are wrking with Spring. This is especially useful if you are new to Spring and start encountering weird bugs. Just ask yourself, how many ApplicationContexts are running and which bean belongs where.

Stay tuned for more.

P.S : I am still new to Spring, so if you think I am barking at the wrong tree, feel free to correct me.

Saturday, December 24, 2011

Java Spring Framework

It has been one busy semester for me. I somehow managed to get through the semester. Now that it is break time, I took the liberty to learn some new Java Technologies, namely Java Persistence Api and Java Spring Framework. I will save JPA for some other time.

Spring Framework, is simply, the most complete framework I have seen. Anything that I wanted to solve, there is already a hook for it in the framework itself. With Spring, I have managed to reduce the number of lines of code that I need to write, significantly.

What is Spring?
Spring is basically, a popular framework that derives its power through dependency injection. Meaning that, when you write a class, simplify specify a setter method, and somehow, during runtime, it will magically be there. Simple. A lot of works are done through autowiring ( magically finding what class needs to be injected where ) and so on. With the new release ( 3.0 > ), Spring is more developer friendly with its use of annotations ( @annotation ) where the configuring of spring xml has been reduced to almost a few lines.

I am still much too new to Spring and therefore, have much to learn. I have discovered that writing persistence classes can be made recognizable by adding ( @Entity, @Repository, @Service ) and model view controllers by ( @Controller, and the corresponding jsp), which brings a more dynamic approach to web development in java. Those, annotation are not necessarily from Spring ( Java Servlet 3.0 ) but combined with Spring, the possibilities are limitless.

So, for all those Java programmers out there, do try Spring out and let me know what do you think :)

Monday, October 31, 2011

Threading ( Condition Variable ) : Best Example

In today's class my professor was explaining how Condition Variable works.
So your thread is now sleeping. Then some event comes calls it to wake up. Wake up1 Wake up! Wake up! It is like having a roommate. The alarm goes off at 4.00am then both of you wake up. Then one of them says, "I have to go class now ( condition satisfied ) " and the other "This guy! Gosh... " and goes back to sleep. But there is a overhead from him waking up to trying to go back to sleep.
This post might only make sense for someone  who has prior knowledge of Threading works and since its a broad topic, I decided not to cover it myself. Googling might yield better result. I just thought his explanation was funny and decided to share!

Friday, October 28, 2011


A couple of months ago ( actually 2 semesters ago ) I had a discussion with my friend, TM Lee about an idea I had. It is basically a site that will have made up words. It sounded like a cool idea but for some reason( I was still fairly new to Rails ) I couldn't get it done and somehow got to procrastinating.

Finally I decided, it is about time that I got myself to finish this site up, thanks to inspirations from a variety of sources. Finally after a few months ( I am in school so I could only dedicate a few hours at most each week ) to get to it. FInally a public friendly simple prototype is ready. Please visit

At first I was kinda nervous how people would perceive this site, since this would be my first public app. To my delight, the site has been well received. When I was designing this site I only had two words in mind: Simple and Fun. I am glad that the users of this site able to add words in a very simple manner and do so with a lot of fun to it.

Naturally, being my friends, a good few posts were a spin off using my name, but I think that is ok. After all, it is a site where you can make up words in the name of fun and hopefully doesn't offend anyone.

Anyway, I am super excited to see how this site would grow and if necessary, I might even pull off a version 2.0. This site is definitely venturing off from my comfort zone ( Java for desktop ) to a much newer cluster of Rails and Web Development!

That's all for today. To everyone whom contributed to aniktionary word base, THANKS!

Saturday, October 22, 2011

Blogger Dynamic View And New Interface

Recently I gave bloggers dynamic view a try and it is disappointing

  • It is slow
    • I am never a person who likes slow or choppy animation and that is exactly what the blogger dynamic view does. There animation is not pleasing and it feels too slow. It takes too much time to show the window. I am not sure if it is just poor implementation of JavaScript or that was the intended result. I have tried loading the site on Firefox and the performance deteriorates.
  • Too many choices
    • It is interesting to have a view customizable by the users but how many time a user does so? When I add an additional page, the options becomes drop down. Though I could specify which should be the default view, I believe the options saves little or no purpose. Furthermore, this is not what I want to communicate to my readers. My readers should see how I perceive things and comment on whether its good or how it could be done differently. By giving my readers more choice I am failing to do this.
Other than that, I have no complaints for the dynamic view. I just wish they could do it right and not just putting out there for the sake of introducing new features. Having said so I do like the new blog template and view.

My first impression
  • Spacious
    • I never knew blogger could be so spacious. I use a 23inch monitor and this means a lot to me. I no longer have to deal with cluttered interface
  • Highlighting the important 
    • The new interface brings focus to what is important and blends out what is not. The blogger interface i am using to write this very post seems more welcoming and gives me a feel for proper text editing feel
  • Just nice
    • The new interface is not fancy and just nice
    • It has minimal or no distractions and gives me exactly what i want just the way i want i
  • Syntax highlighting and indentation
    • though not very important, it would be nice to have those feature when i am composing a blog post

Overall, I am pleased with the way blogspot grew and I wish google would do similar theme across all their other services as well. 

Monday, October 10, 2011

Rest In Peace Steve

Like the rest of the world, I too have been shocked by the loss of Steve Jobs. Though, I am not a die hard fan of Steve, I have come to like his boldness and his need for perfection. His soul, his ideal is evident from the products that Apple ships. Personally, I believe he is the renaissance man of the 21st century, not only an excellent CEO, he is an excellent promoter who can make you drool for the product and give you the urge to buy it. He is an excellent quality control person too where only the products he is satisfied with , ships. Like he said during a KeyNote, this product won't have advertisements in them because he don't want advertisement. He is also an excellent negotiator as he can steer the company in the direction he wants without sacrificing his ideals. Finally he is a visionary that can see years ahead and present the product the generation needs at the moment it needs. This has been tested thorough time and remember the complaints when iPad first came out, and just look at the sheer volumes of iPad that has been shipped. Of course, there can be much said about him and much to be learned from him. He may not be the best, but he is a role model that I want to learn from and I can't wait to get my hands on his autobiography. Above is the simple poster that I designed using the minimalistic design that Apple champions and together with Helvetica, the typography that changed Steve's life. May your soul rest in peace.

Monday, September 19, 2011

Xoom Tablet : Android 3

Recently I joined a class called Android Tablet Programming. I was given a brand new Xoom to test with. And...... Let the magic begin!

My first impression

  1. The tablet is responsive
  2. The native apps are quite fast 
  3. The tablet is slightly heavy but that's alright
  4. The keyboard still feels awkward
  5. I can't read my PDF~!
    1. I download Acrobat Reader but it is only responsive in continuous scroll mode
Given that the tablet has a powerful processor and graphics card, it seems only natural to expect a smoother reading experience. Oh well, moving on...

So I decided to test some basic graphics by overriding the onDraw method of a view. The results 
  • It is fast when I ran simple graphics
  • Slow when I try to animate ---> I did not utilize the multi threaded
Then I went ahead to test the OpenGL capabilities of the device and I realized OpenGL on a mobile device is very different from openGL of a desktop / laptop app. As I was struggling to understand, I found the following book. Pro Android 3.

In Chapter 20, the book gave good foundation on OpenGL on a mobile device ( OpenGL 1.0 and 2.0 ). It went ahead explained the fundamental concepts, which a lot of tutorials out there assumes you are familiar with. Finally, the book proposed ways to abstract the common tasks to simplify the rendering code. The book by no means covers OpenGL extensive ( there is only so much a chapter can contain ) but it gave the much needed mindset for OpenGL. I have attached a link for the book  in Amazon, feel free to have a look at it.

Long story short,
  • OpenGL for mobile devices is called OpenGL ES ( a subset of the OpenGL )
  • OpenGL 1.0 only lets you draw line,point or triangle while other shapes are derived
  • OpenGL 1.0 and OpenGL 2.0 is very different
  • OpenGL 2.0 can only be tested on actual device ( no emulators )
  • The animations are pretty fast
I am currently developing a small app just to test how much I can do with the device and we shall see how it goes.