Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom RepositoryRestConfigurer not initialized when lazy-initialization is set to true #2238

Open
ilseva opened this issue Mar 8, 2023 · 4 comments
Assignees
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@ilseva
Copy link

ilseva commented Mar 8, 2023

Hi!
We need to register custom converters from our repositories exposed as RepositoryRestResource.
We implement a class, annotated with @Configuration, that extends the RepositoryRestConfigurer and we implement the configureConversionService method.
When the application starts with spring.main.lazy-initialization at true the custom converters are not loaded.

Spring Boot version: 2.7.7

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 8, 2023
@mp911de mp911de self-assigned this Mar 14, 2023
@odrotbohm odrotbohm added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels May 3, 2023
@odrotbohm
Copy link
Member

Thanks for bringing this to our attention. Any chance you can provide a minimal reproducer (ideally Java, Maven, as few as possible, additional dependencies)?

@spring-projects-issues
Copy link

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label May 10, 2023
@spring-projects-issues
Copy link

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2023
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels May 17, 2023
@Guschtel
Copy link

Guschtel commented Apr 19, 2024

I can confirm this. It also happened to us on Spring Boot 3.2.4 with

spring:
  main:
    lazy-initialization: true

The workaround ist to not load the relevant @Configurations lazily by adding @Lazy(false). I'm not sure if this is expected behaviour or not.

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(RepositoryRestProperties.class)
@Lazy(value = false)
public class OurRepositoryRestMvcConfiguration implements RepositoryRestConfigurer {

  private final BeanFactory beanFactory;
  private final Jackson2ObjectMapperBuilder objectMapperBuilder;
  private final RepositoryRestProperties properties;

  OurRepositoryRestMvcConfiguration(
    BeanFactory beanFactory,
    Jackson2ObjectMapperBuilder objectMapperBuilder,
    RepositoryRestProperties properties
  ) {
    this.beanFactory = beanFactory;
    this.objectMapperBuilder = objectMapperBuilder;
    this.properties = properties;
  }

  @Override
  public void configureConversionService(ConfigurableConversionService conversionService) {
    final var annotatedTypeScanner = new AnnotatedTypeScanner(Component.class);
    Set<Class<?>> types = annotatedTypeScanner.findTypes(
      "com.example.ourpackage.for.converters"
    );
    types.forEach(type -> {
      try {
        Object bean = beanFactory.getBean(Class.forName(type.getName()));
        if (bean instanceof Converter) {
          conversionService.addConverter((Converter<?, ?>) bean);
        }
      } catch (ClassNotFoundException e) {
         // log error
      }
    });
  }

  @Override
  public void configureRepositoryRestConfiguration(
    RepositoryRestConfiguration config,
    CorsRegistry cors
  ) {
    this.properties.applyTo(config);
  }

  @Override
  public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
    this.objectMapperBuilder.configure(objectMapper);
  }
}

@mp911de mp911de assigned odrotbohm and unassigned mp911de Apr 19, 2024
@mp911de mp911de reopened this Apr 19, 2024
@mp911de mp911de added the status: waiting-for-triage An issue we've not yet triaged label Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

5 participants