Convenient proxy factory bean superclass for proxy factory
beans that create only singletons.
Manages pre- and post-interceptors (references, rather than
interceptor names, as in ProxyFactoryBean ) and provides
consistent interface management.
| Method from org.springframework.aop.framework.AbstractSingletonProxyFactoryBean Detail: |
public void afterPropertiesSet() {
if (this.target == null) {
throw new IllegalArgumentException("Property 'target' is required");
}
if (this.target instanceof String) {
throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
}
if (this.proxyClassLoader == null) {
this.proxyClassLoader = ClassUtils.getDefaultClassLoader();
}
ProxyFactory proxyFactory = new ProxyFactory();
if (this.preInterceptors != null) {
for (int i = 0; i < this.preInterceptors.length; i++) {
proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(this.preInterceptors[i]));
}
}
// Add the main interceptor (typically an Advisor).
proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));
if (this.postInterceptors != null) {
for (int i = 0; i < this.postInterceptors.length; i++) {
proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(this.postInterceptors[i]));
}
}
proxyFactory.copyFrom(this);
TargetSource targetSource = createTargetSource(this.target);
proxyFactory.setTargetSource(targetSource);
if (this.proxyInterfaces != null) {
proxyFactory.setInterfaces(this.proxyInterfaces);
}
else if (!isProxyTargetClass()) {
// Rely on AOP infrastructure to tell us what interfaces to proxy.
proxyFactory.setInterfaces(
ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), this.proxyClassLoader));
}
this.proxy = getProxy(proxyFactory);
}
|
abstract protected Object createMainInterceptor()
Create the "main" interceptor for this proxy factory bean.
Typically an Advisor, but can also be any type of Advice.
Pre-interceptors will be applied before, post-interceptors
will be applied after this interceptor. |
protected TargetSource createTargetSource(Object target) {
if (target instanceof TargetSource) {
return (TargetSource) target;
}
else {
return new SingletonTargetSource(target);
}
}
Determine a TargetSource for the given target (or TargetSource). |
public Object getObject() {
if (this.proxy == null) {
throw new FactoryBeanNotInitializedException();
}
return this.proxy;
}
|
public Class getObjectType() {
if (this.proxy != null) {
return this.proxy.getClass();
}
if (this.proxyInterfaces != null && this.proxyInterfaces.length == 1) {
return this.proxyInterfaces[0];
}
if (this.target instanceof TargetSource) {
return ((TargetSource) this.target).getTargetClass();
}
if (this.target != null) {
return this.target.getClass();
}
return null;
}
|
protected Object getProxy(AopProxy aopProxy) {
return aopProxy.getProxy(this.proxyClassLoader);
}
Return the proxy object to expose.
The default implementation uses a getProxy call with
the factory's bean class loader. Can be overridden to specify a
custom class loader. |
public final boolean isSingleton() {
return true;
}
|
public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) {
this.advisorAdapterRegistry = advisorAdapterRegistry;
}
Specify the AdvisorAdapterRegistry to use.
Default is the global AdvisorAdapterRegistry. |
public void setBeanClassLoader(ClassLoader classLoader) {
if (this.proxyClassLoader == null) {
this.proxyClassLoader = classLoader;
}
}
|
public void setPostInterceptors(Object[] postInterceptors) {
this.postInterceptors = postInterceptors;
}
Set additional interceptors (or advisors) to be applied after the
implicit transaction interceptor, e.g. HibernateInterceptors for
eagerly binding Sessions to the current thread when using JTA.
Note that this is just necessary if you rely on those interceptors in general:
HibernateTemplate and JdoTemplate work nicely with JtaTransactionManager through
implicit on-demand thread binding. |
public void setPreInterceptors(Object[] preInterceptors) {
this.preInterceptors = preInterceptors;
}
Set additional interceptors (or advisors) to be applied before the
implicit transaction interceptor, e.g. PerformanceMonitorInterceptor. |
public void setProxyClassLoader(ClassLoader classLoader) {
this.proxyClassLoader = classLoader;
}
Set the ClassLoader to generate the proxy class in.
Default is the bean ClassLoader, i.e. the ClassLoader used by the
containing BeanFactory for loading all bean classes. This can be
overridden here for specific proxies. |
public void setProxyInterfaces(Class[] proxyInterfaces) {
this.proxyInterfaces = proxyInterfaces;
}
Specify the set of interfaces being proxied.
If not specified (the default), the AOP infrastructure works
out which interfaces need proxying by analyzing the target,
proxying all the interfaces that the target object implements. |
public void setTarget(Object target) {
this.target = target;
}
Set the target object, that is, the bean to be wrapped with a transactional proxy.
The target may be any object, in which case a SingletonTargetSource will
be created. If it is a TargetSource, no wrapper TargetSource is created:
This enables the use of a pooling or prototype TargetSource etc. |