use of org.springframework.aop.target.LazyInitTargetSource in project spring-framework by spring-projects.
the class MBeanExporter method registerLazyInit.
/**
* Registers beans that are configured for lazy initialization with the
* {@code MBeanServer} indirectly through a proxy.
* @param beanName the name of the bean in the {@code BeanFactory}
* @param beanKey the key associated with this bean in the beans map
* @return the {@code ObjectName} under which the bean was registered
* with the {@code MBeanServer}
*/
private ObjectName registerLazyInit(String beanName, String beanKey) throws JMException {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setProxyTargetClass(true);
proxyFactory.setFrozen(true);
if (isMBean(this.beanFactory.getType(beanName))) {
// A straight MBean... Let's create a simple lazy-init CGLIB proxy for it.
LazyInitTargetSource targetSource = new LazyInitTargetSource();
targetSource.setTargetBeanName(beanName);
targetSource.setBeanFactory(this.beanFactory);
proxyFactory.setTargetSource(targetSource);
Object proxy = proxyFactory.getProxy(this.beanClassLoader);
ObjectName objectName = getObjectName(proxy, beanKey);
if (logger.isDebugEnabled()) {
logger.debug("Located MBean '" + beanKey + "': registering with JMX server as lazy-init MBean [" + objectName + "]");
}
doRegister(proxy, objectName);
return objectName;
} else {
// A simple bean... Let's create a lazy-init ModelMBean proxy with notification support.
NotificationPublisherAwareLazyTargetSource targetSource = new NotificationPublisherAwareLazyTargetSource();
targetSource.setTargetBeanName(beanName);
targetSource.setBeanFactory(this.beanFactory);
proxyFactory.setTargetSource(targetSource);
Object proxy = proxyFactory.getProxy(this.beanClassLoader);
ObjectName objectName = getObjectName(proxy, beanKey);
if (logger.isDebugEnabled()) {
logger.debug("Located simple bean '" + beanKey + "': registering with JMX server as lazy-init MBean [" + objectName + "]");
}
ModelMBean mbean = createAndConfigureMBean(proxy, beanKey);
targetSource.setModelMBean(mbean);
targetSource.setObjectName(objectName);
doRegister(mbean, objectName);
return objectName;
}
}
use of org.springframework.aop.target.LazyInitTargetSource in project spring-security by spring-projects.
the class AuthenticationConfiguration method lazyBean.
@SuppressWarnings("unchecked")
private <T> T lazyBean(Class<T> interfaceName) {
LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource();
String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, interfaceName);
if (beanNamesForType.length == 0) {
return null;
}
Assert.isTrue(beanNamesForType.length == 1, "Expecting to only find a single bean for type " + interfaceName + ", but found " + Arrays.asList(beanNamesForType));
lazyTargetSource.setTargetBeanName(beanNamesForType[0]);
lazyTargetSource.setBeanFactory(applicationContext);
ProxyFactoryBean proxyFactory = new ProxyFactoryBean();
proxyFactory = objectPostProcessor.postProcess(proxyFactory);
proxyFactory.setTargetSource(lazyTargetSource);
return (T) proxyFactory.getObject();
}
use of org.springframework.aop.target.LazyInitTargetSource in project spring-framework by spring-projects.
the class SelectivePrototypeTargetSourceCreator method testLazyInitTargetSource.
@Test
public void testLazyInitTargetSource() throws Exception {
CountingTestBean.count = 0;
BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS);
ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
assertTrue(AopUtils.isAopProxy(test));
Advised advised = (Advised) test;
assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
assertEquals("Rod", test.getName());
assertEquals("Kerry", test.getSpouse().getName());
assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
CountingTestBean.count = 0;
}
Aggregations