getObject of method instance resolution in spring FactoryBean
- 2021-01-19 22:15:53
- OfStack
This paper mainly studies the relevant contents of getObject() method in FactoryBean in spring, as follows.
The FactoryBean interface defines the following three interface methods:
Object getObject(): Returns an instance of Bean that was created by FactoryBean. If isSingleton() returns true, the instance will be placed in the single-instance cache pool of the Spring container. boolean isSingleton(): Determines whether the scope of Bean created by FactoryBean is singleton or prototype. Class getObjectType(): Returns the type in which FactoryBean created Bean.When in the configuration file < bean > FactoryBean#getObject() is the object returned by FactoryBean#getObject(), which is a proxy for getBean(). FactoryBean# getObject() is a proxy for getBean().
When we configure Car, there is one for each of the Car attributes < property > Element tags. Suppose we want to specify configuration values for all properties of Car once in a comma-separated way, rather than in a neat way, we can write a single FactroyBean to achieve this:
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;
}
}
Now that we have CarFactoryBean, we can define Car Bean in the configuration file using the following configuration:
<bean id="car" class="com.lxm.fb.carFactoryBean">
<property name="carInfo" value=" The red flag CA72,200,20000.00"/>
</bean>
When ES56en (" ES57en ") is called, ES58en discovers through reflection that ES59en implements the interface of ES60en, at which point the Sping container calls the interface method CarFactoryBean# ES63en () to return the object created by the factory class. If the user wants to obtain an instance of CarFactoryBean, it is necessary to explicitly prefix beanName with" & "Front: getBean(" & car").
conclusion
This article is about the ES78en () method in ES77en ES76en instance resolution of all content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!