views:

94

answers:

2

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 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?

+1  A: 

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"/>
matt b
I did attempt a few variations of "factory-method" but that does not work here because ThreadPoolExecutor.CallerRunsPolicy is a regular old Java constructor. This is a static class.
Drew
The fact that CRP is a static nested class shouldn't matter here. While matt b's example for a static method is correct, you're not actually needing to invoke a static method here... just a plain ol' constructor. That being said, I'd be curious to see the rest of the details of the CannotLoadBeanClassException.
RonU
+5  A: 

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>
Ankit
re-reading the original question and code sample I realize that drew isn't attempting to invoke a static method at all; just the constructor of a nested class. The `$` syntax is correct to use here, I've used this myself before.
matt b
That did work, thanks Ankit. The Spring docs are here: http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-class and it says "to configure a bean definition for a static inner class, you have to use the binary name of the inner class."
Drew