Java Code Examples for org.springframework.beans.factory.config.BeanDefinition#setBeanClassName()

The following examples show how to use org.springframework.beans.factory.config.BeanDefinition#setBeanClassName() . These examples are extracted from open source projects. 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 Project: java-technology-stack   File: AopConfigUtils.java    License: MIT License 6 votes vote down vote up
@Nullable
private static BeanDefinition registerOrEscalateApcAsRequired(
		Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source) {

	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");

	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
			int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
			int requiredPriority = findPriorityForClass(cls);
			if (currentPriority < requiredPriority) {
				apcDefinition.setBeanClassName(cls.getName());
			}
		}
		return null;
	}

	RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
	beanDefinition.setSource(source);
	beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
	beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
	return beanDefinition;
}
 
Example 2
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	final String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
	for (String name : beanDefinitionNames) {
		final BeanDefinition beanDefinition = beanFactory.getBeanDefinition(name);
		final String beanClassName = beanDefinition.getBeanClassName();
		try {
			final Class<?> beanClass = Class.forName(beanClassName);
			final DeprecatedClass annotation = beanClass.getAnnotation(DeprecatedClass.class);
			if (annotation != null) {
				beanDefinition.setBeanClassName(annotation.newImpl().getName());
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
Example 3
private static BeanDefinition registerOrEscalateApcAsRequired(Class<?> cls, BeanDefinitionRegistry registry, Object source) {
	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
			int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
			int requiredPriority = findPriorityForClass(cls);
			if (currentPriority < requiredPriority) {
				apcDefinition.setBeanClassName(cls.getName());
			}
		}
		return null;
	}
	RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
	beanDefinition.setSource(source);
	beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
	beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
	return beanDefinition;
}
 
Example 4
private static BeanDefinition registerOrEscalateApcAsRequired(Class<?> cls, BeanDefinitionRegistry registry, Object source) {
	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
			int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
			int requiredPriority = findPriorityForClass(cls);
			if (currentPriority < requiredPriority) {
				apcDefinition.setBeanClassName(cls.getName());
			}
		}
		return null;
	}
	RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
	beanDefinition.setSource(source);
	beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
	beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
	return beanDefinition;
}
 
Example 5
@Override
public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory ) throws BeansException {
    String[] beanNames = beanFactory.getBeanDefinitionNames();
    for( String beanName : beanNames ) {
        if( beanFactory.containsBeanDefinition( beanName ) ) {
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition( beanName );
            if( beanDefinition instanceof AnnotatedBeanDefinition ) {
                AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
                if( annotatedBeanDefinition.getMetadata().hasAnnotation( JGivenStage.class.getName() ) ) {
                    String className = beanDefinition.getBeanClassName();
                    Class<?> stageClass = createStageClass( beanName, className );
                    beanDefinition.setBeanClassName( stageClass.getName() );
                }
            }
        }
    }
}
 
Example 6
Source Project: das   File: DasTransactionalEnabler.java    License: Apache License 2.0 5 votes vote down vote up
private void replaceBeanDefinition() {
    for(String beanName: getBeanDefinitionNames()) {
        BeanDefinition beanDef = getBeanDefinition(beanName);
        String beanClassName = beanDef.getBeanClassName();

        if(beanClassName == null || beanClassName.equals(BEAN_FACTORY_NAME) || isSpringSmartClassLoader(beanDef)) {
            continue;
        }

        Class beanClass;
        try {
            beanClass = Class.forName(beanDef.getBeanClassName());
        } catch (ClassNotFoundException e) {
            throw new BeanDefinitionValidationException("Cannot validate bean: " + beanName, e);
        }

        boolean annotated = false;
        for (Method method : beanClass.getMethods()) {
            if(isTransactionAnnotated(method)) {
                annotated = true;
                break;
            }
        }

        if(!annotated) {
            continue;
        }

        beanDef.setBeanClassName(BEAN_FACTORY_NAME);
        beanDef.setFactoryMethodName(FACTORY_METHOD_NAME);

        ConstructorArgumentValues cav = beanDef.getConstructorArgumentValues();

        if(cav.getArgumentCount() != 0) {
            throw new BeanDefinitionValidationException("The transactional bean can only be instantiated with default constructor.");
        }

        cav.addGenericArgumentValue(beanClass.getName());
    }
}
 
