Tuesday, June 17, 2008

Spring : Inversion of Control (IoC) / Dependency Injection (DI)

We have heard a lot about Spring in recent years. Specially it is one of an indespensable tool widely employed by Java developers. It has a kind of defacto standard as a container. Though Spring has lot of features to delve into, I am specially talking about the inversion of control and dependency injection in this blog. Here I am trying to define inversion of control/ dependency injection because what I found over the years is that eventhough developers are using IOC and DI they are little aware of what actually they mean. Though nobody needs a precise definition to use the features, I believe understanding them would be better.

Dependency Injection (DI) and Inversion of Control (IoC) are interchangable terms but I am going to define them from Injection and Control perspective. For example look at the class below:-


public class UserManager {
private UserDao userDao;

// code involving useDao

public void setUserDao(UserDao userDao){
this.userDao=userDao;
}

}


Lets assume that userDao is a spring bean injected into UserManager (I am assuming some familiarity with spring because I just want to make concepts of DI and IoC clear). Now interpret it this way: UserManager is dependent on UserDao for some operation> In traditional programming we might create the UserDao instance for use in UserManager like this UserDao userDao=new UserDao() but not as such with Spring. The UserManager is not responsible for creating the dependency that it requires itself but rather depends on Spring to inject its dependency.
Hence the name Dependency Injection.

Now for IoC, see it this way: the control of creating the object necessary for UserManager has been transferred to Spring. The control that traditional objects use to have has been lost and control has inversed. The owner uses the object created by something else. Hence the name Inversion of Control.

Monday, June 16, 2008

Setting up SVN+SSH Protocol in Eclipse

In order to be able to use SVN+SSH with Eclipse Subversion plugin you need to do the following:-

1. First open following file named config in the text editor. The config file is located in the user's Application Data\Subversion directory.

For example if the user for which the subversion client is installed is svnuser then the file path should be:-

C:\Documents and Settings\svnuser\Application Data\Subversion\config

2. In the file locate commented #ssh and uncomment it and set the path to the path of TortoisePlink.exe (you need to have TortoiseSVN installed). You can either set the path as an environment variable or directly put the path in the config file. If you put the path in environment variable you need to point to the variable in config file like as shown:-

ssh= $SVN_SSH ssh

where $SVN_SSH is the environment variable name

If you want to directly point to the executable in the config file do it as follows

ssh=E:\\TortoiseSVN\\bin\\TortoisePlink.exe

TortoiseSVN is not the only option. Actually, we can use any ssh connection tool that supports same command line parameters as the SSH executable available in Unix.

To sum it all here is an extract from the config file
### as:

#ssh = $SVN_SSH ssh
ssh=E:\\TortoiseSVN\\bin\\TortoisePlink.exe
### If you wanted to define a new 'rsh' scheme, to be used with
### 'svn+rsh:' URLs, you could do so as follows:



This is what we need to do at the config file to start using svn+ssh. But sometimes I have found that eclipse plugin continuously keeps on asking for password. This occurs when the the SVN client used in eclipse is Default Native Java HL . So change it to Java SVN.

If you don't know where those changes are to be made follow following detailed steps:-
a. In Eclipse go to windows menu.
b. Click Preferences and expand Team option.
c. After expanding Team you will find SVN option click on it.
d. On the right side you will find the SVN client setting. Select the option as stated above and restart the eclipse (though restart might not always be necessary).