org.springframework.beans.factory.annotation.AnnotatedBeanDefinition Java Examples

The following examples show how to use org.springframework.beans.factory.annotation.AnnotatedBeanDefinition. 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: spring4-understanding   Author: langtianya   File: AnnotationConfigUtils.java    License: Apache License 2.0 6 votes vote down vote up
static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) {
	if (metadata.isAnnotated(Lazy.class.getName())) {
		abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value"));
	}
	else if (abd.getMetadata() != metadata && abd.getMetadata().isAnnotated(Lazy.class.getName())) {
		abd.setLazyInit(attributesFor(abd.getMetadata(), Lazy.class).getBoolean("value"));
	}

	if (metadata.isAnnotated(Primary.class.getName())) {
		abd.setPrimary(true);
	}
	if (metadata.isAnnotated(DependsOn.class.getName())) {
		abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value"));
	}

	if (abd instanceof AbstractBeanDefinition) {
		AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd;
		if (metadata.isAnnotated(Role.class.getName())) {
			absBd.setRole(attributesFor(metadata, Role.class).getNumber("value").intValue());
		}
		if (metadata.isAnnotated(Description.class.getName())) {
			absBd.setDescription(attributesFor(metadata, Description.class).getString("value"));
		}
	}
}
 
Example #2
Source Project: spring4-understanding   Author: langtianya   File: AnnotationBeanNameGenerator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Derive a bean name from one of the annotations on the class.
 * @param annotatedDef the annotation-aware bean definition
 * @return the bean name, or {@code null} if none is found
 */
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
	AnnotationMetadata amd = annotatedDef.getMetadata();
	Set<String> types = amd.getAnnotationTypes();
	String beanName = null;
	for (String type : types) {
		AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
		if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
			Object value = attributes.get("value");
			if (value instanceof String) {
				String strVal = (String) value;
				if (StringUtils.hasLength(strVal)) {
					if (beanName != null && !strVal.equals(beanName)) {
						throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
								"component names: '" + beanName + "' versus '" + strVal + "'");
					}
					beanName = strVal;
				}
			}
		}
	}
	return beanName;
}
 
Example #3
/**
 * Gets the annotation attributes {@link RestTemplate} bean being annotated
 * {@link DubboTransported @DubboTransported}.
 * @param beanName the bean name of {@link LoadBalanced @LoadBalanced}
 * {@link RestTemplate}
 * @param attributesResolver {@link DubboTransportedAttributesResolver}
 * @return non-null {@link Map}
 */
private Map<String, Object> getDubboTranslatedAttributes(String beanName,
		DubboTransportedAttributesResolver attributesResolver) {
	Map<String, Object> attributes = Collections.emptyMap();
	BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
	if (beanDefinition instanceof AnnotatedBeanDefinition) {
		AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
		MethodMetadata factoryMethodMetadata = annotatedBeanDefinition
				.getFactoryMethodMetadata();
		attributes = factoryMethodMetadata != null ? Optional
				.ofNullable(factoryMethodMetadata
						.getAnnotationAttributes(DUBBO_TRANSPORTED_CLASS_NAME))
				.orElse(attributes) : Collections.emptyMap();
	}
	return attributesResolver.resolve(attributes);
}
 
Example #4
Source Project: java-technology-stack   Author: codeEngraver   File: AnnotationBeanNameGenerator.java    License: MIT License 6 votes vote down vote up
/**
 * Derive a bean name from one of the annotations on the class.
 * @param annotatedDef the annotation-aware bean definition
 * @return the bean name, or {@code null} if none is found
 */
@Nullable
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
	AnnotationMetadata amd = annotatedDef.getMetadata();
	Set<String> types = amd.getAnnotationTypes();
	String beanName = null;
	for (String type : types) {
		AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
		if (attributes != null && isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
			Object value = attributes.get("value");
			if (value instanceof String) {
				String strVal = (String) value;
				if (StringUtils.hasLength(strVal)) {
					if (beanName != null && !strVal.equals(beanName)) {
						throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
								"component names: '" + beanName + "' versus '" + strVal + "'");
					}
					beanName = strVal;
				}
			}
		}
	}
	return beanName;
}
 