Example 7
Source Project: spring-analysis-note   File: AopConfigUtils.java    License: MIT License 5 votes vote down vote up
@Nullable
private static BeanDefinition registerOrEscalateApcAsRequired(
		Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source) {

	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
	if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
		// 如果在 registry 已经存在自动代理创建器,并且传入的代理器类型与注册的不一致,根据优先级判断是否需要修改
		BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
		if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
			// 根据优先级选择使用哪一个
			int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
			int requiredPriority = findPriorityForClass(cls);
			if (currentPriority < requiredPriority) {
				// 传进来的参数优先级更大,修改注册的 beanName,使用传进来的代理创建器
				apcDefinition.setBeanClassName(cls.getName());
			}
		}
		// 因为已经存在代理器,不需要之后的默认设置,直接返回
		return null;
	}

	RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
	beanDefinition.setSource(source);
	// 默认的是最小优先级
	beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
	beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	// 自动代理创建器的注册名字永远是 org.springframework.aop.config.internalAutoProxyCreator
	registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
	return beanDefinition;
}
 
Example 8
private void replaceBeanWithInstrumentedClass(BeanDefinition definition, Class<?> configurationType) {
    definition.setBeanClassName(ConfigurationFactoryBean.class.getName());
    MutablePropertyValues propertyValues = definition.getPropertyValues();
    propertyValues.addPropertyValue("configurationFactory", new RuntimeBeanReference(CONF4J_CONFIGURATION_FACTORY));
    propertyValues.addPropertyValue("configurationSource", new RuntimeBeanReference(CONF4J_CONFIGURATION_SOURCE));
    propertyValues.addPropertyValue("configurationType", configurationType);
}
 
Example 9
/**
 * @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
 */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
{
    ParameterCheck.mandatory("beanName", beanName);
    ParameterCheck.mandatory("extendingBeanName", extendingBeanName);

    // check for bean name
    if (!beanFactory.containsBean(beanName))
    {
        throw new NoSuchBeanDefinitionException("Can't find bean '" + beanName + "' to be extended.");
    }

    // check for extending bean
    if (!beanFactory.containsBean(extendingBeanName))
    {
        throw new NoSuchBeanDefinitionException("Can't find bean '" + extendingBeanName + "' that is going to extend original bean definition.");
    }

    // get the bean definitions
    BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
    BeanDefinition extendingBeanDefinition = beanFactory.getBeanDefinition(extendingBeanName);

    // update class
    if (StringUtils.isNotBlank(extendingBeanDefinition.getBeanClassName()) &&
        !beanDefinition.getBeanClassName().equals(extendingBeanDefinition.getBeanClassName()))
    {
        beanDefinition.setBeanClassName(extendingBeanDefinition.getBeanClassName());
    }

    // update properties
    MutablePropertyValues properties = beanDefinition.getPropertyValues();
    MutablePropertyValues extendingProperties = extendingBeanDefinition.getPropertyValues();
    for (PropertyValue propertyValue : extendingProperties.getPropertyValueList())
    {
        properties.add(propertyValue.getName(), propertyValue.getValue());
    }
}
 
Example 10
public static <T> void registerBeanDefinition(Class<T> clazz, String scope) {
    BeanDefinition definition = new GenericBeanDefinition();
    definition.setScope(scope);
    definition.setBeanClassName(clazz.getName());
    Lock wlock = rwLock.writeLock();
    wlock.lock();
    try {
        ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(clazz.getName(), definition);
        // TODO : Has to register beans twice to flush cache, might be a Spring defect. Workaround.
        ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(clazz.getName(), definition);
    } finally {
        wlock.unlock();
    }
}
 
