1. Spring generates beans in three ways



1.1. No-parameter construction method

The no-argument constructor is invoked by default to instantiate the bean.

Write UserDao interface and implement class UserDaoImpl:



File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xkip \ Spring Three ways to generate beans \Img\wps4600.tmp.jpg? LastModify =1523327470

Add the no-argument constructor to UserDaoImpl:

















































Configure UserDaoImpl in the Spring container:


Create unit test class TestDI and write unit test method test1:

File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xkip \ Spring Three ways to generate beans \Img\wps4601.tmp.jpg? LastModify =1523327470




























The test results are as follows:



File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Kapi \ Spring Three ways to generate beans \Img\wps4602.tmp.jpg? LastModify =1523327470

1.2. Static factory instantiation


Generate beans by calling static methods of the factory class

Write the DeptDao interface

File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xkip \ Spring Three ways to generate beans \Img\wps4603.tmp.jpg? LastModify =1523327470





















Write the DeptDaoImpl implementation class



File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xkip \ Spring Three ways to generate beans \Img\wps4604.tmp.jpg? LastModify =1523327470




















Write a factory class in which to create static factory methods

File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xkip \ Spring Three ways to generate beans \Img\wps4605.tmp.jpg? LastModify =1523327470





































































Write the ApplicationContext. XML configuration file to configure the DeptDaoImpl class in a static factory fashion



























www.springframework.org/schema/bean…

















When the DeptDaoImpl bean is configured, the class property does not write the full pathname of DeptDaoImpl but the full pathname of the factory class.

Factory-method: Specifies the name of the static method in the factory class

Write the test method test4 in the TestIOC class




















The test results are as follows:





File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xocalb \ Spring Three ways to generate beans \Img\wps4606.tmp.jpg? LastModify =1523327470

1.3. Instance factory instantiation mode

Modify the Factory Factory class to create an instance Factory method:





































































































Write applicationContext. XML and override the configuration of DeptDaoImpl as an instance factory



























www.springframework.org/schema/bean…









































Factory-bean: Specifies the id of the factory bean;

Factory-method: Specifies the name of the instance Factory method of the Factory bean

Run the test4 test method and the result is as follows:





File ://C:\Users\ student\ \ desktop\ Shanghai Java3 Monthsquality article \ Xocalb \ Spring three ways to generate beans \Img\wps4617.tmp.jpg? LastModify =1523327470

For more technical information: Gzitcast