Example #5
Source Project: JGiven   Author: TNG   File: JGivenBeanFactoryPostProcessor.java    License: Apache License 2.0 6 votes vote down vote up
@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: lams   Author: lamsfoundation   File: AnnotationConfigUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) {
	if (metadata.isAnnotated(Lazy.class.getName())) {
		abd.setLazyInit(attributesFor(metadata, Lazy.class).getBoolean("value"));
	}
	else if (abd.getMetadata() != metadata && abd.getMetadata().isAnnotated(Lazy.class.getName())) {
		abd.setLazyInit(attributesFor(abd.getMetadata(), Lazy.class).getBoolean("value"));
	}

	if (metadata.isAnnotated(Primary.class.getName())) {
		abd.setPrimary(true);
	}
	if (metadata.isAnnotated(DependsOn.class.getName())) {
		abd.setDependsOn(attributesFor(metadata, DependsOn.class).getStringArray("value"));
	}

	if (abd instanceof AbstractBeanDefinition) {
		AbstractBeanDefinition absBd = (AbstractBeanDefinition) abd;
		if (metadata.isAnnotated(Role.class.getName())) {
			absBd.setRole(attributesFor(metadata, Role.class).getNumber("value").intValue());
		}
		if (metadata.isAnnotated(Description.class.getName())) {
			absBd.setDescription(attributesFor(metadata, Description.class).getString("value"));
		}
	}
}
 
Example #7
Source Project: network-spring-boot-starter   Author: ThierrySquirrel   File: NetworkAssemble.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	ClassPathScanningCandidateComponentProvider scanner = new ScanningComponent(Boolean.FALSE, this.environment);
	scanner.setResourceLoader(this.resourceLoader);

	AnnotationTypeFilter annotationTypeFilter = new AnnotationTypeFilter(Network.class);
	scanner.addIncludeFilter(annotationTypeFilter);

	String packageName = ClassUtils.getPackageName(importingClassMetadata.getClassName());
	Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(packageName);
	candidateComponents.forEach(beanDefinition -> {
		AnnotatedBeanDefinition annotatedBeanDefinition = (AnnotatedBeanDefinition) beanDefinition;
		AnnotationMetadata annotationMetadata = annotatedBeanDefinition.getMetadata();
		BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(NetworkFactory.class);
		String className = annotationMetadata.getClassName();
		definition.addPropertyValue(NetworkFactoryConstants.PROPERTY_VALUE.getValue(), className);
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);

		AbstractBeanDefinition abstractBeanDefinition = definition.getBeanDefinition();
		BeanDefinitionHolder holder = new BeanDefinitionHolder(abstractBeanDefinition, className);
		BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);

	});

}
 
Example #8
Source Project: BootNettyRpc   Author: forezp   File: NettyRpcRegistrar.java    License: Apache License 2.0 6 votes vote down vote up
protected ClassPathScanningCandidateComponentProvider getScanner() {

        return new ClassPathScanningCandidateComponentProvider( false, this.environment ) {

            @Override
            protected boolean isCandidateComponent(
                    AnnotatedBeanDefinition beanDefinition) {
                if (beanDefinition.getMetadata().isIndependent()) {

                    if (beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().getInterfaceNames().length == 1
                            && Annotation.class.getName().equals( beanDefinition.getMetadata().getInterfaceNames()[0] )) {
                        try {
                            Class<?> target = ClassUtils.forName( beanDefinition.getMetadata().getClassName(), NettyRpcRegistrar.this.classLoader );
                            return !target.isAnnotation();
                        } catch (Exception ex) {
                            this.logger.error( "Could not load target class: " + beanDefinition.getMetadata().getClassName(), ex );

                        }
                    }
                    return true;
                }
                return false;

            }
        };
    }
 
Example #9
Source Project: flower   Author: zhihuili   File: FlowerClassPathBeanDefinitionScanner.java    License: Apache License 2.0 6 votes vote down vote up
public Set<BeanDefinitionHolder> doScan(String... basePackages) {
  Assert.notEmpty(basePackages, "At least one base package must be specified");
  Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
  for (String basePackage : basePackages) {
    Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
    for (BeanDefinition candidate : candidates) {
      ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
      candidate.setScope(scopeMetadata.getScopeName());
      String beanName = this.beanNameGenerator.generateBeanName(candidate, this.getRegistry());
      if (candidate instanceof AbstractBeanDefinition) {
        postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
      }
      if (candidate instanceof AnnotatedBeanDefinition) {
        AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
      }
      if (checkCandidate(beanName, candidate)) {
        BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
        beanDefinitions.add(definitionHolder);
      }
    }
  }
  return beanDefinitions;
}
 
