I'm using Spring MVC 3.2.4 and Apache Commons Pooling 2.3 for managing connections to a SOAP service. One of the things I am using is the "idle object eviction" thread feature in the GenericObjectPool class:
With this feature I specify the name of the class to use to test for eviction of our connection objects:
Upon inspection of the Commons Pooling implementation of the GenericObjectPool class, the eviction class is instantiated using reflection and executed. All attempts to inject Spring beans into this class have failed including using common techniques such as load-time weaving using @EnableLoadTimeWeaving and @Configurable on the generated class.
Is it possible to inject Spring beans into a class that is generated internally by reflection and is not managed by the Spring container?
EDIT:
Here is the method that instantiates the evictor class:
...
public final void setEvictionPolicyClassName(
String evictionPolicyClassName) {
try {
Class<?> clazz = Class.forName(evictionPolicyClassName);
Object policy = clazz.newInstance();
if (policy instanceof EvictionPolicy<?>) {
@SuppressWarnings("unchecked") // safe, because we just checked the class
EvictionPolicy<T> evicPolicy = (EvictionPolicy<T>) policy;
this.evictionPolicy = evicPolicy;
}
} catch (ClassNotFoundException e) {
...
Here is the run method executed within the thread on a set interval:
...
@Override
public void run() {
ClassLoader savedClassLoader =
Thread.currentThread().getContextClassLoader();
try {
// Set the class loader for the factory
Thread.currentThread().setContextClassLoader(
factoryClassLoader);
// Evict from the pool
try {
evict();
} catch(Exception e) {
...
Here is a sample of the implementation of the EvictionPolicy class:
class SampleEvictionPolicy implements EvictionPolicy<SabreConnection> {
// This is what I would like to add:
// @Autowired
// private desiredBeans desiredBeans
public SampleEvictionPolicy() { }
@Override
boolean evict(EvictionConfig evictionConfig, PooledObject<SabreConnection> tPooledObject, int i) {
// Do some stuff
}