private ApplicationContext createContext(ScopedProxyMode scopedProxyMode) { GenericWebApplicationContext context = new GenericWebApplicationContext(); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context); scanner.setIncludeAnnotationConfig(false); scanner.setBeanNameGenerator(new BeanNameGenerator() { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return definition.getScope(); } }); scanner.setScopedProxyMode(scopedProxyMode); // Scan twice in order to find errors in the bean definition compatibility check. scanner.scan(getClass().getPackage().getName()); scanner.scan(getClass().getPackage().getName()); context.refresh(); return context; }
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { if (registry.containsBeanDefinition("restServer")) { BeanDefinition beanDefinition = registry.getBeanDefinition("restServer"); ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); scanner.addIncludeFilter(new AnnotationTypeFilter(Path.class)); BeanNameGenerator nameGenerator = new AnnotationBeanNameGenerator(); List<RuntimeBeanReference> beanNames = new ManagedList<>(); for (BeanDefinition definition : scanner.findCandidateComponents("net.techreadiness")) { String beanName = nameGenerator.generateBeanName(definition, registry); beanNames.add(new RuntimeBeanReference(beanName)); } beanNames.addAll((Collection<? extends RuntimeBeanReference>) beanDefinition.getPropertyValues() .getPropertyValue("serviceBeans").getValue()); beanDefinition.getPropertyValues().add("serviceBeans", beanNames); } }
/** * Create a new {@link ConfigurationClassParser} instance that will be used * to populate the set of configuration classes. */ public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory, ProblemReporter problemReporter, Environment environment, ResourceLoader resourceLoader, BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry) { this.metadataReaderFactory = metadataReaderFactory; this.problemReporter = problemReporter; this.environment = environment; this.resourceLoader = resourceLoader; this.registry = registry; this.componentScanParser = new ComponentScanAnnotationParser( resourceLoader, environment, componentScanBeanNameGenerator, registry); this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader); }
public ComponentScanAnnotationParser(ResourceLoader resourceLoader, Environment environment, BeanNameGenerator beanNameGenerator, BeanDefinitionRegistry registry) { this.resourceLoader = resourceLoader; this.environment = environment; this.beanNameGenerator = beanNameGenerator; this.registry = registry; }
protected void parseBeanNameGenerator(Element element, ClassPathBeanDefinitionScanner scanner) { if (element.hasAttribute(NAME_GENERATOR_ATTRIBUTE)) { BeanNameGenerator beanNameGenerator = (BeanNameGenerator) instantiateUserDefinedStrategy( element.getAttribute(NAME_GENERATOR_ATTRIBUTE), BeanNameGenerator.class, scanner.getResourceLoader().getClassLoader()); scanner.setBeanNameGenerator(beanNameGenerator); } }
/** * Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used * to populate the given {@link BeanDefinitionRegistry}. */ public ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor, ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory, ResourceLoader resourceLoader, Environment environment, BeanNameGenerator importBeanNameGenerator) { this.registry = registry; this.sourceExtractor = sourceExtractor; this.problemReporter = problemReporter; this.metadataReaderFactory = metadataReaderFactory; this.resourceLoader = resourceLoader; this.environment = environment; this.importBeanNameGenerator = importBeanNameGenerator; this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader); }
/** * Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used * to populate the given {@link BeanDefinitionRegistry}. */ ConfigurationClassBeanDefinitionReader(BeanDefinitionRegistry registry, SourceExtractor sourceExtractor, ResourceLoader resourceLoader, Environment environment, BeanNameGenerator importBeanNameGenerator, ImportRegistry importRegistry) { this.registry = registry; this.sourceExtractor = sourceExtractor; this.resourceLoader = resourceLoader; this.environment = environment; this.importBeanNameGenerator = importBeanNameGenerator; this.importRegistry = importRegistry; this.conditionEvaluator = new ConditionEvaluator(registry, environment, resourceLoader); }
@Test public void customBeanNameGenerator() throws Exception { TestSpringApplication application = new TestSpringApplication( ExampleWebConfig.class); BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); application.setBeanNameGenerator(beanNameGenerator); this.context = application.run(); verify(application.getLoader()).setBeanNameGenerator(beanNameGenerator); Object actualGenerator = this.context .getBean(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR); assertThat(actualGenerator).isSameAs(beanNameGenerator); }
@Test public void customBeanNameGeneratorWithNonWebApplication() throws Exception { TestSpringApplication application = new TestSpringApplication( ExampleWebConfig.class); application.setWebEnvironment(false); BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); application.setBeanNameGenerator(beanNameGenerator); this.context = application.run(); verify(application.getLoader()).setBeanNameGenerator(beanNameGenerator); Object actualGenerator = this.context .getBean(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR); assertThat(actualGenerator).isSameAs(beanNameGenerator); }
@Test public void customBeanNameGenerator() throws Exception { TestSpringApplication application = new TestSpringApplication( ExampleWebConfig.class); BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); application.setBeanNameGenerator(beanNameGenerator); this.context = application.run(); verify(application.getLoader()).setBeanNameGenerator(beanNameGenerator); Object bean = this.context .getBean(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR); assertThat(bean).isSameAs(beanNameGenerator); }
/** * Create a new {@link ConfigurationClassParser} instance that will be used * to populate the set of configuration classes. */ public ConfigurationClassParser(MetadataReaderFactory metadataReaderFactory, ProblemReporter problemReporter, Environment environment, ResourceLoader resourceLoader, BeanNameGenerator componentScanBeanNameGenerator, BeanDefinitionRegistry registry) { this.metadataReaderFactory = metadataReaderFactory; this.problemReporter = problemReporter; this.environment = environment; this.resourceLoader = resourceLoader; this.registry = registry; this.componentScanParser = new ComponentScanAnnotationParser( resourceLoader, environment, componentScanBeanNameGenerator, registry); }
@Test public void customBeanNameGenerator() throws Exception { TestSpringApplication application = new TestSpringApplication( ExampleWebConfig.class); BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); application.setBeanNameGenerator(beanNameGenerator); this.context = application.run(); verify(application.getLoader()).setBeanNameGenerator(beanNameGenerator); assertThat( this.context .getBean(AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR), sameInstance((Object) beanNameGenerator)); }
/** * Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used * to populate the given {@link BeanDefinitionRegistry}. */ public ConfigurationClassBeanDefinitionReader( BeanDefinitionRegistry registry, SourceExtractor sourceExtractor, ProblemReporter problemReporter, MetadataReaderFactory metadataReaderFactory, ResourceLoader resourceLoader, Environment environment, BeanNameGenerator importBeanNameGenerator) { this.registry = registry; this.sourceExtractor = sourceExtractor; this.problemReporter = problemReporter; this.metadataReaderFactory = metadataReaderFactory; this.resourceLoader = resourceLoader; this.environment = environment; this.importBeanNameGenerator = importBeanNameGenerator; }
/** * Create a new {@link ConfigurationClassBeanDefinitionReader} instance that will be used to populate the given * {@link BeanDefinitionRegistry}. */ public ConfigurationClassBeanDefinitionReader(final BeanDefinitionRegistry registry, final SourceExtractor sourceExtractor, final ProblemReporter problemReporter, final MetadataReaderFactory metadataReaderFactory, final ResourceLoader resourceLoader, final Environment environment, final BeanNameGenerator importBeanNameGenerator) { this.registry = registry; this.sourceExtractor = sourceExtractor; this.problemReporter = problemReporter; this.metadataReaderFactory = metadataReaderFactory; this.resourceLoader = resourceLoader; this.environment = environment; this.importBeanNameGenerator = importBeanNameGenerator; }
private void initialize(String defaultProfile, boolean autoScan) { ServerConfiguration serverConfiguration = mainClass.getAnnotation(ServerConfiguration.class); if (serverConfiguration != null && serverConfiguration.beanNameGenerator() != BeanNameGenerator.class) { try { applicationContext.setBeanNameGenerator(serverConfiguration.beanNameGenerator().newInstance()); } catch (Exception ignored) { } } applicationContext.register(mainClass); if (autoScan) { applicationContext.scan(mainClass.getPackage().getName()); } Set<String> activeProfiles = Sets.newLinkedHashSet(); String profileProperty = System.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, defaultProfile); if (StringUtils.isNotBlank(profileProperty)) { applicationContext.getEnvironment().setActiveProfiles(StringUtils.split(profileProperty, ",")); Collections.addAll(activeProfiles, StringUtils.split(profileProperty, ",")); logger.warn("activeProfiles are {}", activeProfiles); } Set<Resource> resources = getPropertiesResources(serverConfiguration == null ? null : serverConfiguration.properties()); ServerPropertiesConfigurer serverPropertiesConfigurer = new ServerPropertiesConfigurer(activeProfiles, resources); serverPropertiesConfigurer.setIgnoreUnresolvablePlaceholders(true); applicationContext.addBeanFactoryPostProcessor(serverPropertiesConfigurer); this.properties = ServerPropertiesConfigurer.mergeProperties(activeProfiles, resources); applicationContext.setEnvironment(new StandardEnvironment() { @Override protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addFirst(new PropertiesPropertySource("applicationProperties", properties)); super.customizePropertySources(propertySources); } }); for (ServerProcessor serverProcessor : ServerProcessor.values()) { serverProcessor.register(mainClass, applicationContext, properties); } if (defaultProfile != null && !activeProfiles.contains(defaultProfile)) { pidFileWriter = new PidFileWriter(new File(System.getProperty("app.home", "."), "logs/pid")); } applicationContext.refresh(); }
/** * Set the bean name generator to be used by the underlying readers and scanner. * @param beanNameGenerator the bean name generator */ public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) { this.annotatedReader.setBeanNameGenerator(beanNameGenerator); this.xmlReader.setBeanNameGenerator(beanNameGenerator); this.scanner.setBeanNameGenerator(beanNameGenerator); }
public BeanNameGenerator getNameGenerator() { return nameGenerator; }
public void setNameGenerator(BeanNameGenerator nameGenerator) { this.nameGenerator = nameGenerator; }
/** * Set the {@link BeanNameGenerator} to be used when triggering component scanning * from {@link Configuration} classes and when registering {@link Import}'ed * configuration classes. The default is a standard {@link AnnotationBeanNameGenerator} * for scanned components (compatible with the default in {@link ClassPathBeanDefinitionScanner}) * and a variant thereof for imported configuration classes (using unique fully-qualified * class names instead of standard component overriding). * <p>Note that this strategy does <em>not</em> apply to {@link Bean} methods. * <p>This setter is typically only appropriate when configuring the post-processor as * a standalone bean definition in XML, e.g. not using the dedicated * {@code AnnotationConfig*} application contexts or the {@code * <context:annotation-config>} element. Any bean name generator specified against * the application context will take precedence over any value set here. * @since 3.1.1 * @see AnnotationConfigApplicationContext#setBeanNameGenerator(BeanNameGenerator) * @see AnnotationConfigUtils#CONFIGURATION_BEAN_NAME_GENERATOR */ public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) { Assert.notNull(beanNameGenerator, "BeanNameGenerator must not be null"); this.localBeanNameGeneratorSet = true; this.componentScanBeanNameGenerator = beanNameGenerator; this.importBeanNameGenerator = beanNameGenerator; }
/** * Provide a custom {@link BeanNameGenerator} for use with {@link AnnotatedBeanDefinitionReader} * and/or {@link ClassPathBeanDefinitionScanner}, if any. * <p>Default is {@link org.springframework.context.annotation.AnnotationBeanNameGenerator}. * <p>Any call to this method must occur prior to calls to {@link #register(Class...)} * and/or {@link #scan(String...)}. * @see AnnotatedBeanDefinitionReader#setBeanNameGenerator * @see ClassPathBeanDefinitionScanner#setBeanNameGenerator */ public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) { this.reader.setBeanNameGenerator(beanNameGenerator); this.scanner.setBeanNameGenerator(beanNameGenerator); getBeanFactory().registerSingleton( AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR, beanNameGenerator); }