Example #10
Source Project: faster-framework-project   Author: faster-framework   File: AnnotationBeanNameGenerator.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Derive a bean name from one of the annotations on the class.
 *
 * @param annotatedDef the annotation-aware bean definition
 * @return the bean name, or {@code null} if none is found
 */
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
    AnnotationMetadata amd = annotatedDef.getMetadata();
    Set<String> types = amd.getAnnotationTypes();
    String beanName = null;
    for (String type : types) {
        AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
        if (isStereotypeWithNameValue(type, amd.getMetaAnnotationTypes(type), attributes)) {
            Object value = attributes.get("value");
            if (value instanceof String) {
                String strVal = (String) value;
                if (StringUtils.hasLength(strVal)) {
                    if (beanName != null && !strVal.equals(beanName)) {
                        throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
                                "component names: '" + beanName + "' versus '" + strVal + "'");
                    }
                    beanName = strVal;
                }
            }
        }
    }
    return beanName;
}
 
Example #11
Source Project: spring-boot-protocol   Author: wangzihaogithub   File: NettyRpcClientBeanDefinitionRegistrar.java    License: Apache License 2.0 6 votes vote down vote up
private void registerNettyRpcClient(AnnotatedBeanDefinition beanDefinition,BeanDefinitionRegistry registry)  {
    AnnotationMetadata metadata = beanDefinition.getMetadata();
    Map<String, Object> nettyRpcClientAttributes = metadata.getAnnotationAttributes(nettyRpcClientCanonicalName);
    Map<String, Object> lazyAttributes = metadata.getAnnotationAttributes(lazyCanonicalName);

    Class<?> beanClass;
    try {
        beanClass = ClassUtils.forName(metadata.getClassName(), classLoader);
    } catch (ClassNotFoundException e) {
        throw new BeanCreationException("NettyRpcClientsRegistrar failure! notfound class",e);
    }

    String serviceName = resolve((String) nettyRpcClientAttributes.get("serviceName"));
    beanDefinition.setLazyInit(lazyAttributes == null || Boolean.TRUE.equals(lazyAttributes.get("value")));
    ((AbstractBeanDefinition)beanDefinition).setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    ((AbstractBeanDefinition)beanDefinition).setInstanceSupplier(newInstanceSupplier(beanClass,serviceName,(int)nettyRpcClientAttributes.get("timeout")));

    String beanName = generateBeanName(beanDefinition.getBeanClassName());
    registry.registerBeanDefinition(beanName,beanDefinition);
}
 
Example #12
Source Project: lams   Author: lamsfoundation   File: AnnotationScopeMetadataResolver.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
	ScopeMetadata metadata = new ScopeMetadata();
	if (definition instanceof AnnotatedBeanDefinition) {
		AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
		AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(
				annDef.getMetadata(), this.scopeAnnotationType);
		if (attributes != null) {
			metadata.setScopeName(attributes.getString("value"));
			ScopedProxyMode proxyMode = attributes.getEnum("proxyMode");
			if (proxyMode == null || proxyMode == ScopedProxyMode.DEFAULT) {
				proxyMode = this.defaultProxyMode;
			}
			metadata.setScopedProxyMode(proxyMode);
		}
	}
	return metadata;
}
 
Example #13
Source Project: nacos-spring-project   Author: nacos-group   File: AnnotationNacosPropertySourceBuilder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Object>[] resolveRuntimeAttributesArray(
		AnnotatedBeanDefinition beanDefinition, Properties globalNacosProperties) {
	// Get AnnotationMetadata
	AnnotationMetadata metadata = beanDefinition.getMetadata();

	Set<String> annotationTypes = metadata.getAnnotationTypes();

	List<Map<String, Object>> annotationAttributesList = new LinkedList<Map<String, Object>>();

	for (String annotationType : annotationTypes) {
		annotationAttributesList
				.addAll(getAnnotationAttributesList(metadata, annotationType));
	}

	return annotationAttributesList.toArray(new Map[0]);
}
 
