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 use a Servlet, JSF uses a el-resolver, standalones use the main method, etc. The entry point bootstraps the application context and autowires all beans.
Autowiring happens by placing an instance of one bean into the desired location 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 folloing:
@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") 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 or UserService
, it will be injected.