spring.version=5.2.7.RELEASE
public class Service15 {
public Service15(){
System.out.println("service15 init");
}
}
public class Service15Factory implements FactoryBean, BeanFactoryAware {
private BeanFactory beanFactory;
public Service15Factory(){
System.out.println("serviceFactory15 init");
}
@Override
public Object getObject() throws Exception {
beanFactory.getBean("service16");
return new Service15();
}
@Override
public Class<?> getObjectType() {
return Service15.class;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}
public class Service16 {
private Service15 service15;
public Service16(){
System.out.println("service16 init");
}
public void setService15(Service15 service15){
this.service15 = service15;
}
}
configuration:
<bean id="service15" class="com.myspring.service.Service15Factory" scope="singleton">
</bean>
<bean id="service16" class="com.myspring.service.Service16">
<property name="service15" ref="service15"></property>
</bean>
I created three classes,service15 will be created by getObject of Service15Factory
, and Service16
dependent on service15,service15 will be Initialized twice.Is this a bug?
Comment From: bclozel
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
In this case, your implementation created a bean cycle (service16 -> service15 -> the service15 FactoryBean looks up service16 -> which requires service15).
Comment From: chenzhongyu11
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
In this case, your implementation created a bean cycle (service16 -> service15 -> the service15 FactoryBean looks up service16 -> which requires service15).
This is my test method and result.I think service15 is configured as a singleton,will be printed only once,but it's printed twice on my console.
public static void main(String[] args) {
Resource resource = new ClassPathResource("my-spring.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Service15 service15 = (Service15)beanFactory.getBean("service15");
}
serviceFactory15 init
service16 init
service15 init
service15 init