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

The following examples show how to use org.springframework.beans.factory.support.GenericBeanDefinition#setAutowireMode() . 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: MybatisInjectWrapper.java    From springboot-plugin-framework-parent with Apache License 2.0 5 votes vote down vote up
public void processBeanDefinitions(BeanDefinitionHolder holder, Class a) {
    GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
    String beanClassName = definition.getBeanClassName();
    LOGGER.debug(() -> "Creating MapperFactoryBean with name '" + holder.getBeanName()
            + "' and '" + beanClassName + "' mapperInterface");

    definition.getConstructorArgumentValues().addGenericArgumentValue(a);
    definition.setBeanClass(this.mapperFactoryBeanClass);

    definition.getPropertyValues().add("addToConfig", this.addToConfig);

    boolean explicitFactoryUsed = false;
    if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
        definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
        explicitFactoryUsed = true;
    } else if (this.sqlSessionFactory != null) {
        definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
        explicitFactoryUsed = true;
    }

    if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
        if (!explicitFactoryUsed) {
            definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
            explicitFactoryUsed = true;
        }
    } else if (this.sqlSessionTemplate != null) {
        if (!explicitFactoryUsed) {
            definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
            explicitFactoryUsed = true;
        }
    }

    if (!explicitFactoryUsed) {
        LOGGER.debug(() -> "Enabling autowire by type for MapperFactoryBean with name '" +
                holder.getBeanName() + "'.");
        definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    }
}
 
Example 2
Source File: ExtensionLoader.java    From spi-imp with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Object parseClassToSpringBean(String name, Class<?> obj) {
    String beanName = obj.getSimpleName().concat(name);

    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(obj);
    GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
    definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_NAME);
    getRegistry().registerBeanDefinition(beanName, definition);

    return context.getBean(beanName);
}
 
Example 3
Source File: CustomBeanDefinitionRegistryPostProcessor.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
	System.out.println("postProcessBeanDefinitionRegistry");
	Class<?> cls = PersonBean.class;
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(cls);
	GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
	definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
	definition.getPropertyValues().add("name", "pepsi02");
	// 注册bean名,一般为类名首字母小写
	registry.registerBeanDefinition("person2", definition);

	BeanDefinition beanDefinition = registry.getBeanDefinition("person1");
	System.out.println("postProcessBeanDefinitionRegistry修改属性name值");
	beanDefinition.getPropertyValues().add("name", "tom");
}
 
Example 4
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 5
Source File: ReyClientBeanRegistrar.java    From x7 with Apache License 2.0 4 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {

    String startClassName = annotationMetadata.getClassName();
    String basePackage = startClassName.substring(0, startClassName.lastIndexOf("."));

    Set<Class<?>> set = ClassFileReader.getClasses(basePackage);

    Map<String, Object> attributes = annotationMetadata.getAnnotationAttributes(EnableReyClient.class.getName());
    Object obj = attributes.get("basePackages");
    if (obj != null){
        String[] strs = (String[]) obj;
        for (String str : strs){
            Set<Class<?>> set1 = ClassFileReader.getClasses(str);
            set.addAll(set1);
        }
    }

    List<String> beanNameList = new ArrayList<>();

    for (Class clz : set) {
        ReyClient annotation = (ReyClient)clz.getAnnotation(ReyClient.class);
        if (annotation == null)
            continue;

        ClientParser.parse(clz,environment);

        String beanName = clz.getName();
        beanNameList.add(beanName);

        String backend = annotation.circuitBreaker();
        if (backend.equals(" ")){
            backend = null;
        }

        boolean retry = annotation.retry();

        if (!registry.containsBeanDefinition(beanName)) {

            BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(clz);
            GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();
            definition.getPropertyValues().add("objectType", clz);
            definition.getPropertyValues().add("backend",backend);
            definition.getPropertyValues().add("retry",retry);
            definition.setBeanClass(HttpClientProxy.class);
            definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);

            registry.registerBeanDefinition(beanName, definition);

        }
    }
}
 
Example 6
Source File: ZebraClassPathMapperScanner.java    From Zebra with Apache License 2.0 4 votes vote down vote up
/**
 * Calls the parent search that will search and register all the candidates. Then the registered objects are post processed to
 * set them as MapperFactoryBeans
 */
@Override
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
	Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);

	if (beanDefinitions.isEmpty()) {
		logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages)
				+ "' package. Please check your configuration.");
	} else {
		for (BeanDefinitionHolder holder : beanDefinitions) {
			GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();

			if (logger.isDebugEnabled()) {
				logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '"
						+ definition.getBeanClassName() + "' mapperInterface");
			}

			// the mapper interface is the original class of the bean
			// but, the actual class of the bean is MapperFactoryBean
			definition.getPropertyValues().add("mapperInterface", definition.getBeanClassName());
			definition.setBeanClass(ZebraMapperFactoryBean.class);

			definition.getPropertyValues().add("addToConfig", this.addToConfig);

			boolean explicitFactoryUsed = false;
			if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {
				definition.getPropertyValues().add("sqlSessionFactory",
						new RuntimeBeanReference(this.sqlSessionFactoryBeanName));
				explicitFactoryUsed = true;
			} else if (this.sqlSessionFactory != null) {
				definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);
				explicitFactoryUsed = true;
			}

			if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {
				if (explicitFactoryUsed) {
					logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
				}
				definition.getPropertyValues().add("sqlSessionTemplate",
						new RuntimeBeanReference(this.sqlSessionTemplateBeanName));
				explicitFactoryUsed = true;
			} else if (this.sqlSessionTemplate != null) {
				if (explicitFactoryUsed) {
					logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");
				}
				definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);
				explicitFactoryUsed = true;
			}

			if (!explicitFactoryUsed) {
				if (logger.isDebugEnabled()) {
					logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName()
							+ "'.");
				}
				definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
			}
		}
	}

	return beanDefinitions;
}
 
Example 7
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;
}