1

I have a class (say Class A) that @Autowire a properties class (Class B). B's fields are read from application.yml and then a bean is created using @Component. This bean can then be used in class A using @Autowire.

When I run the application in the same module, all the beans are created fine and the application runs. But when I do a @Import(A.class) from a @Configuration class of another module (it's a multi-module project), the bean created for Class B contains all null values. This means that it is not reading the application.yml in it's module.

What can be the reason behind such behaviour?

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44
  • `@Import` is supposed for(and from) `@Configuration` objects. (from my knowledge..it's an equivalent for ``...in xml configurations) ... so try rather to `@Import` the configuration/context which is aware of "the bean". – xerx593 Jan 12 '19 at 15:30
  • ...so, the *value* of `@Import` should also be a `@Configuration` ..not a `@Bean`..or `@Component`. https://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch04s03.html – xerx593 Jan 12 '19 at 15:33
  • 1
    Oh it solved it, thanks!! – Prashant Pandey Jan 13 '19 at 04:16

1 Answers1

1

I am not sure of the problem because you haven't shared code snippets or project structure etc. But have you tried scanning your packages with

@CoomponentScan(basepackages = {"package1","package2"}) 

above your application class. This way, spring will know where to look for all the classes.

Hope this helps. Please add code snippets, project structure etc. so that we can understand the problem and help.

If that's not the case, refer this, it might be the problem.

Why is my Spring @Autowired field null?

mkkr1009
  • 91
  • 3