The @autowired annotation, it can be to a class member variables, methods and constructors annotate, complete automatic assembly work, through the use of the @autowired to get rid of the set, the get method, before using the @autowired, we attribute in the allocation of a bean, is the use of the
By this way, the configuration is tedious, and the code is more, in the Spring 2.5 introduces the @autowired annotation
The following use case to specify
UserRepository. Java
Copy the code
1 package com. Proc. Bean. The repository;
2
3 public interface UserRepository {
4
May void the save ();
6}
Copy the code
Here defines a UserRepository interface, which defines a save method
UserRepositoryImps. Java
Copy the code
Copy the code
1 package com. Proc. Bean. The repository;
2
3 the import org. Springframework. Stereotype. The Repository;
4
5 @ Repository (" userRepository ")
6 public class UserRepositoryImps implements UserRepository {
7
8 @ Override
9 public void the save () {
System. 10 out. Println (" UserRepositoryImps save ");
11}
12}
Copy the code
Copy the code
Define a UserRepository interface implementation class, and implement the save method, specifies the bean here in the IoC identifier name for UserRepository
UserService. Java
Copy the code
Copy the code
1 package com. Proc. Bean. The service;
2
3 the import org. Springframework. Beans. Factory. The annotation. Autowired;
4 the import org. Springframework. Stereotype. Service;
5
6 import com. Proc. Beans. Repository. UserRepository;
7
8 @ Service
9 public class UserService {
10
11 the @autowired
12 private UserRepository UserRepository;
13
14 public void the save () {
15 userRepository. The save ();
16}
17}
Copy the code
Copy the code
A UserRepository type attribute is needed here, through the @autowired automatic assembly way, to look up to, from the IoC container and returned to the attribute
The applicationcontext.xml configuration
The test code:
1 ApplicationContext CTX=new ClassPathXmlApplicationContext (" the applicationcontext.xml ");
2
3 UserService UserService=(UserService) CTX) getBean (" UserService ");
4 userService. The save ();
Output: UserRepositoryImps save
So what is the use of the principle of the @autowired?
Actually when start the spring IoC container automatically loaded with a rear AutowiredAnnotationBeanPostProcessor processor, when a container scanning to @ Autowied @ the Resource (rear CommonAnnotationBeanPostProcessor processor) or @ Inject will needs in the IoC container automatically find bean, and assembly to the object's properties
Note:
When using the @autowired, first query in container corresponding type of bean
If the query results for a just, give the bean assembly to the @autowired specified data
If the results of a query is more than one, then @autowired will according to the name lookup,
If the query result is empty, then throws an exception, solution, use the required=false
For example:
In the above example, we set in a class to implement interface UserRepository
Copy the code
Copy the code
Package com. Proc. Bean. The repository;
The import org. Springframework. Stereotype. The Repository;
@ Repository
Public class UserJdbcImps implements UserRepository {
@ Override
Public void the save () {
System. The out. Println (" UserJdbcImps save ");
}
}
Copy the code
Copy the code
After start the container at this moment, in the container, there are two UserRepository types of instances, a name for the UserRepository, another for userJdbcImps, in UserService
The @autowired
Private UserRepository UserRepository;
Output: UserRepositoryImps save
Here because there are two queries to the type of the instance, then adopts the name matching method, name lookup for userRepository instances in the container, and automatic assembly for this parameter,
If want to load userJdbcImps instance here, in addition to the field userRepository name to userJdbcImps, can provide a @ the Qualifier tags, to specify need to assembly the bean's name, code writing
1 the @autowired
2 @ the Qualifier (" userJdbcImps ")
3 private UserRepository UserRepository;
Output: UserJdbcImps save