Blog
-
Spring and Logback
IoC, or DI definitely takes a perspective turnaround inside your head, but you get around it slowly. I was playing around ways to integrate LogBack, and Spring – essentially around having Spring give me a pre-created instance of LogBack logger.
I searched around posts, but most people seem to be against using Spring just for substituting one-line of Logback (
Logger log = LoggerFactory.getLogger("LogbackTest");
) to get your logger.I, on the other hand, was more interested in how to get Spring give me a LogBack logger instance without too much contrived hand-written code to achieve so. And with a bit of reading through Spring principles, documentation I found the way.
Essentially, when using Logback’s LoggerFactory you have access to only a single getLogger() factory method. This is static which makes things a bit different for what Spring would call a bean – a class providing constructor, getter, setter methods. To circumvent this non-bean style, Spring provides what you call as static initializers a.k.a substitutes for constructors, which allow you to call a static method in lieu of calling a constructor on an object.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="bean1" class="org.slf4j.LoggerFactory" factory-method="getLogger"> <constructor-arg value="LogbackTest" /> </bean> </beans>
Now, this bean1 can be used as a regular bean inside your class
ApplicationContext ctx = new FileSystemXmlApplicationContext("logbacktest.xml"); Logger log = (Logger) ctx.getBean("bean1"); log.debug("This is my first message"); log.info("How about this information message");
Throw in a logback.xml in your classpath, and viola you have a nice Spring injected dependency – log in your code, while still using Logback!
-
Spring IoC, DI quick tutorial
There has been an (evident) craze amongst Java community with contrived terms such as IoC (Inversion of Control), DI (Dependency Injection) mainly through Spring. I initially found the connotation “Don’t call me, I’ll call you” a bit difficult (yeah, it’s bending your head upside down) to understand, but after spending few hours around Spring documentation, I get the gist.
To a layman (Java layman, of course) it is helpful to picture that each Java program is a set of one or more classes, which act together. Essentially, this means classes are “dependent” on some classes in order to be fully functional. Usually, the class requiring functionality of another class instantiates the class, and uses it. This is called coupling because class instantiates the object of required class. What if we always got an “instantiated” instance of required class, and our class did not have to worry of instantiation? – This is called IoC (Inversion of Control) principle in Spring, and it achieves this by providing ready-to-use instance (injecting dependency) to your class. This has some important uses, since now you don’t worry of creating connections to databases, loggers to log4j, or sessions for JMS queues. Spring will create these for you, and your class can focus on the actual purpose – using the pre-instantiated ready-to-use object.
Get it? OK, to simplify it further, let us assume you have a class A having one field – log. You want to use log to log information, but your class nowhere has the logic to instantiate or initialize log. You accept a pre-instantiated log through constructor, or getter/setter methods, and you will have a ready-to-use log instance passed to your class via Spring!
I won’t go into details of Spring further, since Spring’s own documentation on http://static.springsource.org/spring/docs/2.5.x/reference/index.html is the definitive source to look into.
-
Interesting books – Crystal Skull
Recently, I read an interesting book. Was an good read, after a long time focusing only on technical books.
Is this for real?
-
Javascript sprite animations
Javascript sprites weren’t as difficult as I thought. With a little bit of help, math, some css, js, images (of course) a decent animation can be shown without using any flash at all. I coupled it in a small class Animator, and wrote a small demo for anybody interested in using it.
-
Getting out of telnet
You telnet an ip, port but you cannot get out? Ctrl+C doesn’t work, Esc doesn’t work !
Duh, Ctrl+C, Esc are the commands sent directly to the ip, port you just telnet-ed, so telnet doesn’t see them at all !For this you need to return back to telnet prompt, so hit
Ctrl+] (yes, Ctrl and right square bracket)
telnet> quit
Connection closed.Hope this helps
-
Happy Birthday Nicolas
My dearest son turns 2 today ! It’s been a hell of a tough time these 2 years (wonder what’s next?…), but his gentle smile makes me forget all that. Happy Birthday to you my son !
Love, Papa & Mama (who’s fast asleep now … giggle)Once again, Happy Birthday to you Nicolas !
-
My first website – nostalgic
I stumbled yesterday upon my first website I had created. This sure brings back memories on my old desktop PC, and keyboard sitting, and typing away at late nights 😉
http://www.naiksblog.info/shantibhushan/default.htmlOn another note, it does have some useful notes on Chasen (Japanese language morphological analysis tool), Mecab, Using Python with Chasen, Cygwin etc.
-
Most handsome boy – my son Nicolas
Wouldn’t you agree?
-
Outlook export to Thunderbird
There is no straight (should I say “easy”) way to export Outlook mails to Thunderbird. Using Thunderbird’s import from Outlook did not work since quite a few emails came up as raw HTML, and I had to manually change Automatic encoding detection to OFF, Universal to see the contents each time.
A sure-shot (well… which took me 99% of the) way was Outlook –> Import into Outlook Express –> Import into Thunderbird. This worked, but with one sad issue – non-English attachment names are not as original. If you can live with that, then this is the surest way to go !Few references which helped-
Import .pst files – MozillaZine KB