Tag Archives: spring

Dependency Injection

/* Vietnamese: Bài viết này nhằm mục đích giải thích một cách đơn giản nhất khái niệm Dependency Injection – một khái niệm mới mẻ những đã khá phổ biến trong thế giới lập trình hiện nay */

To my readers (if there’s any yet): I have written this blog for a while. At first, it should reflect my learning process – not all, but every post is a mark somehow. For over half of a year, now I think it’s best to make many short posts instead of lengthly ones. Thank all of you for your support.

Dependency Injection is a fairly new concept. Its origin can be traced back to Rod Johnson – the founder of Spring Framework. But we won’t go to history lesson now, let’s skip to the content itself.

The basic idea of “Dependency Injection” lies on the problem: how we should manage the dependency between modules(in the simplest case, object) in a large system.

An object should not initialize its dependencies. Dependencies of an object should be given to it by the environment.

Let’s see the following example:

Naturally, when a module A(Printer) need module B(PaperService), the original OOP thinking styles should produce something like this:

class Printer {

PaperService paperService;

public Printer () {

paperService = new PaperServiceImpl();




But by Dependency Injection way, it should be like this:

 class Printer {

PaperService paperService;

public Printer(PaperServiceImpl service) {

this.paperService = service;




Warning about primitive ModelAttribute

Recently while working with Spring Framework 3.0, I stumbled upon a trap that might be a bug of Spring Framework. It costs our team lots of time & efforts, since the bug is like a mystery that happens randomly. Eventually we decide to take a work around, but it’s still take lots of time to change the way the system was working.

The mystery is called Model Attribute, a handy utility supported by Spring 3.x.

Personally, I like this feature a lot. The ModelAttribute Annotation way gives me the ability to declare and init a variable just once, then it will be automatically added into model of any action. For example:

public User getCurrentUser() {
return userService.getCurrentUser();

But a problem happen here & there when I use primitive type (long, int, …) or even their wrapper type (Long, Integer,…) for the ModelAttribute. An error just pop out of nowhere:

java.lang.NoSuchMethodException: long.()<init>

Following my senior hint, I even comes to the code of Spring Framework, and actually see that it try to call the default constructer of Long type. Since that constructer does not exist (certainly), it fails and brings up the error. The headache is that this thing does not always happen: I still can not figure what trigger it up. But the fact that it’s out there make me & our team change the course of the program: instead of an tidy assignment, now we need to call the same function again & again whenever we need that modelAttribute (almost always in my case).

I have put a question out there in StackOverflow, but it still get no-good answer. I wonder if I should raise a bug for Spring… but until then I think that this “brilliant” way shouldn’t be used.

Hope this post is useful for anyone come across the same problem.

Finding key issues of the framework

Website System

1. How to learn a framework fast?

2. To learn a framework, you should grasp the “key” issues. But how to know what is a key issue?

Those two questions are pinned in my head somehow.

Not long ago, I’m investigating MVC model in Spring Framework 3.0. Most of the concept is fine, but I have problems doing the work with Spring 3 annotation config. It just seems all the book out there mention about Spring 2.x syntax, which is “xml configuration”. It’s easy to understand because that Spring 3 just appear and it will take a relative time to make a book available. So my habit about following book guides was discarded aside. I must try to find another source.

According the advice of my seniors, I go to the Spring document & find most of the thing is already here. Enough details, though a bit complex to a not-quite-familiar-with-java coder. I struggle with those concept a bit. But by comparing the document to several examples, finally I got some “working things”, which is great.

But here comes the most complicated part: integrating between frameworks. In one of my projects, I should use Spring combined with Freemarker. I investigate, and luckily enough, find this page which tell me how to use Freemarker macro to perform bindings (it’s not an easy luck).

But WTH, I can’t find a way to use the formCheckboxes! I found no working sample, and I’m not alone.

Dozens people out there can’t find a way to use that macro.

And the solution?

Well, I write my own macros.

It took me quite a time before I relize that simple solution, and curse how stupid I am. Since the end results of freemarker is HTML, who cares if it was rendered by supported macro or customed macro?

When you know what actually happens inside that big system, the answer seems to be natural.

Still remember 2 questions at the beginning of this post?

I must says the truth, that I don’t know the best answer. But I have something that seems to work:

The solution depends on the needs. If you just use the surface of the libraries API as the common “players”, some popular tutorials might be enough. Parsing XML maybe a good example for this issue. With the same reason, I think with beginner, Grails (instead of Spring) is a good start, cause it requires little work to make a full-fledge simple application.

If you want to create a customized & not-so-popular web application, it requires more tons of work. And a solid background is preferable.