Example #14
private void registerBeanDefinitions(BeanDefinitionRegistry registry, String[] basePackages) {

        ExposingClassPathBeanDefinitionScanner scanner = new ExposingClassPathBeanDefinitionScanner(registry, false,
                getEnvironment(), getResourceLoader());

        BeanNameGenerator beanNameGenerator = resolveAnnotatedBeanNameGenerator(registry);
        // Set the BeanNameGenerator
        scanner.setBeanNameGenerator(beanNameGenerator);
        // Add the AnnotationTypeFilter for annotationTypes
        for (Class<? extends Annotation> supportedAnnotationType : getSupportedAnnotationTypes()) {
            scanner.addIncludeFilter(new AnnotationTypeFilter(supportedAnnotationType));
        }
        // Register the primary BeanDefinitions
        Map<String, AnnotatedBeanDefinition> primaryBeanDefinitions = registerPrimaryBeanDefinitions(scanner, basePackages);
        // Register the secondary BeanDefinitions
        registerSecondaryBeanDefinitions(scanner, primaryBeanDefinitions, basePackages);
    }
 
Example #15
Source Project: spring4-understanding   Author: langtianya   File: AnnotationScopeMetadataResolverTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void customRequestScopeWithAttribute() {
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(
		AnnotatedWithCustomRequestScopeWithAttributeOverride.class);
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode());
}
 
Example #16
Source Project: spring-context-support   Author: alibaba   File: AnnotatedBeanDefinitionRegistryUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Is present bean that was registered by the specified {@link Annotation annotated} {@link Class class}
 *
 * @param registry       {@link BeanDefinitionRegistry}
 * @param annotatedClass the {@link Annotation annotated} {@link Class class}
 * @return if present, return <code>true</code>, or <code>false</code>
 * @since 1.0.3
 */
public static boolean isPresentBean(BeanDefinitionRegistry registry, Class<?> annotatedClass) {

    boolean present = false;

    String[] beanNames = registry.getBeanDefinitionNames();

    ClassLoader classLoader = annotatedClass.getClassLoader();

    for (String beanName : beanNames) {
        BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
        if (beanDefinition instanceof AnnotatedBeanDefinition) {
            AnnotationMetadata annotationMetadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
            String className = annotationMetadata.getClassName();
            Class<?> targetClass = resolveClassName(className, classLoader);
            present = nullSafeEquals(targetClass, annotatedClass);
            if (present) {
                if (logger.isDebugEnabled()) {
                    logger.debug(format("The annotatedClass[class : %s , bean name : %s] was present in registry[%s]",
                            className, beanName, registry));
                }
                break;
            }
        }
    }

    return present;
}
 
Example #17
Source Project: spring4-understanding   Author: langtianya   File: AnnotationScopeMetadataResolverTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void customRequestScopeWithAttributeViaAsm() throws IOException {
	MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
	MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScopeWithAttributeOverride.class.getName());
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata());
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode());
}
 
Example #18
Source Project: MicroCommunity   Author: java110   File: Java110ListenerDiscoveryRegistrar.java    License: Apache License 2.0 5 votes vote down vote up
protected ClassPathScanningCandidateComponentProvider getScanner() {
    return new ClassPathScanningCandidateComponentProvider(false) {

        @Override
        protected boolean isCandidateComponent(
                AnnotatedBeanDefinition beanDefinition) {
            if (beanDefinition.getMetadata().isIndependent()) {
                // TODO until SPR-11711 will be resolved
                if (beanDefinition.getMetadata().isInterface()
                        && beanDefinition.getMetadata()
                        .getInterfaceNames().length == 1
                        && Annotation.class.getName().equals(beanDefinition
                        .getMetadata().getInterfaceNames()[0])) {
                    try {
                        Class<?> target = ClassUtils.forName(
                                beanDefinition.getMetadata().getClassName(),
                                Java110ListenerDiscoveryRegistrar.this.classLoader);
                        return !target.isAnnotation();
                    }
                    catch (Exception ex) {
                        this.logger.error(
                                "Could not load target class: "
                                        + beanDefinition.getMetadata().getClassName(),
                                ex);

                    }
                }
                return true;
            }
            return false;

        }
    };
}
 
Example #19
Source Project: spring-analysis-note   Author: Vip-Augus   File: ClassPathBeanDefinitionScanner.java    License: MIT License 5 votes vote down vote up
/**
 * Perform a scan within the specified base packages,
 * returning the registered bean definitions.
 * <p>This method does <i>not</i> register an annotation config processor
 * but rather leaves this up to the caller.
 * @param basePackages the packages to check for annotated classes
 * @return set of beans registered if any for tooling registration purposes (never {@code null})
 */
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
	Assert.notEmpty(basePackages, "At least one base package must be specified");
	Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
	for (String basePackage : basePackages) {
		Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
		for (BeanDefinition candidate : candidates) {
			ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
			candidate.setScope(scopeMetadata.getScopeName());
			String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
			if (candidate instanceof AbstractBeanDefinition) {
				postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
			}
			if (candidate instanceof AnnotatedBeanDefinition) {
				AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
			}
			if (checkCandidate(beanName, candidate)) {
				BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
				definitionHolder =
						AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
				beanDefinitions.add(definitionHolder);
				registerBeanDefinition(definitionHolder, this.registry);
			}
		}
	}
	return beanDefinitions;
}
 
