Action beans should/must be "prototype" scope ?
Brent Putman
putmanb at georgetown.edu
Thu Feb 6 16:11:10 EST 2014
On 2/6/14 2:39 PM, Tom Zeller wrote:
> I have not tried a PostProcessor nor plan to anytime soon, would be
> good to know if that works.
I tried it. It mostly seems to work ok. Rough sketch attached.
Couple of notes:
1) Would need real error handling, some other conditional checking, etc
2) BeanDefinition doesn't give you the actual Class of the bean, only
the String name, so have to use something to load the Class instance
from the name (perhaps not Class.forName(), maybe Thread context
classloader, etc). As they note, that may or may not be the runtime
class of the bean, depending on use of bean definition inheritance. I
suppose also theoretically BeanPostProcessor can muck with that also, as
the returned bean need not be the same class as the input bean.
Probably not relevant to our use case, but probably needs some more
in-depth thought to confirm.
3) Most importantly: You probably already know this, but in Java
annotations aren't "inherited" by design on Class instances. So assuming
we'd want to be able to declare @Scope on superclasses and/or
interfaces, this code would need to implement the annotation
"inheritance" by traversing up the inheritance tree looking for the
@Scope at each level (and each level's implemented interfaces if we
wanted to support on interfaces).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://shibboleth.net/pipermail/dev/attachments/20140206/f7fd88f1/attachment.html
-------------- next part --------------
package brent.spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Scope;
public class ScopeAnnotationBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for (String beanName : beanFactory.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
Class<?> beanClass;
try {
beanClass = Class.forName(beanDefinition.getBeanClassName());
Scope scope = beanClass.getAnnotation(Scope.class);
if (scope != null && scope.value() != null) {
beanDefinition.setScope(scope.value());
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
More information about the dev
mailing list