io.micronaut.core.naming.NameResolver Java Examples

The following examples show how to use io.micronaut.core.naming.NameResolver. 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: DataSourceMigrationRunner.java    From micronaut-flyway with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource onCreated(BeanCreatedEvent<DataSource> event) {
    DataSource dataSource = dataSourceResolver.resolve(event.getBean());

    if (event.getBeanDefinition() instanceof NameResolver) {
        ((NameResolver) event.getBeanDefinition())
                .resolveName()
                .ifPresent(name -> {
                    applicationContext
                            .findBean(FlywayConfigurationProperties.class, Qualifiers.byName(name))
                            .ifPresent(flywayConfig -> run(flywayConfig, dataSource));
                });
    }

    return dataSource;
}
 
Example #2
Source File: MicronautBeanFactory.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
private String computeBeanName(BeanDefinition<?> definition) {
    String name;
    if (definition instanceof NameResolver) {
        name = ((NameResolver) definition).resolveName().orElse(Primary.class.getSimpleName());
    } else {
        name = definition.getValue(Named.class, String.class).orElseGet(() ->
                definition.getAnnotationTypeByStereotype(Qualifier.class).map(Class::getSimpleName).orElse(definition.getClass().getSimpleName())
        );
    }
    return definition.getBeanType().getName() + "(" + name + ")";
}
 
Example #3
Source File: EntitiesInPackageCondition.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(ConditionContext context) {
    final AnnotationMetadataProvider component = context.getComponent();
    if (component instanceof BeanDefinition) {
        BeanDefinition<?> definition = (BeanDefinition<?>) component;
        final BeanContext beanContext = context.getBeanContext();
        if (beanContext instanceof ApplicationContext) {

            final Optional<String> name = definition instanceof NameResolver ? ((NameResolver) definition).resolveName() : Optional.empty();
            final Qualifier<JpaConfiguration> q = Qualifiers.byName(name.orElse("default"));
            final Optional<JpaConfiguration> jpaConfiguration = beanContext.findBean(JpaConfiguration.class, q);
            JpaConfiguration.EntityScanConfiguration entityScanConfig = jpaConfiguration.map(JpaConfiguration::getEntityScanConfiguration).orElse(null);

            if (entityScanConfig == null) {
                return false;
            }

            boolean isClasspathScanEnabled = entityScanConfig.isClasspath();
            String[] packagesToScan = entityScanConfig.getPackages();
            boolean hasEntitiesOnClassPath = false;
            boolean hasIntrospections = false;
            if (isClasspathScanEnabled) {
                final Environment environment = ((ApplicationContext) beanContext).getEnvironment();
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasEntitiesOnClassPath = environment.scan(Entity.class, packagesToScan).findAny().isPresent();
                } else {
                    hasEntitiesOnClassPath = environment.scan(Entity.class).findAny().isPresent();
                }
            } else {
                if (ArrayUtils.isNotEmpty(packagesToScan)) {
                    hasIntrospections = !BeanIntrospector.SHARED
                            .findIntrospections(Entity.class, packagesToScan).isEmpty();
                } else {
                    hasIntrospections = !BeanIntrospector.SHARED
                            .findIntrospections(Entity.class).isEmpty();
                }
            }

            return hasEntitiesOnClassPath || hasIntrospections;
        }
    }
    return true;
}