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

The following examples show how to use org.springframework.beans.factory.support.GenericBeanDefinition#setBeanClass() . 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: AnnotationDrivenBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private GenericBeanDefinition createObjectMapperFactoryDefinition(@Nullable Object source) {
	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
	beanDefinition.setSource(source);
	beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	return beanDefinition;
}
 
Example 2
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 3
Source File: ConfigurationPropertiesBindingPostProcessorRegistrar.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private void registerConfigurationPropertiesBindingPostProcessor(
        BeanDefinitionRegistry registry) {
    GenericBeanDefinition definition = new GenericBeanDefinition();
    definition.setBeanClass(ConfigurationPropertiesBindingPostProcessor.class);
    definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    registry.registerBeanDefinition(
            ConfigurationPropertiesBindingPostProcessor.BEAN_NAME, definition);

}
 
Example 4
Source File: DefaultsBeanDefinitionParser.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * @param clazz
 * @return
 */
@SuppressWarnings("rawtypes")
private BeanDefinitionHolder createBeanDefinition(Class clazz, ParserContext parserContext) {
	GenericBeanDefinition gbd = new GenericBeanDefinition();
	gbd.setBeanClass(clazz);
	gbd.setInitMethodName(INIT_METHOD_NAME);
	BeanDefinitionHolder holder = new BeanDefinitionHolder(gbd, 
			parserContext.getReaderContext().generateBeanName(gbd));
	
	return holder;
}
 
Example 5
Source File: StaticApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Register a singleton bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 6
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 7
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 8
Source File: MicronautBeanFactory.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
@Override
public org.springframework.beans.factory.config.BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
    final BeanDefinitionReference<?> reference = beanDefinitionMap.get(beanName);
    if (reference != null) {
        final BeanDefinition<?> def = reference.load(beanContext);
        if (def.isEnabled(beanContext)) {
            final GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
            genericBeanDefinition.setBeanClass(def.getBeanType());
            return genericBeanDefinition;
        }
    }
    throw new NoSuchBeanDefinitionException(beanName);
}
 
Example 9
Source File: RpcInvokerAnnotationScanner.java    From hasting with MIT License 5 votes vote down vote up
@Override
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
	HashSet<BeanDefinitionHolder> beans = new HashSet<BeanDefinitionHolder>();
	Set<BeanDefinitionHolder> set = super.doScan(basePackages);
	for(BeanDefinitionHolder bdh:set){
		GenericBeanDefinition bdf = (GenericBeanDefinition)bdh.getBeanDefinition();
		bdf.getPropertyValues().add("invokerInterface", bdf.getBeanClassName());
		bdf.getPropertyValues().add("rpcClientCache", rpcClients);
		bdf.setBeanClass(RpcInvokerFactoryBean.class);
	}
	return beans;
}
 
Example 10
Source File: CustomizeImportBeanDefinitionRegistrar.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    if (!registry.containsBeanDefinition(BEAN_NAME)) {
        Utils.printTrack("start registerBeanDefinitions");
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(CustomizeServiceImpl4.class);
        beanDefinition.setSynthetic(true);
        registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
    }
}
 
Example 11
Source File: ApplicationContextExpressionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void stringConcatenationWithDebugLogging() {
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();

	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(String.class);
	bd.getConstructorArgumentValues().addGenericArgumentValue("test-#{ T(java.lang.System).currentTimeMillis() }");
	ac.registerBeanDefinition("str", bd);
	ac.refresh();

	String str = ac.getBean("str", String.class);
	assertTrue(str.startsWith("test-"));
}
 
Example 12
Source File: ApplicationContextExpressionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void systemPropertiesSecurityManager() {
	GenericApplicationContext ac = new GenericApplicationContext();
	AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);

	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(TestBean.class);
	bd.getPropertyValues().add("country", "#{systemProperties.country}");
	ac.registerBeanDefinition("tb", bd);

	SecurityManager oldSecurityManager = System.getSecurityManager();
	try {
		System.setProperty("country", "NL");

		SecurityManager securityManager = new SecurityManager() {
			@Override
			public void checkPropertiesAccess() {
				throw new AccessControlException("Not Allowed");
			}
			@Override
			public void checkPermission(Permission perm) {
				// allow everything else
			}
		};
		System.setSecurityManager(securityManager);
		ac.refresh();

		TestBean tb = ac.getBean("tb", TestBean.class);
		assertEquals("NL", tb.getCountry());

	}
	finally {
		System.setSecurityManager(oldSecurityManager);
		System.getProperties().remove("country");
	}
}
 
Example 13
Source File: DefaultsBeanDefinitionParser.java    From jdal with Apache License 2.0 5 votes vote down vote up
/**
 * @param clazz
 * @return
 */
@SuppressWarnings("rawtypes")
private BeanDefinitionHolder createBeanDefinition(Class clazz, ParserContext parserContext) {
	GenericBeanDefinition gbd = new GenericBeanDefinition();
	gbd.setBeanClass(clazz);
	gbd.setInitMethodName(INIT_METHOD_NAME);
	BeanDefinitionHolder holder = new BeanDefinitionHolder(gbd, 
			parserContext.getReaderContext().generateBeanName(gbd));
	
	return holder;
}
 
Example 14
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 15
Source File: MangoDaoScanner.java    From mango with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
  for (Class<?> daoClass : findMangoDaoClasses()) {
    GenericBeanDefinition bf = new GenericBeanDefinition();
    bf.setBeanClassName(daoClass.getName());
    MutablePropertyValues pvs = bf.getPropertyValues();
    pvs.addPropertyValue("daoClass", daoClass);
    bf.setBeanClass(factoryBeanClass);
    bf.setPropertyValues(pvs);
    bf.setLazyInit(false);
    dlbf.registerBeanDefinition(daoClass.getName(), bf);
  }
}
 
Example 16
Source File: StaticApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Register a singleton bean with the underlying bean factory.
 * <p>For more advanced needs, register with the underlying BeanFactory directly.
 * @see #getDefaultListableBeanFactory
 */
public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 17
Source File: GroovyBeanDefinitionReader.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
/**
 * Defines an inner bean definition.
 * @param type the bean type
 * @return the bean definition
 */
public GenericBeanDefinition bean(Class<?> type) {
	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(type);
	return beanDefinition;
}
 
Example 18
Source File: GroovyBeanDefinitionReader.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Define an inner bean definition.
 * @param type the bean type
 * @return the bean definition
 */
public GenericBeanDefinition bean(Class<?> type) {
	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(type);
	return beanDefinition;
}
 
Example 19
Source File: GspAutoConfiguration.java    From grails-boot with Apache License 2.0 4 votes vote down vote up
protected GenericBeanDefinition createBeanDefinition(Class<?> beanClass) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(beanClass);
    beanDefinition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_NAME);
    return beanDefinition;
}
 
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();
            }
        }
    }
}