Spring_4 _ annotation method dependency Injection

Source: Internet
Author: User

Spring_4 _ annotation method dependency Injection

The configuration of Web. xml, the PersonDao class, the PersonService class, And the PersonDaoBean class are the same as those in 1. Dependencies need to be added for dependency assembly using annotation Methods

Common-annotations.jar pack.

1) configuration of the beans. xml file:

 

 
  
   
    
      
     
    
   
  
 

 

2)Code of the PersonServiceBean class:

 

Public class PersonServiceBean implements PersonService {// ===========================@ Resource annotation injection method ======== ========/// @ Resource annotation injection method execution principle: (name-based assembly) // 1) @ Resource is assembled by name by default during execution, and bean is added to the field to perform dependency assembly in the xml file, // when added to the setter method, the bean name is used by default to perform dependency assembly in the xml file. // if the property name does not exist, it is assembled Based on the Type dependency. If the final assembly fails, an error is returned; // 2) @ Resource (name = beanName): Only dependency assembly is performed in the xml file by name. If no dependency is found, an error is returned. // The first annotation injection method: add @ Resource @ Resourceprivate PersonDao personDao before the field; // The second annotation injection method: Add @ Resource before the field (name = name of the bean to be injected into the xml file) // @ Resource (name = personDaoxxx) // private PersonDao personDao; // The third annotation injection method: add @ Resource // public void setPersonDao (PersonDao personDao) {// this. personDao = personDao; // The fourth annotation injection method: Add @ Resource before the setter method of the attribute (name = name of the bean to be injected into the xml file) // @ Resource (name = personDaoxxx) // public void setPersonDao (PersonDao personDao) {// this. personDao = personDao; ///} // =======================@ Autowired annotation injection method ==================== ======/// @ Autowired annotation injection principle: (assemble by type, if multiple beans of the same type exist at the same time, an error is returned.) // 1) @ Autowired is added to the field or the setter method, automatically assemble based on the type of the field and the type dependency of the attribute. // If the dependency Assembly fails, an error is returned. // 2) @ Autowired @ Qulifier (value = Name of the bean to be injected into the xml file) // during execution, only dependent assembly is performed in the xml file by name. If not, an error is returned; // The first annotation injection method // @ Autowired // private PersonDao personDao; // The second annotation injection method // @ Autowired @ Qualifier (value = personDaoxxx) // private PersonDao personDao; // The third annotation injection method // @ Autowired // public void setPersonDao (PersonDao personDao) {// this. personDao = personDao; //} public void save () {personDao. add ();}}

 

3)SpringTest class code:

 

public class springTest {@Testpublic void instanceSpring() {AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(springXml/beans.xml);PersonService personService = (PersonService) ctx.getBean(personService);personService.save();}}


 





Related Article

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.