-2

When we use @Autowire annotation in spring to crete the object is it mandatory to specify the bean in configuration class ? And how does @Autowired work in springboot?

saiteja
  • 1
  • 1
  • 1
    The object that you are autowiring must be a Spring bean, which means it must be defined in a configuration class using a `@Bean` method, or in Spring XML configuration or have been registered as a Spring bean some other way. `@Autowired` in Spring Boot works exactly the same as in regular Spring. – Jesper Oct 17 '19 at 11:35

1 Answers1

0

It's not mandatory in many cases, for instance when injecting objects annotated as @Entity, @Repository, @Controller, @Service or @Component (superclass of all the others) declared
within the main package where the @SpringBootApplication is placed.

You can always place components outside the main package if you declare a component scan in additional packages, using @ComponentScan({"package_1",..."package_n"}) in your @SpringBootApplication class. Remember to include the main package in the list.

daniCRo
  • 1
  • 2