Three ways to instantiate beans in Spring: 1) using class constructors <bean id= "OrderService" class= "Cn.itcast.OrderService"/> 2) Using static factory methods for instantiation <bean id= "Personservice" class= "Cn.itacast.OrderFactory" factory-method= "Createorder"/> public class orderfactory{ public static Orderservicebean Createorder () { &N Bsp return new Orderservicebean (); } } 3) Using instance factory method instantiation & Nbsp;<bean id= "Personservicefactory" class= "cn.itcast.service.OrderFactory"/> <bean id= " Personservice "factory-bean=" Personservicefactory "factory-method=" Createorder "/> public class orderfactory{ public static Orderservicebean Createorder () { & nbsp return new Orderservicebean (); } } beans scope: .singleton In each spring IOC container, a bean defines only one instance of an object. By default, the Bean is initialized when the container is started. ButIs that we can specify the bean node's lazy-init= "true" to defer initialization of the bean, which is only initialized when the bean is used for the first time. such as: <bean id= "xxx" class= "Class full class name" lazy-init= "true"/> if you want to apply lazy initialization to all beans, You can set defult-lazy-init= "true" at the root node beans, as follows: <beans default-lazy-init= "true" ...> . prototype Every bean that gets from the container is a new object. .request .session Global session Injection dependent objects &NBSP;1) Basic type Object injection <bean id= " OrderService "class=" Orderservicebean "> <constructor-arg index=" 0 "type=" Java.lang.String "value=" xxx "/> //constructor injection <property name=" name "value=" Zhao "/> //Zodiac Setter Method injection </bean> 2) Inject other bean methods 1: <bean id= "Orderdao" class= "service. Orderdaobean "/><bean id=" OrderService "class=" service. Orderservicebean "> <property name=" Orderdao "ref=" Orderdao "/></bean> Method 2: Use the innerDepartment Bean (internal Bean cannot be used by other beans) <bean id= "OrderService" class= "service. Orderservicebean "> <property name=" Orderdao "> <bean class=" Service. Orderdaobean "/> </property></bean> assemble assembly type: public class Orderservicebean { Private set<string> sets=new hashset<string> ();p rivate list<string> lists=new ArrayList<String > ();p rivate Properties Properties=new properties ();p rivate map<string,string> maps=new hashmap<string, String> (); //setter and Getter methods omitted ...} The configuration file for the collection is as follows: <?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/schema/ Beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://www.springframework.org/ Schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id=" order " class= "Cn.itcast.service.OrderServiceBean" > <property Name= "Lists" ><list><value>hujingwei</value><value>wanglei</value></list ></property> <property name= "Sets" ><set><value>set</value></set> </property> <property name= "Maps" ><map><entry key= Hujingwei "value="/><entry key= "Wanglei" value= "/></map></property> <property name=" Properties "><props> <prop key= ">sss</prop></props></property> </bean></beans> " Automatic assembly in Java code, the @autowired or @resource annotation is used to assemble, the difference between the two annotations is: @Autowired by default is assembled by type, @Resource by default is assembled by name, when the name match is not found is only assembled by type. @Autowired annotations are assembled by assembly type, by default the dependent object must exist, and if is allowed to be a null value, you can set its Required property to false. If we want to assemble by name, we can use it together with @qualifier annotations . The following: @Autowired @Qualifier ("Personorderbean") private Persondao Persondao;
Spring Some summary