If you’ve been working with Spring MVC, you already know this. Whenever you create a new Controller you end up injecting the services or DAOs (if you’re not using the service layer) that you’re going to use. It gets to be a little cumbersome and repetitive. It could be improved. How? By defining a Spring aware factory.
First, the problem. There are several problems or inconveniences. First, you’re injecting the same service or DAO everywhere. If you’re checking if it actually was injected, then you’re duplicating that code. A violation of best practices. If you’re not used to inversion of control, it tends to be confusing. You’re asking yourself, where did this object come from? Any time you write a new controller, you also have to write some more XML and you don’t get syntax checking (that’s my number one complain about Spring). You’re braking the DRY principle: Don’t Repeat Yourself.
Is there a solution to this? Yes, there is. A nice one, actually. I got the idea from Hibernate Quickly, which I’m currently reading. You create a Factory (best practice, anyway) and you use that to retrieve your services or DAOs. No injection, no Spring. But how do you read the beans from the Spring XML files? The factory will implement ContextAware interface. (You have to also configure the Factory as a bean in the XML file for this to work.) Once you do that, you have access to all of the beans you defined. Nice and easy.
Here’s an example bean that you’ll end up with:
public class AppServiceFactory implements ApplicationContextAware {static Log log = LogFactory.getLog(AppServiceFactory.class);/*** Set by Spring when initialized.*/private static ApplicationContext appContext;public void setApplicationContext(ApplicationContext applicationContext) {log.debug("Setting application context [" + applicationContext + "]");appContext = applicationContext;}static ApplicationContext getAppContext() {if (appContext == null)throw new IllegalStateException("Application context should have been set");return appContext;}
private AppServiceFactory() { /* singleton */ }
public static AccountService getAccountService() {String serviceName = "accountService";AccountService service =(AccountService) getAppContext().getBean(serviceName);validateService(service, serviceName);return service;}
static void validateService(Object service, String name) {if (service == null)throw new IllegalStateException(name + " has to be configured.");}
// ...}
What are the benefits fo doing it this way? It’s a whole lot easier to understand. You’re asking the factory to give you the DAO or a service. It’s easier to debug. It follows the DRY principle — you’re not re-injecting the same bean all over the place. It’s easier, and faster: You define the controller in the XML config, you create it, and just use the factory in the controler. Don’t need to inject anything, no extra configuration for other controllers. You’re following the DRY principle. The service is retrieved and set in only one place. If there’s any validation you need to do, you’ll do it in one place. It works nicely for me.
Spring In Action
by Craig Walls, Ryan Breidenbach
ISBN 1932394354
Date Read 8/2005
My Rating
Spring in Action is a well written overview book on the Spring Framework. It could have been better, though. Key words: well written overview.
It is an overview. This book covers the different areas of the Spring Framework. The authors do an excellent job there. However, they don’t go into too much detail about any particular topic. They show you a simple example of each part, talk how you can use it, etc. Not too much details.
It is well written. Spring in Action is one of these books that you enjoy reading. It is a technical book but almost reads as a general book. Very good style. Authors did a good job in that regard.
This book tells you a great deal about best practices. It does a good job in that regard as well. However, this book does not cover testing, which is considered a big thing in developement, and Spring makes that easy (supposedly; I’d like to see that in action). How come I can’t find that in the book? Examples are short and in snippets. I like to see full examples, though, as it is easier to see the context.
Overall, a good book on Spring. A great intro to Spring. If you’re new to Spring, this is the book to start with. If you need a reference, this book is not optimal. You might want to look into Pro Spring, which is a much more detailed book.
|
The critical distinction between a craftsman and an expert is what happens after a sufficient level of expertise has been achieved. The expert will do everything she can to remain wedded to a single context, narrowing the scope of her learning, her practice, and her projects. The craftsman has the courage and humility to set aside her expertise and pick up an unfamiliar technology or learn a new domain.
–Dave Hoover
in article on StickyMinds.com |
This is a great definition of a software craftsman: the best I found so far. The article by Dave Hoover on StickyMinds.com is a very good one, link below. Especially if you want to find out who a craftsman really is.
What can you learn from this? Be humble. Be curious. Be eager to learn new technologies. And be lazy.
ReferenceExperts, Craftsman, and Ignorance by Dave Hoover
Related PostEvery Craftsman is Dump and Lazy
Software writers I love. There are only a couple of them. Paul Graham is in this group. (Steve McConnell, Martin Fowler, Robert Martin are the rest (out of top of my head)). His essays are always top quality. They are filled with some great, practical information. They are based on his personal experiences. They are really valuable to read.
One of the recent articles from Paul Graham, How To Start A Startup (see my post on it), was great. It’s filled with practical info. It’s well written. It’s complicated info explained in simple terms.
Another one I really like is Beating the Averages. (This one I plan to re-read every year.) If you want to be a successful company, a successful developer, beat the average guys. There is a lot of good information in this article.
I have not read all of Paul’s essays yet, but I’m sure they’re top quality. Paul publishes an essay a month, roughly.
I read Paul’s essays because they’re filled with useful, practical information. I read his essays because I like to read the best. I read Paul’s essays because I can see best writing in action (good way to improve — he even has an article on how to improve your writing , Writing, Briefly). Not enough?
Reference[1] PaulGraham.com — His website[2] Paul Graham’s RSS Feed
It’s not for me. At least for now. I’ll reconsider after I’ve had 7-10 years of experience (I have 5 now). But it’s definitely something that’s on my mind. One of the ideas that I got from reading this article, is to consult when I’m nearing retirement (I would love to retire in my early 50s). I would be able to work part time, get some money, and do what I like to do. Not a bad idea. Hey, maybe I wouldn’t have to retire?
Anyway, the following is a very good article.
Considering a Consulting Career by Katherine Spencer Lee in CW.
The Best Software Writing I
by Joel Spolsky
ISBN 1590595009
Date Read 7/2005
My Rating
If you have been active in the software world, you probably have read most of the articles that are in this book (I had read around 40-50% before reading this book). In that case, this book will not be as beneficial to you, but you’ll enjoy it.
On the other hand, if you don’t know who Joel Spolsky is, or Eric Sink, or Paul Graham — this is a must read for you! You will get introduced to some of the finest writers in the software domain. This book will help get on track.
The one essay that I had not read before, and that left the most lasting impression on me is the EA: The Human Story, which talks about the horrible working requirements in the EA — the computer gaming company. Wow.
The article on outsourcing is great. Most of the articles are high quality. Most are worth reading.
It’s a weekend book. It’s a book that you take with you to the beach. I read the whole book while on vacation in Spain — it’s not technical, easy to read.
Great stuff, Joel.
If you don’t feel like buying the book, you can find the whole list with the links to all of the articles here.
Yeah, that’s true.
Craftsman is lazy because he does not want to repeat what he wrote before.
Crafstman is dump because he knows that if he’s not, he’ll stop learning and stop being critical of his work. Both are essential for being successful.
It’s good to be dump and lazy after all, huh? So be dump, be lazy — those are qualities of the best!
Read the post, link below, by Jeff to see more explanation. Great stuff.
ReferenceHow to be Lazy, Dumb, and Successful by Jeff Atwood (Coding Horror blog)
|
The reason you write a spec is not to solve every possible problem in advance: the reason you write a spec is to solve as many problems as you possibly can in advance so that you minimize the number of surprises that come up during development.
–Joel Spolsky
- talking about Copilot.com spec |
Reference:Blog entry about The Project Aardvark Spec by Joel Spolsky
Related:The original spec (pdf) by Joel Spolsky
I read a lot of Java books, articles, and blogs. The articles by Yakov Fain in JDJ are always interesting. The latest, How To Pass A Technical Job Interview With Flying Colors, is very good as well.
Are you looking for a job? Do you have a lot of knowledge? Oh, yeah? You can have a great success if you IPO. No, this is not a stock-market entry. It is a career entry. Yes, you can IPO – Get the Interview (I), Pass the interview (P), and Get the Offer (O). Treat these separately and do them one at a time, Yakov argues. He goes on to explain the IPO in detail.
He has some very good tips:- tailor your resume for each employer;- prepare by creating interesting stories about a challenging project you’ve been part of;- be energetic and show interest;- look for a job while you still have a job and accept it only when it is better.
“Take charge of your career and actively build it the way you want, ” Yakov ends. Mr. Fain is right on the ball with that article.
Reference:How To Pass A Technical Job Interview With Flying Colors by Yakov Fain, JDJ, July 2005
Related:Sample Java Interview Questions
I did not attend JavaOne this year (I wish). Eric Armstrong, blogger at Aritma.com, did. Not only did he attend the conference, he put together a very good blog entry about the cool stuff that is happening in the Java world, as well as in the software world.
He does a very nice job. I totally agree with what he’s saying. He covers AJAX, Hibernate, J2EE, Groovy vs BeanShell (I might have to learn them).
Here is a summary of the entry:
Reference:JavaOne 2005: Wrap Up by Eric Armstrong