In this section we introduce the relevant common configuration of the spring framework
- Spring relies on injection in two ways (construct method injection and setter mode injection)
- P-namespace Mode configuration
- Properties Property File Configuration method
- How collection objects are configured
- Bean Scopes scope (singleton scope and native scope)
1. Spring Dependency Injection method
- Constructs method injection, which is equivalent to invoking a constructor method to inject a dependency between its objects into the object when spring initializes the object
- First define the dependent objects in the class.
- Then define the constructor method by setting the object's dependencies in the constructor's parameters
- Finally, use the <constructor-arg> tag in the spring configuration file to get the dependency injection of the object.
package com.gxa.spring.day02; public class Petserviceimpl { private petdaoimpl Petdao ; // dependent object public Petserviceimpl (Petdaoimpl Petdao) {// Di of the construction method this . Petdao = Petdao; public void Selectpet () {Petdao.selectpet (); }}
Package com.gxa.spring.day02; Public class Petdaoimpl { publicvoid Selectpet () { /** * Complete Pet data Query * /System.out.println ("= = Pet Data Query = =");} }
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <BeanID= "Petservice"class= "Com.gxa.spring.day02.PetServiceImpl"> <Constructor-argname= "Petdao"ref= "Petdao"></Constructor-arg> </Bean> <BeanID= "Petdao"class= "Com.gxa.spring.day02.PetDaoImpl"></Bean> </Beans>
- Value injection, which completes the object's di by adding a setter method to the dependent object
- Define the dependent objects first.
- Adding setter methods to dependent objects
- Finally, using the <property.../> tag in the config file is ok.
Packagecom.gxa.spring.day01; Public classPetserviceimpl {PrivatePetdaoimpl Petdao;//Dependent Objects PrivateItemdaoimpl Itemdao;//Dependent Objects Public voidSetpetdao (Petdaoimpl Petdao) { This. Petdao =Petdao; } Public voidSetitemdao (Itemdaoimpl Itemdao) { This. Itemdao =Itemdao; } Public voidSelectpet () {Petdao.selectpet (); Itemdao.selectitem (); }}
package com.gxa.spring.day01; public class Petdaoimpl { public void Selectpet () { /** * Complete Pet data query */ System.out.println (" = = Pet Data Query = = "
Package com.gxa.spring.day01; Public class Itemdaoimpl { publicvoid SelectItem () { /** * Complete the PET classification data Query * /System.out.println ("= = Pet Classification data Query = =");} }
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd" > <BeanID= "Person"class= "Com.gxa.spring.day01.Person"></Bean> <BeanID= "Petservice"class= "Com.gxa.spring.day01.PetServiceImpl"> < Propertyname= "Petdao"ref= "Petdao"></ Property> < Propertyname= "Itemdao"ref= "Itemdao"></ Property> </Bean> <BeanID= "Petdao"class= "Com.gxa.spring.day01.PetDaoImpl"></Bean> <BeanID= "Itemdao"class= "Com.gxa.spring.day01.ItemDaoImpl"></Bean></Beans>
[Liu Yang java]_spring Related configuration Introduction _ 5th]