org.springframework.context.annotation.ScopeMetadata Java Examples

The following examples show how to use org.springframework.context.annotation.ScopeMetadata. 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: FlowerClassPathBeanDefinitionScanner.java    From flower with 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 #2
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
	scanner.setIncludeAnnotationConfig(false);
	scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {
		@Override
		public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
			ScopeMetadata metadata = new ScopeMetadata();
			if (definition instanceof AnnotatedBeanDefinition) {
				AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
				for (String type : annDef.getMetadata().getAnnotationTypes()) {
					if (type.equals(javax.inject.Singleton.class.getName())) {
						metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
						break;
					}
					else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
						metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
						metadata.setScopedProxyMode(scopedProxyMode);
						break;
					}
					else if (type.startsWith("javax.inject")) {
						metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
					}
				}
			}
			return metadata;
		}
	});

	// Scan twice in order to find errors in the bean definition compatibility check.
	scanner.scan(getClass().getPackage().getName());
	scanner.scan(getClass().getPackage().getName());

	context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
	context.refresh();
	return context;
}
 
Example #3
Source File: PluginMybatisMapperProcessor.java    From springboot-plugin-framework-parent with Apache License 2.0 5 votes vote down vote up
@Override
public void registry(PluginRegistryInfo pluginRegistryInfo) throws Exception {
    if(!mybatisExist()){
        return;
    }
    List<Class<?>> groupClasses = pluginRegistryInfo.getGroupClasses(PluginMapperGroup.KEY);
    if(groupClasses == null || groupClasses.isEmpty()){
        return;
    }
    BasePlugin basePlugin = pluginRegistryInfo.getBasePlugin();
    String pluginId = pluginRegistryInfo.getPluginWrapper().getPluginId();
    Set<String> beanNames = new HashSet<>();
    for (Class<?> groupClass : groupClasses) {
        if (groupClass == null) {
            continue;
        }
        BeanNameGenerator beanNameGenerator = new PluginAnnotationBeanNameGenerator(basePlugin.getWrapper().getPluginId());
        AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(groupClass);
        ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
        abd.setScope(scopeMetadata.getScopeName());
        String beanName = beanNameGenerator.generateBeanName(abd, applicationContext);
        BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
        AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
        BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, applicationContext);
        mybatisInjectWrapper.processBeanDefinitions(definitionHolder, groupClass);
        beanNames.add(beanName);
        PluginInfoContainer.addRegisterBeanName(pluginId, beanName);
    }
    pluginRegistryInfo.addProcessorInfo(KEY, beanNames);
}
 
Example #4
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
	scanner.setIncludeAnnotationConfig(false);
	scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {
		@Override
		public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
			ScopeMetadata metadata = new ScopeMetadata();
			if (definition instanceof AnnotatedBeanDefinition) {
				AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
				for (String type : annDef.getMetadata().getAnnotationTypes()) {
					if (type.equals(javax.inject.Singleton.class.getName())) {
						metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
						break;
					}
					else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
						metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
						metadata.setScopedProxyMode(scopedProxyMode);
						break;
					}
					else if (type.startsWith("javax.inject")) {
						metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
					}
				}
			}
			return metadata;
		}
	});

	// Scan twice in order to find errors in the bean definition compatibility check.
	scanner.scan(getClass().getPackage().getName());
	scanner.scan(getClass().getPackage().getName());

	context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
	context.refresh();
	return context;
}
 
Example #5
Source File: ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private ApplicationContext createContext(final ScopedProxyMode scopedProxyMode) {
	GenericWebApplicationContext context = new GenericWebApplicationContext();
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
	scanner.setIncludeAnnotationConfig(false);
	scanner.setScopeMetadataResolver(new ScopeMetadataResolver() {
		@Override
		public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
			ScopeMetadata metadata = new ScopeMetadata();
			if (definition instanceof AnnotatedBeanDefinition) {
				AnnotatedBeanDefinition annDef = (AnnotatedBeanDefinition) definition;
				for (String type : annDef.getMetadata().getAnnotationTypes()) {
					if (type.equals(javax.inject.Singleton.class.getName())) {
						metadata.setScopeName(BeanDefinition.SCOPE_SINGLETON);
						break;
					}
					else if (annDef.getMetadata().getMetaAnnotationTypes(type).contains(javax.inject.Scope.class.getName())) {
						metadata.setScopeName(type.substring(type.length() - 13, type.length() - 6).toLowerCase());
						metadata.setScopedProxyMode(scopedProxyMode);
						break;
					}
					else if (type.startsWith("javax.inject")) {
						metadata.setScopeName(BeanDefinition.SCOPE_PROTOTYPE);
					}
				}
			}
			return metadata;
		}
	});

	// Scan twice in order to find errors in the bean definition compatibility check.
	scanner.scan(getClass().getPackage().getName());
	scanner.scan(getClass().getPackage().getName());

	context.registerAlias("classPathBeanDefinitionScannerJsr330ScopeIntegrationTests.SessionScopedTestBean", "session");
	context.refresh();
	return context;
}
 
Example #6
Source File: ClassPathBeanDefinitionScannerAgent.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Resolve candidate to a bean definition and (re)load in Spring.
 * Synchronize to avoid parallel bean definition - usually on reload the beans are interrelated
 * and parallel load will cause concurrent modification exception.
 *
 * @param candidate the candidate to reload
 */
public void defineBean(BeanDefinition candidate) {
    synchronized (getClass()) { // TODO sychronize on DefaultListableFactory.beanDefinitionMap?

        ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
        candidate.setScope(scopeMetadata.getScopeName());
        String beanName = this.beanNameGenerator.generateBeanName(candidate, registry);

        if (candidate instanceof AbstractBeanDefinition) {
            postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
        }
        if (candidate instanceof AnnotatedBeanDefinition) {
            processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
        }

        removeIfExists(beanName);
        if (checkCandidate(beanName, candidate)) {

            BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
            definitionHolder = applyScopedProxyMode(scopeMetadata, definitionHolder, registry);

            LOGGER.reload("Registering Spring bean '{}'", beanName);
            LOGGER.debug("Bean definition '{}'", beanName, candidate);
            registerBeanDefinition(definitionHolder, registry);

            DefaultListableBeanFactory bf = maybeRegistryToBeanFactory();
            if (bf != null)
                ResetRequestMappingCaches.reset(bf);

            ProxyReplacer.clearAllProxies();
            freezeConfiguration();
        }
    }


}
 
Example #7
Source File: ClassPathBeanDefinitionScannerAgent.java    From HotswapAgent with GNU General Public License v2.0 5 votes vote down vote up
private BeanDefinitionHolder applyScopedProxyMode(
        ScopeMetadata metadata, BeanDefinitionHolder definition, BeanDefinitionRegistry registry) {
    return (BeanDefinitionHolder) ReflectionHelper.invoke(null, AnnotationConfigUtils.class,
            "applyScopedProxyMode", new Class[]{ScopeMetadata.class, BeanDefinitionHolder.class, BeanDefinitionRegistry.class},
            metadata, definition, registry);

}