Spring entry Blog [6. Spring Automatic Assembly annotation @ Autowired]

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.