Books

Monday, June 09, 2014

How to Autowire a Bean That Requires Constructor Argument In Spring

Spring allows you to inject a managed object (bean) as a dependency into another object via the @Autowired annotation.

For example, if I have a UserService that has a dependency on UserRepository, I can have the UserRepository injected using @Autowired annotation like this:

UserRepository Class...
class UserRepository {
UserRepository () {}
}


UserService Class...
class UserService {

@Autowired
private UserRepository userRepository;

UserService () {}

}

This is done using Field Injection. The same thing can be accomplied using Setter Injection:

class UserService {

private UserRepository userRepository;

UserService () {}

@Autowired // Using setter injection
public void setUserRepository(
UserRepository userRepository) {
this.userRepository = userRepository
}

}


or via Constructor Injection:

class UserService {

private UserRepository userRepository;

@Autowired // Using constructor Injection
UserService (UserRepository userRepository) {
this.userRepository = userRepository
}

}


There are different opinions on which method is "the right way" but since this post is not about that debate. I would point you to this post instead: Why I changed My Mind About Field Injection

Which ever method you choose to use, you are essentially doing the same thing: instructing Spring to supply an object's dependency by using the @Autowired annotation.

But what happens when the Object you want injected in has a constructor that requires an argument?

Making Aliases Permanent


Aliases provides you with short cuts to commands or group of commands.

..For example, instead of always having to type ls -la to get the long listing format of a directory's content, you can set up a short form, say ll and type that instead. You do this via aliases by typing in an open terminal this:

alias ll='ls -la'

It would set up an alias of ll that equates to 'ls -la'. As you see the format of setting up an alias is:

alias alias_name="command"

The only issue with this, is that the alias is lost once you end the terminal session. i.e. once you close the terminal. To make the aliase permanent you need to add it in a place where it gets loaded and available at the start of a terminal session.

The most appropiate place to put the aliases in other to make it permanent is in an hidden file called .bashrc in the user's home directory. Or better still, put it in a file usually named as .bash_alaises and have it refereced from .bashrc

This is straight forward.

with vim ~/.bashrc




and you add the aliases in the .bashrc file as shown above. The other, more prefered way is to create another file, usually named .bash_aliases and put your aliases inside instead.

The reason this works is due to the presence of a code section in .bashrc that loads the content of the .bash_alaises.

The portion of the .bashrc looks like this:


If you do not have this piece of code, feel free to add it.

All this has been easy, straight forward and clear enough. The confusing part to all this, especially if you are new to linux, is the seemingly plethora of other files you can put your aliases in and all mysteriously seem to get the job done.

If you do a google search you may find results that points to other files where you can define aliases. /etc/profile, ~/.profile, ~/.bash_login, .bash_profile

The reason these all seems to work is because they are all special kinds of files that have their contents sourced by the shell at different point in time. Some are sourced at login, some are sourced when the shell is invoked interactively.

To really understand how they all fit, lets go over some basic stuff here: