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

The following examples show how to use org.springframework.beans.factory.support.GenericBeanDefinition#setPropertyValues() . 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: AbstractEntityModel.java    From OpERP with MIT License 6 votes vote down vote up
@PostConstruct
public void registerWithServer() {
	AutowireCapableBeanFactory factory = context
			.getAutowireCapableBeanFactory();
	BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(RmiServiceExporter.class);
	beanDefinition.setAutowireCandidate(true);

	MutablePropertyValues propertyValues = new MutablePropertyValues();

	Class<?> serviceInterface = this.getClass().getInterfaces()[0];

	propertyValues.addPropertyValue("serviceInterface", serviceInterface);
	String serviceName = serviceInterface.getCanonicalName();
	propertyValues.addPropertyValue("serviceName", serviceName);
	propertyValues.addPropertyValue("service", this);
	propertyValues.addPropertyValue("registryPort", "1099");
	beanDefinition.setPropertyValues(propertyValues);

	registry.registerBeanDefinition(serviceName, beanDefinition);
	context.getBean(serviceName); // Need this else
									// NotBoundException
	getService().registerClient(getHostAddress());

}
 
Example 2
Source File: BaseBeanFactory.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public List<BeanDefinitionHolder> createBeans(Map<String, Object> parameters) throws RuntimeConfigException {
	GenericBeanDefinition def = new GenericBeanDefinition();
	def.setBeanClassName(className);
	MutablePropertyValues propertyValues = new MutablePropertyValues();
	List<NamedObject> namedObjects = new ArrayList<NamedObject>();
	if (checkCollection(BEAN_REFS, NamedObject.class, parameters) != Priority.NONE) {
		namedObjects.addAll((Collection) parameters.get(BEAN_REFS));
	}
	for (String name : parameters.keySet()) {
		if (!ignoredParams.contains(name)) {
			propertyValues.addPropertyValue(name, beanDefinitionDtoConverterService
					.createBeanMetadataElementByIntrospection(parameters.get(name), namedObjects));
		}
	}
	def.setPropertyValues(propertyValues);
	BeanDefinitionHolder holder = new BeanDefinitionHolder(def, (String) parameters.get(BEAN_NAME));
	List<BeanDefinitionHolder> holders = new ArrayList<BeanDefinitionHolder>();
	holders.add(holder);
	return holders;
}
 
Example 3
Source File: BeanDefinitionDtoConverterServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
private BeanDefinition createBeanDefinitionByIntrospection(Object object, NamedBeanMap refs,
		ConversionService conversionService) {
	validate(object);
	GenericBeanDefinition def = new GenericBeanDefinition();
	def.setBeanClass(object.getClass());
	MutablePropertyValues propertyValues = new MutablePropertyValues();
	for (PropertyDescriptor descriptor : BeanUtils.getPropertyDescriptors(object.getClass())) {
		if (descriptor.getWriteMethod() != null) {
			try {
				Object value = descriptor.getReadMethod().invoke(object, (Object[]) null);
				if (value != null) {
					if ("id".equals(descriptor.getName())) {

					} else {
						propertyValues.addPropertyValue(descriptor.getName(),
								createMetadataElementByIntrospection(value, refs, conversionService));
					}
				}
			} catch (Exception e) {
				// our contract says to ignore this property
			}
		}
	}
	def.setPropertyValues(propertyValues);
	return def;
}
 
Example 4
Source File: BeanDefinitionDtoConverterServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Convert from a DTO to an internal Spring bean definition.
 * 
 * @param beanDefinitionDto The DTO object.
 * @return Returns a Spring bean definition.
 */
public BeanDefinition toInternal(BeanDefinitionInfo beanDefinitionInfo) {
	if (beanDefinitionInfo instanceof GenericBeanDefinitionInfo) {
		GenericBeanDefinitionInfo genericInfo = (GenericBeanDefinitionInfo) beanDefinitionInfo;
		GenericBeanDefinition def = new GenericBeanDefinition();
		def.setBeanClassName(genericInfo.getClassName());
		if (genericInfo.getPropertyValues() != null) {
			MutablePropertyValues propertyValues = new MutablePropertyValues();
			for (Entry<String, BeanMetadataElementInfo> entry : genericInfo.getPropertyValues().entrySet()) {
				BeanMetadataElementInfo info = entry.getValue();
				propertyValues.add(entry.getKey(), toInternal(info));
			}
			def.setPropertyValues(propertyValues);
		}
		return def;
	} else if (beanDefinitionInfo instanceof ObjectBeanDefinitionInfo) {
		ObjectBeanDefinitionInfo objectInfo = (ObjectBeanDefinitionInfo) beanDefinitionInfo;
		return createBeanDefinitionByIntrospection(objectInfo.getObject());
	} else {
		throw new IllegalArgumentException("Conversion to internal of " + beanDefinitionInfo.getClass().getName()
				+ " not implemented");
	}
}
 
Example 5
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 6
Source File: AbstractEntityService.java    From OpERP with MIT License 5 votes vote down vote up
@Override
public void registerClient(String clientAddress) {
	System.out.println("Client from " + clientAddress);

	ApplicationContext context = ServerApp.getApplicationContext();
	AutowireCapableBeanFactory factory = context
			.getAutowireCapableBeanFactory();
	BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
	GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
	beanDefinition.setBeanClass(RmiProxyFactoryBean.class);
	beanDefinition.setAutowireCandidate(true);

	Class<EM> entityModelInterfaceClass = getEntityModelInterfaceClass();

	MutablePropertyValues propertyValues = new MutablePropertyValues();
	propertyValues.addPropertyValue("serviceInterface",
			entityModelInterfaceClass);
	propertyValues.addPropertyValue("serviceUrl", "rmi://" + clientAddress
			+ ":1099/" + entityModelInterfaceClass.getCanonicalName());
	beanDefinition.setPropertyValues(propertyValues);

	registry.registerBeanDefinition(
			entityModelInterfaceClass.getCanonicalName(), beanDefinition);
	EM entityModel = context.getBean(entityModelInterfaceClass);
	registerEntityModel(entityModel);
	System.out.println(entityModel);

}
 
Example 7
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 8
Source File: AbstractStoreBeanDefinitionRegistrar.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected BeanDefinition createBeanDefinition(Class<?> beanType) {
	GenericBeanDefinition beanDef = new GenericBeanDefinition();
	beanDef.setBeanClass(beanType);

	MutablePropertyValues values = new MutablePropertyValues();
	beanDef.setPropertyValues(values);

	return beanDef;
}
 
Example 9
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 10
Source File: StaticApplicationContext.java    From spring4-understanding with Apache License 2.0 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 11
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 12
Source File: StaticApplicationContext.java    From lams with GNU General Public License v2.0 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 13
Source File: MangoDaoAutoCreator.java    From mango-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 向spring中注入dao代理
 * @param beanFactory
 */
private void registryMangoDao(DefaultListableBeanFactory beanFactory){
    for (Class<?> daoClass : findMangoDaoClasses(config.getScanPackage())) {
        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);
        beanFactory.registerBeanDefinition(daoClass.getName(), bf);
    }
}
 
Example 14
Source File: BeanPostProcessorRegister.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
                                    BeanDefinitionRegistry registry) {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(CommonAnnotationBeanPostProcessor.class);
    beanDefinition.setSynthetic(true);
    MutablePropertyValues values = new MutablePropertyValues();
    values.addPropertyValue("callback", new SpringBootAnnotationResolver());
    beanDefinition.setPropertyValues(values);
    registry.registerBeanDefinition("commonAnnotationBeanPostProcessor", beanDefinition);
}
 
Example 15
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 16
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 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, MutablePropertyValues pvs) throws BeansException {
	GenericBeanDefinition bd = new GenericBeanDefinition();
	bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE);
	bd.setBeanClass(clazz);
	bd.setPropertyValues(pvs);
	getDefaultListableBeanFactory().registerBeanDefinition(name, bd);
}
 
Example 18
Source File: SpringBootAnnotationResolver.java    From brpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the rpc proxy factory bean.
 *
 * @return the rpc proxy factory bean
 */
