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();}}