private AnnotationRepositoryConfigurationSource getConfigurationSource( BeanDefinitionRegistry registry) { StandardAnnotationMetadata metadata = new StandardAnnotationMetadata( getConfiguration(), true); return new MybatisAnnotationRepositoryConfigurationSource(metadata, getAnnotation(), resourceLoader, this.environment, registry) { @Override public String[] getMapperLocations() { return MybatisRepositoriesAutoConfigureRegistrar.this.getMapperLocations(); } @Override public Iterable<String> getBasePackages() { return MybatisRepositoriesAutoConfigureRegistrar.this.getBasePackages(); } }; }
@Test public void testViewScopedClass() { GenericApplicationContext acx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(acx); acx.registerBeanDefinition("viewScopedClass", new AnnotatedGenericBeanDefinition( new StandardAnnotationMetadata(ViewScopedClass.class))); acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition( ScopedBeansConfiguration.class)); acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment())); acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment())); acx.refresh(); assertThat(acx.getBeanDefinition("viewScopedClass").getScope()) .isEqualTo(ViewScope.SCOPE_VIEW); assertThat(acx.getBeanDefinition("viewScopedBean").getScope()) .isEqualTo(ViewScope.SCOPE_VIEW); }
@Test public void testSessionScopedClass() { GenericApplicationContext acx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(acx); acx.registerBeanDefinition("sessionScopedClass", new AnnotatedGenericBeanDefinition( new StandardAnnotationMetadata(SessionScopedClass.class))); acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition( ScopedBeansConfiguration.class)); acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment())); acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment())); acx.refresh(); assertThat(acx.getBeanDefinition("sessionScopedClass").getScope()) .isEqualTo(WebApplicationContext.SCOPE_SESSION); assertThat(acx.getBeanDefinition("sessionScopedBean").getScope()) .isEqualTo(WebApplicationContext.SCOPE_SESSION); }
@Test public void testNoScopedClass() { GenericApplicationContext acx = new GenericApplicationContext(); AnnotationConfigUtils.registerAnnotationConfigProcessors(acx); acx.registerBeanDefinition("noScopedClass", new AnnotatedGenericBeanDefinition( new StandardAnnotationMetadata(NoScopedClass.class))); acx.registerBeanDefinition("scopedBeansConfiguration", new RootBeanDefinition( ScopedBeansConfiguration.class)); acx.addBeanFactoryPostProcessor(JsfScopeAnnotationsAutoConfiguration.jsfScopeAnnotationsConfigurer(acx.getEnvironment())); acx.addBeanFactoryPostProcessor(CdiScopeAnnotationsAutoConfiguration.cdiScopeAnnotationsConfigurer(acx.getEnvironment())); acx.refresh(); assertThat(acx.getBeanDefinition("noScopedClass").getScope()) .isEqualTo(""); assertThat(acx.getBeanDefinition("noScopedBean").getScope()) .isEqualTo(""); }
/** * Register member (nested) classes that happen to be configuration classes themselves. * @param metadata the metadata representation of the containing class * @throws IOException if there is any problem reading metadata from a member class */ private void processMemberClasses(AnnotationMetadata metadata) throws IOException { if (metadata instanceof StandardAnnotationMetadata) { for (Class<?> memberClass : ((StandardAnnotationMetadata) metadata).getIntrospectedClass().getDeclaredClasses()) { if (ConfigurationClassUtils.isConfigurationCandidate(new StandardAnnotationMetadata(memberClass))) { processConfigurationClass(new ConfigurationClass(memberClass, true)); } } } else { for (String memberClassName : metadata.getMemberClassNames()) { MetadataReader reader = this.metadataReaderFactory.getMetadataReader(memberClassName); AnnotationMetadata memberClassMetadata = reader.getAnnotationMetadata(); if (ConfigurationClassUtils.isConfigurationCandidate(memberClassMetadata)) { processConfigurationClass(new ConfigurationClass(reader, true)); } } } }
public static boolean matches(ConditionContext context) { Class<AuthorizationServerEndpointsConfigurationBeanCondition> type = AuthorizationServerEndpointsConfigurationBeanCondition.class; Conditional conditional = AnnotationUtils.findAnnotation(type, Conditional.class); StandardAnnotationMetadata metadata = new StandardAnnotationMetadata(type); for (Class<? extends Condition> conditionType : conditional.value()) { Condition condition = BeanUtils.instantiateClass(conditionType); if (condition.matches(context, metadata)) { return true; } } return false; }
/** * Factory method to obtain a {@link SourceClass} from a {@link ConfigurationClass}. */ public SourceClass asSourceClass(ConfigurationClass configurationClass) throws IOException { try { AnnotationMetadata metadata = configurationClass.getMetadata(); if (metadata instanceof StandardAnnotationMetadata) { return asSourceClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass()); } return asSourceClass(configurationClass.getMetadata().getClassName()); } catch (ClassNotFoundException ex) { throw new IllegalStateException(ex); } }
public SourceClass(Object source) { this.source = source; if (source instanceof Class<?>) { this.metadata = new StandardAnnotationMetadata((Class<?>) source, true); } else { this.metadata = ((MetadataReader) source).getAnnotationMetadata(); } }
/** * Create a new AnnotatedGenericBeanDefinition for the given annotation metadata, * allowing for ASM-based processing and avoidance of early loading of the bean class. * Note that this constructor is functionally equivalent to * {@link org.springframework.context.annotation.ScannedGenericBeanDefinition * ScannedGenericBeanDefinition}, however the semantics of the latter indicate that a * bean was discovered specifically via component-scanning as opposed to other means. * @param metadata the annotation metadata for the bean class in question * @since 3.1.1 */ public AnnotatedGenericBeanDefinition(AnnotationMetadata metadata) { Assert.notNull(metadata, "AnnotationMetadata must not be null"); if (metadata instanceof StandardAnnotationMetadata) { setBeanClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass()); } else { setBeanClassName(metadata.getClassName()); } this.metadata = metadata; }
/** * Factory method to obtain a {@link SourceClass} from a {@link ConfigurationClass}. */ public SourceClass asSourceClass(ConfigurationClass configurationClass) throws IOException { AnnotationMetadata metadata = configurationClass.getMetadata(); if (metadata instanceof StandardAnnotationMetadata) { return asSourceClass(((StandardAnnotationMetadata) metadata).getIntrospectedClass()); } return asSourceClass(configurationClass.getMetadata().getClassName()); }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; }
@Test public void metadataFromImportsOneThenTwo() { AnnotationMetadata importMetadata = new AnnotationConfigApplicationContext( ConfigurationOne.class, ConfigurationTwo.class) .getBean(MetadataHolder.class).importMetadata; assertEquals(ConfigurationOne.class, ((StandardAnnotationMetadata) importMetadata).getIntrospectedClass()); }
@Test public void metadataFromImportsTwoThenOne() { AnnotationMetadata importMetadata = new AnnotationConfigApplicationContext( ConfigurationTwo.class, ConfigurationOne.class) .getBean(MetadataHolder.class).importMetadata; assertEquals(ConfigurationOne.class, ((StandardAnnotationMetadata) importMetadata).getIntrospectedClass()); }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @throws IllegalArgumentException if beanName is null (as of Spring 3.1.1) * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; this.importedBy = null; }
private AnnotationRepositoryConfigurationSource getConfigurationSource() { StandardAnnotationMetadata metadata = new StandardAnnotationMetadata( getConfiguration(), true); return new AnnotationRepositoryConfigurationSource(metadata, getAnnotation(), this.resourceLoader, this.environment) { @Override public java.lang.Iterable<String> getBasePackages() { return AbstractRepositoryConfigurationSourceSupport.this .getBasePackages(); } }; }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @see ConfigurationClass#ConfigurationClass(Class, ConfigurationClass) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.getName()); this.beanName = beanName; }
@Before public void setUp() throws Exception { metadata = new StandardAnnotationMetadata(TestConfiguration.class, true); registry = new DefaultListableBeanFactory(); underTest = new AutoPropertiesRegistrar(); }
/** * Create a new {@link ConfigurationClass} with the given name. * @param clazz the underlying {@link Class} to represent * @param beanName name of the {@code @Configuration} class bean * @throws IllegalArgumentException if beanName is null (as of Spring 3.1.1) * @see ConfigurationClass#ConfigurationClass(Class, boolean) */ public ConfigurationClass(Class<?> clazz, String beanName) { Assert.hasText(beanName, "Bean name must not be null"); this.metadata = new StandardAnnotationMetadata(clazz, true); this.resource = new DescriptiveResource(clazz.toString()); this.beanName = beanName; this.imported = false; }
/** * Create a new AnnotatedGenericBeanDefinition for the given bean class. * @param beanClass the loaded bean class */ public AnnotatedGenericBeanDefinition(Class<?> beanClass) { setBeanClass(beanClass); this.metadata = new StandardAnnotationMetadata(beanClass, true); }
@Override protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationMetadata metadata = new StandardAnnotationMetadata(EnableMongoContentAutoConfiguration.class); AnnotationAttributes attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(this.getAnnotation().getName())); String[] basePackages = this.getBasePackages(); Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages); for (BeanDefinition definition : definitions) { String factoryBeanName = StoreUtils.getStoreFactoryBeanName(attributes); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(factoryBeanName); builder.getRawBeanDefinition().setSource(importingClassMetadata); builder.addPropertyValue(AbstractStoreBeanDefinitionRegistrar.STORE_INTERFACE_PROPERTY, definition.getBeanClassName()); registry.registerBeanDefinition(StoreUtils.getStoreBeanName(definition), builder.getBeanDefinition()); } }
@Override protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationMetadata metadata = new StandardAnnotationMetadata(EnableJpaContentAutoConfiguration.class); AnnotationAttributes attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(this.getAnnotation().getName())); String[] basePackages = this.getBasePackages(); Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages); for (BeanDefinition definition : definitions) { String factoryBeanName = StoreUtils.getStoreFactoryBeanName(attributes); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(factoryBeanName); builder.getRawBeanDefinition().setSource(importingClassMetadata); builder.addPropertyValue(AbstractStoreBeanDefinitionRegistrar.STORE_INTERFACE_PROPERTY, definition.getBeanClassName()); registry.registerBeanDefinition(StoreUtils.getStoreBeanName(definition), builder.getBeanDefinition()); } }
@Override protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationMetadata metadata = new StandardAnnotationMetadata(EnableFilesystemContentAutoConfiguration.class); AnnotationAttributes attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(this.getAnnotation().getName())); String[] basePackages = this.getBasePackages(); Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages); for (BeanDefinition definition : definitions) { String factoryBeanName = StoreUtils.getStoreFactoryBeanName(attributes); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(factoryBeanName); builder.getRawBeanDefinition().setSource(importingClassMetadata); builder.addPropertyValue(AbstractStoreBeanDefinitionRegistrar.STORE_INTERFACE_PROPERTY, definition.getBeanClassName()); registry.registerBeanDefinition(StoreUtils.getStoreBeanName(definition), builder.getBeanDefinition()); } }
@Override protected void registerContentStoreBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationMetadata metadata = new StandardAnnotationMetadata(EnableS3ContentAutoConfiguration.class); AnnotationAttributes attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(this.getAnnotation().getName())); String[] basePackages = this.getBasePackages(); Set<GenericBeanDefinition> definitions = StoreUtils.getStoreCandidates(this.getResourceLoader(), basePackages); for (BeanDefinition definition : definitions) { String factoryBeanName = StoreUtils.getStoreFactoryBeanName(attributes); BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(factoryBeanName); builder.getRawBeanDefinition().setSource(importingClassMetadata); builder.addPropertyValue(AbstractStoreBeanDefinitionRegistrar.STORE_INTERFACE_PROPERTY, definition.getBeanClassName()); registry.registerBeanDefinition(StoreUtils.getStoreBeanName(definition), builder.getBeanDefinition()); } }
/** * @return annotation metadata of superclass, {@code null} if none found or previously processed */ protected AnnotationMetadata doProcessConfigurationClass(ConfigurationClass configClass, AnnotationMetadata metadata) throws IOException { // Recursively process any member (nested) classes first processMemberClasses(metadata); // Process any @PropertySource annotations AnnotationAttributes propertySource = MetadataUtils.attributesFor(metadata, org.springframework.context.annotation.PropertySource.class); if (propertySource != null) { processPropertySource(propertySource); } // Process any @ComponentScan annotations AnnotationAttributes componentScan = MetadataUtils.attributesFor(metadata, ComponentScan.class); if (componentScan != null) { // The config class is annotated with @ComponentScan -> perform the scan immediately Set<BeanDefinitionHolder> scannedBeanDefinitions = this.componentScanParser.parse(componentScan, metadata.getClassName()); // Check the set of scanned definitions for any further config classes and parse recursively if necessary for (BeanDefinitionHolder holder : scannedBeanDefinitions) { if (ConfigurationClassUtils.checkConfigurationClassCandidate(holder.getBeanDefinition(), this.metadataReaderFactory)) { this.parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName()); } } } // Process any @Import annotations Set<Object> imports = new LinkedHashSet<Object>(); Set<String> visited = new LinkedHashSet<String>(); collectImports(metadata, imports, visited); if (!imports.isEmpty()) { processImport(configClass, metadata, imports, true); } // Process any @ImportResource annotations if (metadata.isAnnotated(ImportResource.class.getName())) { AnnotationAttributes importResource = MetadataUtils.attributesFor(metadata, ImportResource.class); String[] resources = importResource.getStringArray("value"); Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader"); for (String resource : resources) { String resolvedResource = this.environment.resolveRequiredPlaceholders(resource); configClass.addImportedResource(resolvedResource, readerClass); } } // Process individual @Bean methods Set<MethodMetadata> beanMethods = metadata.getAnnotatedMethods(Bean.class.getName()); for (MethodMetadata methodMetadata : beanMethods) { configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass)); } // Process superclass, if any if (metadata.hasSuperClass()) { String superclass = metadata.getSuperClassName(); if (!superclass.startsWith("java") && !this.knownSuperclasses.containsKey(superclass)) { this.knownSuperclasses.put(superclass, configClass); // superclass found, return its annotation metadata and recurse if (metadata instanceof StandardAnnotationMetadata) { Class<?> clazz = ((StandardAnnotationMetadata) metadata).getIntrospectedClass(); return new StandardAnnotationMetadata(clazz.getSuperclass(), true); } else { MetadataReader reader = this.metadataReaderFactory.getMetadataReader(superclass); return reader.getAnnotationMetadata(); } } } // No superclass -> processing is complete return null; }