Skip to main content

@Autowired and @Service - Spring

First, and most important - all Spring beans are managed - they "live" inside a container, called "application context".
Second, each application has an entry point to that context. Web applications have a Servlet, JSFuses a el-resolver, etc. Also, there is a place where the application context is bootstrapped and all beans - autowired. In web applications this can be a startup listener.
Autowiring happens by placing an instance of one bean into the desired field in an instance of another bean. Both classes should be beans, i.e. they should be defined to live in the application context.
What is "living" in the application context? This means that the context instantiates the objects, not you. I.e. - you never make new UserServiceImpl() - the container finds each injection point and sets an instance there.
In your controllers, you just have the following:
@Controller // Defines that this class is a spring bean@RequestMapping("/users")public class SomeController {

// Tells the application context to inject an instance of UserService here
@Autowired
private UserService userService;

@RequestMapping("/login")
public void login(@RequestParam("username") String username,
@RequestParam("password") String password) {

// The UserServiceImpl is already injected and you can use it
userService.login(username, password);

}}
A few notes:
  • In your applicationContext.xml you should enable the <context:component-scan> so that classes are scanned for the @Controller@Service, etc. annotations.
  • The entry point for a Spring-MVC application is the DispatcherServlet, but it is hidden from you, and hence the direct interaction and bootstrapping of the application context happens behind the scene.
  • UserServiceImpl should also be defined as bean - either using <bean id=".." class=".."> or using the @Service annotation. Since it will be the only implementor of UserService, it will be injected.
  • Apart from the @Autowired annotation, Spring can use XML-configurable autowiring. In that case all fields that have a name or type matching the one of an existing bean is automatically gets a bean injected. In fact, that was the initial idea of autowiring - to have fields injected with dependencies without any configuration. Other annotations like @Inject@Resource can also be used.

Comments

Popular posts from this blog

SVN to GIT Migration with revision history

This blog explains the process I have followed while moving one of code branch of one of the projects to Gitlab. SVN to Git migration can be done using the git-svn command in Git.  Documentation about this command can be found from here. https://git-scm.com/docs/git-svn My Company svn repository can be accessed using two ways svn+ssh://192.168.x.x/usr/local/ svnroot/branches/PROJECT/PROJECT_BRANCH http://svn.companyName.net/repos/svnroot/branches/ PROJECT/PROJECT_BRANCH SVN to GIT migration can be done using one of the above-mentioned URLs.  Git can be installed and configured to use its inbuilt SSH client or third party ssh client ( https://6xgate.github.io/TortoisePlink/ ) First of all, this process should be run in a server where the system does not go idle. I have tried this on my PC. But when I have locked the PC, it crashes. Therefore I have tried this in one of our internal server. I have tried to get SVN revision history when the P...

Java 11 Modularity and Spring REST

I have recently started working on creating a REST API for a client project.  I was using Java 8 for my past developments. Since Java 11 was introduced in the last September  (https://en.wikipedia.org/wiki/Java_version_history) I thought of developing the REST API in Java 11. Java 11 is having major changes after Java 8. Java 11 is having a concept of modules (This was introduced after Java 9). Several libraries are acting as separate modules. Those are not included in JDK and those have to be imported separately. Ref : http://openjdk.java.net/jeps/261 (I have experienced this in JBoss7 also. They were using the module system and each service can specify which modules which are using. Those modules can be manually configured by the user.) One example is that you have to import the Java fx library separately (For GUI). Java-Fx is decoupled from JDK. <!-- https://mvnrepository.com/artifact/org.openjfx/javafx -->   ...

President - Association of Computer Engineering Students

I was selected to be the president of ACES, Association of Computer Engineering Students, the official student body of the Department of Computer Engineering Students. ACES Site :  aces.ce.pdn.ac.lk  It was a great opportunity to work with fellow undergraduates, lecturers, senior graduates, and industry. Until now We were able to organize few events in University of Peradeniya and Colombo. This event was organized by ACES Council, a group of 14 undergraduates. The events are, ACES Hackathon - This is an inner hackathon for undergraduates in the University of Peradeniya. ACES Hackathon, held annually for the last 5 years was held from the 29th of April to the 1st of May 2016 at the Faculty of Engineering, University of Peradeniya. The Hackathon brings together undergraduates from the Faculty of Engineering and Science for 3 days to develop their creative skills to provide innovative IT solutions. This year there were 150+ participants.   www.readme.lk/saw-ac...