Java Code Examples for org.springframework.beans.factory.support.GenericBeanDefinition#setScope()

The following examples show how to use org.springframework.beans.factory.support.GenericBeanDefinition#setScope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmbeddedCassandraContextCustomizer.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
private static void registerCassandraConnectionBeanDefinition(ConfigurableApplicationContext context,
		BeanDefinitionRegistry registry) {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(CassandraConnection.class);
	bd.setDestroyMethodName("close");
	bd.setLazyInit(true);
	bd.setDependsOn(Cassandra.class.getName());
	bd.setScope(BeanDefinition.SCOPE_SINGLETON);
	bd.setInstanceSupplier(new CassandraConnectionSupplier(context));
	registry.registerBeanDefinition(CassandraConnection.class.getName(), bd);
}
 
Example 2
Source File: DisconfMgrBean.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * register aspectJ for disconf get request
 *
 * @param registry
 */
private void registerAspect(BeanDefinitionRegistry registry) {

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(DisconfAspectJ.class);
    beanDefinition.setLazyInit(false);
    beanDefinition.setAbstract(false);
    beanDefinition.setAutowireCandidate(true);
    beanDefinition.setScope("singleton");

    registry.registerBeanDefinition("disconfAspectJ", beanDefinition);
}
 
Example 3
Source File: StaticApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 4
Source File: PortletApplicationContextScopeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ConfigurablePortletApplicationContext initApplicationContext(String scope) {
	MockServletContext sc = new MockServletContext();
	GenericWebApplicationContext rac = new GenericWebApplicationContext(sc);
	rac.refresh();
	PortletContext pc = new ServletWrappingPortletContext(sc);
	StaticPortletApplicationContext ac = new StaticPortletApplicationContext();
	ac.setParent(rac);
	ac.setPortletContext(pc);
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(DerivedTestBean.class);
	bd.setScope(scope);
	ac.registerBeanDefinition(NAME, bd);
	ac.refresh();
	return ac;
}
 
Example 5
Source File: AbstractBeanFactoryBasedTargetSourceCreator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final TargetSource getTargetSource(Class<?> beanClass, String beanName) {
	AbstractBeanFactoryBasedTargetSource targetSource =
			createBeanFactoryBasedTargetSource(beanClass, beanName);
	if (targetSource == null) {
		return null;
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Configuring AbstractBeanFactoryBasedTargetSource: " + targetSource);
	}

	DefaultListableBeanFactory internalBeanFactory = getInternalBeanFactoryForBean(beanName);

	// We need to override just this bean definition, as it may reference other beans
	// and we're happy to take the parent's definition for those.
	// Always use prototype scope if demanded.
	BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
	GenericBeanDefinition bdCopy = new GenericBeanDefinition(bd);
	if (isPrototypeBased()) {
		bdCopy.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	}
	internalBeanFactory.registerBeanDefinition(beanName, bdCopy);

	// Complete configuring the PrototypeTargetSource.
	targetSource.setTargetBeanName(beanName);
	targetSource.setBeanFactory(internalBeanFactory);

	return targetSource;
}
 
