How to create spring bean from static constructor of inner class?

I am trying to use the Spring Framework IoC Container to instantiate the ThreadPoolExecutor.CallerRunsPolicy class. In Java, I would do it like this ...

import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; ... RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); 

But when I try to make an equivalent in Spring, it throws a CannotLoadBeanClassException.

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

In summary: in Spring ApplicationContext XML, how can you call the constructor of a static inner class?

+7
java spring dependency-injection inversion-of-control ioc-container
source share
2 answers

I think the reason is that it does not work, because Spring cannot understand it as a static inner class. This could probably work:

 <beans> <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy"/> </beans> 
+13
source share

Use the factory-method attribute :

The following bean definition states that a bean will be created by calling the factory method. The definition does not indicate the type (class) of the returned object, but only the class containing the factory method. In this example, the createInstance () method must be static.

 <bean id="clientService" class="examples.ClientService" factory-method="createInstance"/> 
+1
source share

All Articles