This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.
Question: How does Spring’s auto-injection work?
I’m a little confused about how Spring’s Inversion of Control (Ioc) works.
Suppose I have a class named UserServiceImpl that implements the class UserService interface.
How is the @AutoWired annotation injected at this point?
How do I instantiate this service in my Controller?
Can I use the following code?
UserService userService = new UserServiceImpl();
Copy the code
Answer a
First and most importantly, Spring Beans are managed by a container called Application Context.
Second, each application has an entry point to that context. Web applications use servlets, JSF uses el-Resolver, and so on. In addition, at some point, the Applicatoin Context will be initialized and all beans will be injected. In a Web application, this place could be a Startup Listener
Automatic injection is putting one bean instance into the fields of another bean instance. Both classes must be beans, that is, they should both be defined in the Applicable Context.
What does definition mean in the application context? This means that instances are created by the context, not by you, and you will never create objects using statements like new UserServiceImpl(). The container will find each injection point and create an instance there.
In Controller, all you need to do is:
@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 ituserService.login(username, password); }}Copy the code
Some points to note:
- in
application.xml
Configuration file, you need to enable<context:component-scan>
In order to scan the class@ Controller, @ Service
Such as annotations. - The application entry for Spring MVC is
DispatcherServlet
But it’s hidden from you, soapplication context
The direct interaction initialization takes place behind the scenes. UserServiceImpl
Should also be defined asbean
.
- use
<bean id =".." class=".." >
- Or use
@Service
annotations
This class can only be injected automatically if it is scanned by Spring.
In addition to the @Autowired annotation, Spring can configure automatic injection using XML files. In this case, all fields matching the bean name are automatically injected into the bean. In fact, this is the original meaning of auto-injection — automatic injection of fields without any configuration. Other annotations can also be used, such as @inject, @Resource.
Answer two
This depends on whether you use annotations or XML for configuration
Suppose you define beans in applicationContext.xml.
<beans ... > <bean id="userService" class="com.foo.UserServiceImpl"/>
<bean id="fooController" class="com.foo.FooController"/>
</beans>
Copy the code
Automatic injection also starts when the application starts. Annotate it in fooController as follows:
public class FooController {
// You could also annotate the setUserService method instead of this
@Autowired
private UserService userService;
// rest of class goes here
}
Copy the code
When the @AutoWired annotation is scanned, Spring will look for the class in the Application Context that matches the attribute and inject it automatically. If you have more than one bean named UserService, you need to restrict which bean to use.
If you perform the following operations
UserService service = new UserServiceImpl();
Copy the code
It will not be injected automatically until you configure the @autoWired annotation.
The article translated from Stack Overflow:stackoverflow.com/questions/3…