0

Using Spring, i have a class 'BaseController' with BaseService injected using autowiring. How the autowiring working even if the class is not inititalized. Check code below

@Controller
class BaseController{
    @Autowired
    private BaseService baseService;
    //......
}

and bean definition in xml as

<context:annotation-config />
<bean name="baseService" class="com.test.service.BaseService" />

I am not initializing baseService either with Setter/Constructor. How does it works, when i call a method as show below

baseService.methodOne();
Giridhar
  • 512
  • 5
  • 20

2 Answers2

2

Spring framework relies on Spring IoC (Inversion of Control) Container to create all the components and initialize them by injecting their dependencies. The dependencies are injected though constructors, setters and/or fields by using the reflection API.

Here you annotated the field baseService with the annotation Autowired which will indicate the Spring IoC Container to inject a bean of type BaseService, if at the time the container needs to create your Controller, the dependency has not been created and initialized, it will do it automatically and if this bean has dependencies it will apply the same logic on the dependencies until the dependency tree has been fully created and initialized. This is how it works in a nutshell.

If we have two controller classes A & B, with dependency on BaseService. Does Spring container create two objects and injects into A and B separately or Only one instance of BaseService is shared among all the classes that has dependency.

It depends on the scope that you set on your bean declaration, if the scope is singleton, the container will create only one instance of your bean and then will inject the same instance in your controllers. If you chose prototype for example, it will create a new instance of your bean for each of your controllers. In your case knowing that singleton is the default scope, it will inject the same instance. For more details about the supported scopes in Spring IoC Container, you can read this.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • Thanks @Nicolas Filotto Adding to that question, If we have two controller classes A & B, with dependency on BaseService. Does Spring container create two objects and injects into A and B separately or Only one instance of BaseService is shared among all the classes that has dependency. – Giridhar Aug 18 '16 at 02:02
  • response updated, please check – Nicolas Filotto Aug 18 '16 at 07:57
1

If you have enabled the class scanning, spring detects all the spring bean types at the startup and inject dependencies if it marked with @Autowired, @Resource, etc annotations.

According to your example, BaseController must be a type of spring bean and also the BaseService. If the BaseService is an interface there must be an impl. if there are many impls, then you need a @Qualifier annotation as well.

Spring uses reflection, so you do not need a setter or a constructor to inject the dependency.

By default all the beans are singleton unless you set scope via @Scope

sura2k
  • 7,365
  • 13
  • 61
  • 80