Example 6
Source File: StaticApplicationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 7
Source File: StaticApplicationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 8
Source File: ColaMockController.java    From COLA with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    ColaMockController.beanFactory = beanFactory;
    ColaMockito.g().setBeanFactory(beanFactory);
    String[] beanDefinitionNames = registry.getBeanDefinitionNames();
    Map<String, String> index = Arrays.stream(beanDefinitionNames).collect(Collectors.toMap(key->key, v->v));
    for(String name : serviceList){
        String[] meta = name.split(Constants.SERVICE_LIST_DELIMITER);
        String beanName = meta[0];
        String beanClass = meta[1];
        if(index.get(meta[0]) == null){
            try {
                Class type = Thread.currentThread().getContextClassLoader().loadClass(beanClass);
                //if("fileserverClient".equals(beanName)){
                //    System.out.println("============");
                //}
                if(!mockFilterManager.match(type)){
                    continue;
                }
                GenericBeanDefinition definition = new GenericBeanDefinition();
                definition.getConstructorArgumentValues().addGenericArgumentValue(beanName);
                definition.getConstructorArgumentValues().addGenericArgumentValue(type);
                definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
                definition.setBeanClass(AutoMockFactoryBean.class);
                definition.setScope("singleton");
                definition.setLazyInit(true);
                definition.setAutowireCandidate(true);
                registry.registerBeanDefinition(beanName, definition);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example 9
Source File: EmbeddedCassandraContextCustomizer.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
private static void registerCassandraInitializerBeanDefinition(CqlDataSet dataSet,
		ConfigurableApplicationContext context,
		BeanDefinitionRegistry registry) {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(CassandraInitializer.class);
	bd.setLazyInit(false);
	bd.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	bd.setScope(BeanDefinition.SCOPE_SINGLETON);
	bd.setInstanceSupplier(() -> new CassandraInitializer(context, dataSet));
	registry.registerBeanDefinition(CassandraInitializer.class.getName(), bd);
}
 
Example 10
Source File: DisconfMgrBean.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * register aspectJ for disconf get request
 *
 * @param registry
 */
private void registerAspect(BeanDefinitionRegistry registry) {

    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(DisconfAspectJ.class);
    beanDefinition.setLazyInit(false);
    beanDefinition.setAbstract(false);
    beanDefinition.setAutowireCandidate(true);
    beanDefinition.setScope("singleton");

    registry.registerBeanDefinition("disconfAspectJ", beanDefinition);
}
 
Example 11
Source File: EmbeddedCassandraContextCustomizer.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
private static void registerCassandraBeanDefinition(boolean exposeProperties,
		ConfigurableApplicationContext context,
		BeanDefinitionRegistry registry) {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(Cassandra.class);
	bd.setDestroyMethodName("stop");
	bd.setLazyInit(false);
	bd.setScope(BeanDefinition.SCOPE_SINGLETON);
	bd.setInstanceSupplier(new CassandraSupplier(exposeProperties, context));
	registry.registerBeanDefinition(Cassandra.class.getName(), bd);
}
 
Example 12
Source File: WebApplicationContextScopeTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebApplicationContext initApplicationContext(String scope) {
	MockServletContext sc = new MockServletContext();
	GenericWebApplicationContext ac = new GenericWebApplicationContext(sc);
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(DerivedTestBean.class);
	bd.setScope(scope);
	ac.registerBeanDefinition(NAME, bd);
	ac.refresh();
	return ac;
}
 
Example 13
Source File: StaticApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 14
Source File: StaticApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 15
Source File: WebApplicationContextScopeTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private WebApplicationContext initApplicationContext(String scope) {
	MockServletContext sc = new MockServletContext();
	GenericWebApplicationContext ac = new GenericWebApplicationContext(sc);
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(DerivedTestBean.class);
	bd.setScope(scope);
	ac.registerBeanDefinition(NAME, bd);
	ac.refresh();
	return ac;
}
 
Example 16
Source File: StaticApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 17
Source File: StaticApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a prototype bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerPrototype(String name, Class<?> clazz) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 18
Source File: SimpleTransactionScopeTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void getWithTransactionManager() throws Exception {
	try (GenericApplicationContext context = new GenericApplicationContext()) {
		context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());

		GenericBeanDefinition bd1 = new GenericBeanDefinition();
		bd1.setBeanClass(TestBean.class);
		bd1.setScope("tx");
		bd1.setPrimary(true);
		context.registerBeanDefinition("txScopedObject1", bd1);

		GenericBeanDefinition bd2 = new GenericBeanDefinition();
		bd2.setBeanClass(DerivedTestBean.class);
		bd2.setScope("tx");
		context.registerBeanDefinition("txScopedObject2", bd2);

		context.refresh();

		CallCountingTransactionManager tm = new CallCountingTransactionManager();
		TransactionTemplate tt = new TransactionTemplate(tm);
		Set<DerivedTestBean> finallyDestroy = new HashSet<>();

		tt.execute(status -> {
			TestBean bean1 = context.getBean(TestBean.class);
			assertSame(bean1, context.getBean(TestBean.class));

			DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
			assertSame(bean2, context.getBean(DerivedTestBean.class));
			context.getBeanFactory().destroyScopedBean("txScopedObject2");
			assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
			assertTrue(bean2.wasDestroyed());

			DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
			assertSame(bean2a, context.getBean(DerivedTestBean.class));
			assertNotSame(bean2, bean2a);
			context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
			assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
			assertFalse(bean2a.wasDestroyed());

			DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
			finallyDestroy.add(bean2b);
			assertSame(bean2b, context.getBean(DerivedTestBean.class));
			assertNotSame(bean2, bean2b);
			assertNotSame(bean2a, bean2b);

			Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
			TransactionTemplate tt2 = new TransactionTemplate(tm);
			tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
			tt2.execute(status2 -> {
				DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
				immediatelyDestroy.add(bean2c);
				assertSame(bean2c, context.getBean(DerivedTestBean.class));
				assertNotSame(bean2, bean2c);
				assertNotSame(bean2a, bean2c);
				assertNotSame(bean2b, bean2c);
				return null;
			});
			assertTrue(immediatelyDestroy.iterator().next().wasDestroyed());
			assertFalse(bean2b.wasDestroyed());

			return null;
		});

		assertTrue(finallyDestroy.iterator().next().wasDestroyed());
	}
}
 
Example 19
Source File: SimpleTransactionScopeTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void getWithTransactionManager() throws Exception {
	try (GenericApplicationContext context = new GenericApplicationContext()) {
		context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());

		GenericBeanDefinition bd1 = new GenericBeanDefinition();
		bd1.setBeanClass(TestBean.class);
		bd1.setScope("tx");
		bd1.setPrimary(true);
		context.registerBeanDefinition("txScopedObject1", bd1);

		GenericBeanDefinition bd2 = new GenericBeanDefinition();
		bd2.setBeanClass(DerivedTestBean.class);
		bd2.setScope("tx");
		context.registerBeanDefinition("txScopedObject2", bd2);

		context.refresh();

		CallCountingTransactionManager tm = new CallCountingTransactionManager();
		TransactionTemplate tt = new TransactionTemplate(tm);
		Set<DerivedTestBean> finallyDestroy = new HashSet<>();

		tt.execute(status -> {
			TestBean bean1 = context.getBean(TestBean.class);
			assertSame(bean1, context.getBean(TestBean.class));

			DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
			assertSame(bean2, context.getBean(DerivedTestBean.class));
			context.getBeanFactory().destroyScopedBean("txScopedObject2");
			assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
			assertTrue(bean2.wasDestroyed());

			DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
			assertSame(bean2a, context.getBean(DerivedTestBean.class));
			assertNotSame(bean2, bean2a);
			context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
			assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
			assertFalse(bean2a.wasDestroyed());

			DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
			finallyDestroy.add(bean2b);
			assertSame(bean2b, context.getBean(DerivedTestBean.class));
			assertNotSame(bean2, bean2b);
			assertNotSame(bean2a, bean2b);

			Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
			TransactionTemplate tt2 = new TransactionTemplate(tm);
			tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
			tt2.execute(status2 -> {
				DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
				immediatelyDestroy.add(bean2c);
				assertSame(bean2c, context.getBean(DerivedTestBean.class));
				assertNotSame(bean2, bean2c);
				assertNotSame(bean2a, bean2c);
				assertNotSame(bean2b, bean2c);
				return null;
			});
			assertTrue(immediatelyDestroy.iterator().next().wasDestroyed());
			assertFalse(bean2b.wasDestroyed());

			return null;
		});

		assertTrue(finallyDestroy.iterator().next().wasDestroyed());
	}
}
 
Example 20
Source File: SpringBeanLoader.java    From cuba with Apache License 2.0 4 votes vote down vote up
public void updateContext(Collection<Class> classes) {
    if (beanFactory != null) {
        boolean needToRefreshRemotingContext = false;
        for (Class clazz : classes) {
            Service serviceAnnotation = (Service) clazz.getAnnotation(Service.class);
            Component componentAnnotation = (Component) clazz.getAnnotation(Component.class);
            Controller controllerAnnotation = (Controller) clazz.getAnnotation(Controller.class);

            String beanName = null;
            if (serviceAnnotation != null) {
                beanName = serviceAnnotation.value();
            } else if (componentAnnotation != null) {
                beanName = componentAnnotation.value();
            } else if (controllerAnnotation != null) {
                beanName = controllerAnnotation.value();
            }

            if (StringUtils.isNotBlank(beanName)) {
                GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
                beanDefinition.setBeanClass(clazz);
                Scope scope = (Scope) clazz.getAnnotation(Scope.class);
                if (scope != null) {
                    beanDefinition.setScope(scope.value());
                }

                beanFactory.registerBeanDefinition(beanName, beanDefinition);
            }

            if (StringUtils.isNotBlank(beanName)) {
                needToRefreshRemotingContext = true;
            }
        }

        if (needToRefreshRemotingContext) {
            ApplicationContext remotingContext = RemotingContextHolder.getRemotingApplicationContext();
            if (remotingContext != null && remotingContext instanceof ConfigurableApplicationContext) {
                ((ConfigurableApplicationContext) remotingContext).refresh();
            }
        }
    }
}