开发者

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. In Java, I'd do it this way...

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
...
RejectedExecutionHandler rejectedExecutionHandler = new开发者_如何学Go ThreadPoolExecutor.CallerRunsPolicy();

But when I try to do the equivalent in Spring, it throws a 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?


I think the reason it is not working is because Spring is not able to understand it as a static inner class. Probably this can work:

<beans>
   <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/>
</beans>


Use the factory-method attribute:

The following bean definition specifies that the bean will be created by calling a factory-method. 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.

<bean id="clientService" class="examples.ClientService"
  factory-method="createInstance"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