[Java] view plaincopy
Automatic Assembly:
@ Autowired: use automatic assembly. Automatically injects values in the bean container into the bean.
Case:
1. Java file:
Public class UserAction {
@ Autowired
Private UserService userService;
// The set method is still indispensable, because autowired is also injected with setter
Public void setUserService (UserService userService ){
This. userService = userService;
}
Public void addUser (){
UserService. HelloWorld ();
}
}
2. Xml file:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
Default-autowire = "byName">
<Bean id = "userDao" class = "com. spring. dao. UserDaoImpl"> </bean>
<Bean id = "userService" class = "com. spring. service. UserServiceImpl" scope = "prototype">
<Property name = "userDao" ref = "userDao"> </property>
</Bean>
<! -- You do not need to specify the property. You can specify autowire here. -->
<Bean id = "userAction" class = "com. spring. action. UserAction" autowire = "byName">
</Bean>
</Beans>
It is worth noting that. The above byName also has a method of byType.
And the scope of its annotation. It is not hard to see that there is a constraint in the declaration part of the xml file. This is a global constraint. It is set to avoid tedious repetitive work. I think it is easy to understand. I will not talk about it here
Author: zhang6622056