Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does autowiring work in Spring?

I'm a little confused as to how the inversion of control (IoC) works in Spring.

Say I have a service class called UserServiceImpl that implements UserService interface.

How would this be @Autowired?

And in my Controllers, how would I instantiate an instance of this service?

Would I just do the following?

UserService userService = new UserServiceImpl(); 
like image 212
Blankman Avatar asked Jun 30 '10 21:06

Blankman


People also ask

How is Autowiring done in Spring?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.

What does @autowired do in Spring?

Starting with Spring 2.5, the framework introduced annotations-driven Dependency Injection. The main annotation of this feature is @Autowired. It allows Spring to resolve and inject collaborating beans into our bean.

What is @autowired and how it works?

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.

What is the difference between @bean and @autowired?

@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).


1 Answers

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, JSF uses 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 that matches with an existing bean automatically get 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.
like image 122
Bozho Avatar answered Sep 29 '22 21:09

Bozho