Example #20
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationScopeMetadataResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void resolveScopeMetadataShouldNotApplyScopedProxyModeToSingleton() {
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithSingletonScope.class);
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals(BeanDefinition.SCOPE_SINGLETON, scopeMetadata.getScopeName());
	assertEquals(NO, scopeMetadata.getScopedProxyMode());
}
 
Example #21
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationScopeMetadataResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void resolveScopeMetadataShouldReadScopedProxyModeFromAnnotation() {
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithScopedProxy.class);
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(TARGET_CLASS, scopeMetadata.getScopedProxyMode());
}
 
Example #22
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationScopeMetadataResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void customRequestScope() {
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnnotatedWithCustomRequestScope.class);
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(NO, scopeMetadata.getScopedProxyMode());
}
 
Example #23
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationScopeMetadataResolverTests.java    License: MIT License 5 votes vote down vote up
@Test
public void customRequestScopeViaAsm() throws IOException {
	MetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory();
	MetadataReader reader = readerFactory.getMetadataReader(AnnotatedWithCustomRequestScope.class.getName());
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(reader.getAnnotationMetadata());
	ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(bd);
	assertNotNull("resolveScopeMetadata(..) must *never* return null.", scopeMetadata);
	assertEquals("request", scopeMetadata.getScopeName());
	assertEquals(NO, scopeMetadata.getScopedProxyMode());
}
 
Example #24
Source Project: spring4-understanding   Author: langtianya   File: AnnotationBeanNameGeneratorTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void generateBeanNameWithNamedComponentWhereTheNameIsBlank() {
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithBlankName.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertNotNull("The generated beanName must *never* be null.", beanName);
	assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName));
	String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
	assertEquals(expectedGeneratedBeanName, beanName);
}
 
Example #25
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationBeanNameGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() {
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnonymousComponent.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertNotNull("The generated beanName must *never* be null.", beanName);
	assertTrue("The generated beanName must *never* be blank.", StringUtils.hasText(beanName));
	String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
	assertEquals(expectedGeneratedBeanName, beanName);
}
 
Example #26
Source Project: onetwo   Author: wayshall   File: AbstractImportRegistrar.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	Class<? extends Annotation> componentAnnoClass = getComponentAnnotationClass();
	List<BeanDefinition> beandefList = scanBeanDefinitions(importingClassMetadata);
	beandefList.stream().filter(AnnotatedBeanDefinition.class::isInstance)
					.forEach(bd->{
						AnnotatedBeanDefinition beanDefinition = (AnnotatedBeanDefinition) bd;
						AnnotationMetadata annotationMetadata = beanDefinition.getMetadata();
						Assert.isTrue(annotationMetadata.isInterface(),
								"@"+componentAnnoClass.getSimpleName()+" can only be specified on an interface");

						AnnotationAttributes tagAttributes = SpringUtils.getAnnotationAttributes(annotationMetadata, componentAnnoClass);
						registerComponent(registry, annotationMetadata, tagAttributes);
					});
}
 
Example #27
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationBeanNameGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void generateBeanNameFromMetaComponentWithNonStringValue() {
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertEquals("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta", beanName);
}
 
Example #28
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationBeanNameGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void generateBeanNameFromComposedControllerAnnotationWithoutName() {
	// SPR-11360
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithoutName.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
	assertEquals(expectedGeneratedBeanName, beanName);
}
 
Example #29
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationBeanNameGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void generateBeanNameFromComposedControllerAnnotationWithBlankName() {
	// SPR-11360
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithBlankName.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
	assertEquals(expectedGeneratedBeanName, beanName);
}
 
Example #30
Source Project: spring-analysis-note   Author: Vip-Augus   File: AnnotationBeanNameGeneratorTests.java    License: MIT License 5 votes vote down vote up
@Test
public void generateBeanNameFromComposedControllerAnnotationWithStringValue() {
	// SPR-11360
	BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
	AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(
		ComposedControllerAnnotationWithStringValue.class);
	String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
	assertEquals("restController", beanName);
}