Example 11
/**
 * {@inheritDoc}
 *
 * @since 1.0.2
 */
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
	if (this.processPropertyPlaceHolders) {
		processPropertyPlaceHolders();
	}
	String[] beanDefinitionNames = registry.getBeanDefinitionNames();
	for (String beanDefinitionName : beanDefinitionNames) {
		BeanDefinition beanDefinition = registry.getBeanDefinition(beanDefinitionName);
		String beanClassName = beanDefinition.getBeanClassName();
		if (SqlSessionFactoryBean.class.getName().equals(beanClassName)) {
			beanDefinition.setBeanClassName(FixedSqlSessionFactoryBean.class.getName());
		}
	}

	ZebraClassPathMapperScanner scanner = new ZebraClassPathMapperScanner(registry);
	scanner.setAddToConfig(this.addToConfig);
	scanner.setAnnotationClass(this.annotationClass);
	scanner.setMarkerInterface(this.markerInterface);
	scanner.setSqlSessionFactory(this.sqlSessionFactory);
	scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
	scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
	scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
	scanner.setResourceLoader(this.applicationContext);
	scanner.setBeanNameGenerator(this.nameGenerator);
	scanner.registerFilters();
	scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage,
	      ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
 
Example 12
@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    for (String name : beanFactory.getBeanNamesForType(TransactionInterceptor.class, false, false)) {
        BeanDefinition bd = beanFactory.getBeanDefinition(name);
        bd.setBeanClassName(DomainTransactionInterceptor.class.getName());
        bd.setFactoryBeanName(null);
        bd.setFactoryMethodName(null);
    }
}
 
Example 13
Source Project: dal   File: DalTransactionalEnabler.java    License: Apache License 2.0 4 votes vote down vote up
private void replaceBeanDefinition() {
    for (String beanName : getBeanDefinitionNames()) {
        BeanDefinition beanDef = getBeanDefinition(beanName);
        String beanClassName = beanDef.getBeanClassName();

        if (beanClassName == null || beanClassName.equals(BEAN_FACTORY_NAME))
            continue;

        Class beanClass;
        try {
            beanClass = Class.forName(beanDef.getBeanClassName());
        } catch (ClassNotFoundException e) {
            throw new BeanDefinitionValidationException("Cannot validate bean: " + beanName, e);
        }

        boolean annotated = false;

        List<Method> methods = new ArrayList<>();
        ReflectUtils.addAllMethods(beanClass, methods);
        List<Method> unsupportedMethods = new ArrayList<>();

        for (int i = 0; i < methods.size(); i++) {
            Method currentMethod = methods.get(i);

            if (isTransactionAnnotated(currentMethod)) {
                if (Modifier.isFinal(currentMethod.getModifiers()) || Modifier.isStatic(currentMethod.getModifiers()) || Modifier.isPrivate(currentMethod.getModifiers()))
                    unsupportedMethods.add(currentMethod);
                annotated = true;
            }
        }

        if (!unsupportedMethods.isEmpty()){
            StringBuilder errMsg=new StringBuilder();
            errMsg.append(String.format("The Methods below are not supported in dal transaction due to private, final or static modifier, please use public,protected or default modifier instead:"));
            errMsg.append("\n");
            int index=1;
            for (Method method : unsupportedMethods) {
                errMsg.append(String.format("%d. %s", index, method.toString()));
                errMsg.append("\n");
                index++;
            }
            throw new DalRuntimeException(errMsg.toString());
        }

        if (!annotated)
            continue;

        beanDef.setBeanClassName(BEAN_FACTORY_NAME);
        beanDef.setFactoryMethodName(FACTORY_METHOD_NAME);

        ConstructorArgumentValues cav = beanDef.getConstructorArgumentValues();

        if (cav.getArgumentCount() != 0)
            throw new BeanDefinitionValidationException("The transactional bean can only be instantiated with default constructor.");

        cav.addGenericArgumentValue(beanClass.getName());
    }
}