1

RepositoryRestMvcConfiguration class of Spring Data REST has method enumTranslator():

@Bean
public EnumTranslator enumTranslator() {
  return new EnumTranslator(resourceDescriptionMessageSourceAccessor());
}

that (I expect) registers an 'enumTranslator' Bean.

But when I try to use it in my component then it isn't injected and always is null:

@Component
public class MyComponent {

    private final EnumTranslator enumTranslator; 

    public MyComponent(EnumTranslator enumTranslator) {
        this.enumTranslator = enumTranslator;
    }

    public void someMethod() {             
        // enumTranslator -> null
    }
}

Could somebody explain why does this happens and how to correct inject such beans?

UPDATE

I've figured out - I tried to implement a class (Spring converter) that isn't managed by Spring ))

Thanks everybody who tried to help me!

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • What you describe should work, so there's probably something missing from your description. Are you sure that it's Spring that's instantiating the `MyComponent`? – slim Nov 13 '17 at 17:17
  • (Run it in a debugger, put a breakpoint in the constructor, and look at the stack to see what's calling the `MyComponent(EnumTranslator)` constructor) – slim Nov 13 '17 at 17:19
  • Thanks @slim ! I've found the reason - check my update.. ) – Cepr0 Nov 14 '17 at 09:29

1 Answers1

1

It looks like the RepositoryRestMvcConfiguration bean was not loaded in the Spring container.
Try for example to import it with @Import(RepositoryRestMvcConfiguration.class) from a Configuration class.

According to the documentation :

2.4 Configuring Spring Data REST

...

You can either import this class into your existing configuration using an @Import annotation or you can subclass it and override any of the configureXXX methods to add your own configuration to that of Spring Data REST.

Community
  • 1
  • 1
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Since Spring 4.3, if there's only one constructor, `@Autowired` is implicit and not required. – slim Nov 13 '17 at 17:15
  • Right. Thanks. I kept the habit to specify it anyway. I edited. – davidxxx Nov 13 '17 at 17:30
  • I don't refer to autowiring it but to importing the bean. You can check it by displaying all loaded Spring beans. https://stackoverflow.com/questions/9602664/print-all-the-spring-beans-that-are-loaded – davidxxx Nov 13 '17 at 17:34
  • Thanks @davidxxx ! I've found the reason - check my update ) – Cepr0 Nov 14 '17 at 09:29