Spring Annotations - @ Autowired

@Autowired annotation, it can be labeled class fields, methods and constructors, the automatic assembly work is completed. By using @Autowired to eliminate set, get method. Time before use @Autowired, we configured from the properties of a bean, is so used

< Property name = "attribute name" value = "attribute value" />    

In this way, the configuration more complicated, and more codes. In Spring 2.5 introduces @Autowired comment

 

The following use cases to specify

UserRepository.java

1 package com.proc.bean.repository;
2 
3 public interface UserRepository {
4     
5     void save();
6 }

This defines a UserRepository interface which defines a save method

 

UserRepositoryImps.java

Copy the code
 1 package com.proc.bean.repository;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository("userRepository")
 6 public class UserRepositoryImps implements UserRepository{
 7 
 8     @Override
 9     public void save() {
10         System.out.println("UserRepositoryImps save");
11     }
12 }
Copy the code

UserRepository define a class that implements the interface, and implement the save method, where the bean in the specified identifier name userRepository IoC

 

UserService.java

Copy the code
 1 package com.proc.bean.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import com.proc.bean.repository.UserRepository;
 7 
 8 @Service
 9 public class UserService {
10 
11     @Autowired
12     private UserRepository userRepository;
13     
14     public void save(){
15         userRepository.save();
16     }
17 }
Copy the code

It should UserRepository a type attribute, by way @Autowired automatic assembly, to find from the IoC container, and returns to the attribute

 

applicationContext.xml Configuration

<context:component-scan base-package="com.proc.bean" />

 

Test code:

1 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2 
3 UserService userService=(UserService) ctx.getBean("userService");
4 userService.save();

Output: UserRepositoryImps save

 

So what use @Autowired the principle?

  In fact, at start Spring IoC, AutowiredAnnotationBeanPostProcessor container automatically loads a post processor, when the container is scanned @ Autowied, @ Resource or @Inject, it will automatically find the bean needs IoC container, and fitted to the object's properties

 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  

  

 Precautions:

  In use @Autowired, corresponding to the first type of query bean in the container

    If the query results are exactly as one would assemble the bean specified data to @Autowired

    If the result of the query is more than one, it will be to find @Autowired by name.

    If the result of the query is empty, then throws an exception. Solving method, required = false

 

for example:

 In the above example, we set a class to implement an interface UserRepository

Copy the code
package com.proc.bean.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcImps implements UserRepository {

    @Override
    public void save() {
        System.out.println("UserJdbcImps save");
    }
}
Copy the code

  After this time the starting container, there are two examples of the type of UserRepository in the container, a name userRepository, the other is userJdbcImps. In the UserService

@Autowired
private UserRepository userRepository;

  Output: UserRepositoryImps save

  Here are two examples due to the query type, the matching method using the name, look userRepository instance name in the vessel, and the parameter to automated assembly.

 

  If there userJdbcImps instance want to load, in addition to a field name into userJdbcImps userRepository outside, may be provided with a @Qualifier tag assembly needs to specify the name of the bean, the code written

1 @Autowired
2 @Qualifier("userJdbcImps")
3 private UserRepository userRepository;

Output: UserJdbcImps save

 

Reprinted from: http://www.cnblogs.com/caoyc/p/5626365.html 

@Autowired annotation, it can be labeled class fields, methods and constructors, the automatic assembly work is completed. By using @Autowired to eliminate set, get method. Time before use @Autowired, we configured from the properties of a bean, is so used

< Property name = "attribute name" value = "attribute value" />    

In this way, the configuration more complicated, and more codes. In Spring 2.5 introduces @Autowired comment

 

The following use cases to specify

UserRepository.java

1 package com.proc.bean.repository;
2 
3 public interface UserRepository {
4     
5     void save();
6 }

This defines a UserRepository interface which defines a save method

 

UserRepositoryImps.java

Copy the code
 1 package com.proc.bean.repository;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository("userRepository")
 6 public class UserRepositoryImps implements UserRepository{
 7 
 8     @Override
 9     public void save() {
10         System.out.println("UserRepositoryImps save");
11     }
12 }
Copy the code

UserRepository define a class that implements the interface, and implement the save method, where the bean in the specified identifier name userRepository IoC

 

UserService.java

Copy the code
 1 package com.proc.bean.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import com.proc.bean.repository.UserRepository;
 7 
 8 @Service
 9 public class UserService {
10 
11     @Autowired
12     private UserRepository userRepository;
13     
14     public void save(){
15         userRepository.save();
16     }
17 }
Copy the code

It should UserRepository a type attribute, by way @Autowired automatic assembly, to find from the IoC container, and returns to the attribute

 

applicationContext.xml Configuration

<context:component-scan base-package="com.proc.bean" />

 

Test code:

1 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
2 
3 UserService userService=(UserService) ctx.getBean("userService");
4 userService.save();

Output: UserRepositoryImps save

 

So what use @Autowired the principle?

  In fact, at start Spring IoC, AutowiredAnnotationBeanPostProcessor container automatically loads a post processor, when the container is scanned @ Autowied, @ Resource or @Inject, it will automatically find the bean needs IoC container, and fitted to the object's properties

 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  

  

 Precautions:

  In use @Autowired, corresponding to the first type of query bean in the container

    If the query results are exactly as one would assemble the bean specified data to @Autowired

    If the result of the query is more than one, it will be to find @Autowired by name.

    If the result of the query is empty, then throws an exception. Solving method, required = false

 

for example:

 In the above example, we set a class to implement an interface UserRepository

Copy the code
package com.proc.bean.repository;

import org.springframework.stereotype.Repository;

@Repository
public class UserJdbcImps implements UserRepository {

    @Override
    public void save() {
        System.out.println("UserJdbcImps save");
    }
}
Copy the code

  After this time the starting container, there are two examples of the type of UserRepository in the container, a name userRepository, the other is userJdbcImps. In the UserService

@Autowired
private UserRepository userRepository;

  Output: UserRepositoryImps save

  Here are two examples due to the query type, the matching method using the name, look userRepository instance name in the vessel, and the parameter to automated assembly.

 

  If there userJdbcImps instance want to load, in addition to a field name into userJdbcImps userRepository outside, may be provided with a @Qualifier tag assembly needs to specify the name of the bean, the code written

1 @Autowired
2 @Qualifier("userJdbcImps")
3 private UserRepository userRepository;

Output: UserJdbcImps save

 

Reprinted from: http://www.cnblogs.com/caoyc/p/5626365.html 

Guess you like

Origin www.cnblogs.com/wangjin1017/p/12387363.html