Search code examples
javaspringdependency-injectioninversion-of-controlioc-container

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


Solution

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