DEV Community

MAYUR KUMBHAR
MAYUR KUMBHAR

Posted on

Wiring in Spring

Context

Bean is most fundamental unit in spring framework which we have seen in previous posts. Bean creation may include creating the normal object which dose not have any dependency on other objects they solely can be created from the primitive data types.

But most of the objects we create they depends on other objects either developer created or library objects.

Now with bean we give control to spring framework how to create and manage the java objects in application. When there is dependency between the Objects, spring framework should understand and construct the dependent objects (beans) as well.

Wiring

So to enable the framework to create dependent beans objects and inject them on runtime whenever required this concept is called as "wiring".
Here spring framework process the connection between the objects & make sure required objects are created and injected.

Ways to wire a bean

Spring provides multiple ways to wire a bean, by dependency injection, annotation based wiring, xml-based wiring, java-based configuration wiring, component scanning.

Springboot provides the @Autowired annotation to automatically wire the dependent objects.

Similarly java has @Inject annotation which more general dose can be used without spring, to wire the dependent objects.

The @Inject annotation is part of the Java Dependency Injection (JSR-330) standard, which is supported by frameworks like Spring

Ways to use @Autowired annotation

Annotation @Autowired used with multiple ways via field injection, constructor injection, setter injection, optional dependency & with @Qualifier annotation.

Field injection

To inject the dependent object in bean, we can put @Autowired annotation on the class fields, which then framework detects and injects the appropriate dependency.


@Autowired 
private ExampleBean someBean;

Enter fullscreen mode Exit fullscreen mode

Constructor injection

While creating the bean, we can provide the constructor param as @Autowired to inject the required object. Spring will automatically pass the require Object as param in constructor.


@Autowired
public ExampleBean(DependentBean dependentBean){
   this.dependentBean = dependentBean;
}

Enter fullscreen mode Exit fullscreen mode

Setter injection

Another way to inject the dependency is to use the setter method annotated with @Autowired, So spring can use it to inject the required dependency.


@Autowired 
public void setDependentBean(DependeentBean bean){
   this.dependentBean = bean;
}

Enter fullscreen mode Exit fullscreen mode

Optional Dependency injection

Sometimes the dependency on object is optional, as with some objects can be created without depending on all the objects. This can be achieved using the Optional and required=false in the @Autowired.

By default the @Autowired is not optional & always injects the dependency.


@Autowired(required = false)
private Optional<DependentBean> bean;

Enter fullscreen mode Exit fullscreen mode

Use of Qualifiers with Autowiring

Sometime we have multiple beans in our context, now if we only specify the @Autowired on the field, constructor, setter Spring frameworks get confused on which exact dependency to inject as there are multiple objects of same type are available in context.

To remove ambiguity and provide exact dependent object to inject we use @Qualifier with value to specify the which exact bean to use for this dependency injection.


@Autowired 
@Qualifier(value = "someSpecificBean")
private DependentBean bean;

Enter fullscreen mode Exit fullscreen mode

@Inject can also be used similar to @Autowired annotation, with slight difference.


Top comments (0)