Search in sources :

Example 1 with ScopeMetadata

use of org.springframework.context.annotation.ScopeMetadata in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method createContext.

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;
}
Also used : ClassPathBeanDefinitionScanner(org.springframework.context.annotation.ClassPathBeanDefinitionScanner) ScopeMetadata(org.springframework.context.annotation.ScopeMetadata) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) ScopeMetadataResolver(org.springframework.context.annotation.ScopeMetadataResolver) AnnotatedBeanDefinition(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext)

Aggregations

AnnotatedBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedBeanDefinition)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 ClassPathBeanDefinitionScanner (org.springframework.context.annotation.ClassPathBeanDefinitionScanner)1 ScopeMetadata (org.springframework.context.annotation.ScopeMetadata)1 ScopeMetadataResolver (org.springframework.context.annotation.ScopeMetadataResolver)1 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)1