[英]How to create a Spring bean from a static inner class constructor?
I am trying to use the Spring Framework IoC Container to create an instance of class ThreadPoolExecutor.CallerRunsPolicy . 我正在尝试使用Spring Framework IoC Container来创建类ThreadPoolExecutor.CallerRunsPolicy的实例。 In Java, I'd do it this way... 在Java中,我会这样做...
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
...
RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
But when I try to do the equivalent in Spring, it throws a CannotLoadBeanClassException . 但是当我尝试在Spring中执行等效操作时,它会抛出一个CannotLoadBeanClassException 。
<beans>
<bean class="java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy"/>
</beans>
More generally: in a Spring ApplicationContext XML, how can you call a constructor of a static inner class? 更一般地说:在Spring ApplicationContext XML中,如何调用静态内部类的构造函数?
I think the reason it is not working is because Spring is not able to understand it as a static inner class. 我认为它不起作用的原因是因为Spring无法将其理解为静态内部类。 Probably this can work: 可能这可以工作:
<beans>
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
</beans>
Use the factory-method
attribute : 使用factory-method
属性 :
The following bean definition specifies that the bean will be created by calling a factory-method. 以下bean定义指定通过调用factory-method创建bean。 The definition does not specify the type (class) of the returned object, only the class containing the factory method. 该定义未指定返回对象的类型(类),仅指定包含工厂方法的类。 In this example, the createInstance() method must be a static method. 在此示例中,createInstance()方法必须是静态方法。
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.