The main research in this article is the relevant content of the getObject() method in FactoryBean in spring, as follows.
The FactoryBean interface defines the following 3 interface methods:
When the implementation class configured by the class attribute of <bean> in the configuration file is FactoryBean, the getBean method returns not the FactoryBean itself, but the object returned by the FactoryBean#getObject() method, which is equivalent to FactoryBean#getObject() proxying the getBean() method.
When configuring Car, each property of Car corresponds to a <property> element tag. Suppose we think this method is meticulous and concise, and hope to set configuration values for all the Car properties at one time through comma-separated methods, then we can achieve our goal by writing a FactroyBean:
import org.springframework.beans.factory.FactoryBean;public class CarFactoryBean implements FactoryBean{private String carInfo;public Stirng getCarInfo(){return carInfo;}public void setCarInfo(String carInfo){this.carInfo=carInfo;}public Object getObject()throws Exception{Car car=new Car();String[]infos = carInfo.split(",");car.setBrand(infos[0]);car.setMaxSpeed(Integer.aarseint(infos[1]));car.setPrice(double.parsedouble(infos[2]));return car;}public Class getObjectType(){return Car.class;}public Boolean isSingleton() {return true;}}With this CarFactoryBean, we can use the following configuration method to define the Car Bean in the configuration file:
<bean id="car"> <property name="carInfo" value="Red Flag CA72,200,20000.00"/> </bean>
When gettingBean("car"), Spring discovers through the reflection mechanism that CarFactoryBean implements the FactoryBean interface. At this time, the Spring container calls the interface method CarFactoryBean#getObject() to return the object created by the factory class. If the user wants to get an instance of CarFactoryBean, he or she needs to explicitly prefix the beanName with the "&" prefix: getBean("&car") when using the getBean(beanName) method.
The above is all the content of this article about the analysis of the getObject() method example in FactoryBean in spring. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!