Search in sources :

Example 1 with LazyInitTargetSource

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;
    }
}
Also used : RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LazyInitTargetSource(org.springframework.aop.target.LazyInitTargetSource) ObjectName(javax.management.ObjectName)

Example 2 with LazyInitTargetSource

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();
}
Also used : ProxyFactoryBean(org.springframework.aop.framework.ProxyFactoryBean) LazyInitTargetSource(org.springframework.aop.target.LazyInitTargetSource)

Example 3 with LazyInitTargetSource

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;
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Advised(org.springframework.aop.framework.Advised) BeanFactory(org.springframework.beans.factory.BeanFactory) LazyInitTargetSource(org.springframework.aop.target.LazyInitTargetSource) Test(org.junit.Test)

Aggregations

LazyInitTargetSource (org.springframework.aop.target.LazyInitTargetSource)3 ObjectName (javax.management.ObjectName)1 ModelMBean (javax.management.modelmbean.ModelMBean)1 RequiredModelMBean (javax.management.modelmbean.RequiredModelMBean)1 Test (org.junit.Test)1 Advised (org.springframework.aop.framework.Advised)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 ProxyFactoryBean (org.springframework.aop.framework.ProxyFactoryBean)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 ITestBean (org.springframework.tests.sample.beans.ITestBean)1