private RpcProxyFactoryBean createRpcProxyFactoryBean(RpcProxy rpcProxy,
                                                      DefaultListableBeanFactory beanFactory,
                                                      Class serviceInterface) {
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(RpcProxyFactoryBean.class);
    beanDef.setDependsOn("brpcApplicationContextUtils");
    MutablePropertyValues values = new MutablePropertyValues();
    BrpcConfig brpcConfig = getServiceConfig(beanFactory, serviceInterface);
    for (Field field : RpcClientOptions.class.getDeclaredFields()) {
        try {
            if (field.getType().equals(Logger.class)) {
                // ignore properties of org.slf4j.Logger class
                continue;
            }
            field.setAccessible(true);
            values.addPropertyValue(field.getName(), field.get(brpcConfig.getClient()));
        } catch (Exception ex) {
            log.warn("field not exist:", ex);
        }
    }
    values.addPropertyValue("serviceInterface", serviceInterface);
    values.addPropertyValue("serviceId", rpcProxy.name());
    if (brpcConfig.getNaming() != null) {
        values.addPropertyValue("namingServiceUrl", brpcConfig.getNaming().getNamingServiceUrl());
        values.addPropertyValue("group", brpcConfig.getNaming().getGroup());
        values.addPropertyValue("version", brpcConfig.getNaming().getVersion());
        values.addPropertyValue("ignoreFailOfNamingService",
                brpcConfig.getNaming().isIgnoreFailOfNamingService());
    }

    // interceptor
    String interceptorNames = brpcConfig.getClient().getInterceptorBeanNames();
    if (!StringUtils.isBlank(interceptorNames)) {
        List<Interceptor> customInterceptors = new ArrayList<>();
        String[] interceptorNameArray = interceptorNames.split(",");
        for (String interceptorBeanName : interceptorNameArray) {
            Interceptor interceptor = beanFactory.getBean(interceptorBeanName, Interceptor.class);
            customInterceptors.add(interceptor);
        }
        values.addPropertyValue("interceptors", Arrays.asList(customInterceptors));
    }

    beanDef.setPropertyValues(values);
    String serviceInterfaceBeanName = serviceInterface.getSimpleName();
    beanFactory.registerBeanDefinition(serviceInterfaceBeanName, beanDef);
    return beanFactory.getBean("&" + serviceInterfaceBeanName, RpcProxyFactoryBean.class);
}
 
Example 19
Source File: RpcAnnotationResolver.java    From brpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the rpc proxy factory bean.
 *
 * @param rpcProxy         the rpc proxy
 * @param beanFactory      the bean factory
 * @param rpcClientOptions the rpc client options
 * @param namingServiceUrl naming service url
 * @return the rpc proxy factory bean
 */
protected RpcProxyFactoryBean createRpcProxyFactoryBean(RpcProxy rpcProxy,
                                                        Class serviceInterface,
                                                        DefaultListableBeanFactory beanFactory,
                                                        RpcClientOptions rpcClientOptions,
                                                        String namingServiceUrl) {
    GenericBeanDefinition beanDef = new GenericBeanDefinition();
    beanDef.setBeanClass(RpcProxyFactoryBean.class);
    MutablePropertyValues values = new MutablePropertyValues();
    for (Field field : rpcClientOptions.getClass().getDeclaredFields()) {
        try {
            if (field.getType().equals(Logger.class)) {
                // ignore properties of org.slf4j.Logger class
                continue;
            }
            field.setAccessible(true);
            values.addPropertyValue(field.getName(), field.get(rpcClientOptions));
        } catch (Exception ex) {
            LOGGER.warn("field not exist:", ex);
        }
    }
    values.addPropertyValue("serviceInterface", serviceInterface);
    values.addPropertyValue("namingServiceUrl", namingServiceUrl);
    values.addPropertyValue("group", rpcProxy.group());
    values.addPropertyValue("version", rpcProxy.version());
    values.addPropertyValue("ignoreFailOfNamingService", rpcProxy.ignoreFailOfNamingService());
    values.addPropertyValue("serviceId", rpcProxy.name());

    // interceptor
    String interceptorNames = parsePlaceholder(rpcProxy.interceptorBeanNames());
    if (!StringUtils.isBlank(interceptorNames)) {
        List<Interceptor> customInterceptors = new ArrayList<Interceptor>();
        String[] interceptorNameArray = interceptorNames.split(",");
        for (String interceptorName : interceptorNameArray) {
            Interceptor interceptor = beanFactory.getBean(interceptorName, Interceptor.class);
            customInterceptors.add(interceptor);
        }
        values.addPropertyValue("interceptors", customInterceptors);
    } else {
        values.addPropertyValue("interceptors", interceptors);
    }

    beanDef.setPropertyValues(values);
    String serviceInterfaceBeanName = serviceInterface.getSimpleName();
    beanFactory.registerBeanDefinition(serviceInterfaceBeanName, beanDef);
    return beanFactory.getBean("&" + serviceInterfaceBeanName, RpcProxyFactoryBean.class);
}