Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to @Autowire services in SpringBoot

Good day, guys. I have a question about autowiring services into my classes when using Springboot. All of the examples I have seen on the Internet as well as in the Springboot specification do something of the like (taking an excerpt from the Springboot version 1.5.7 specification):

package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;

@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}

This is a class that injects a property through its constructor, by means of @Autowiring the constructor. Another form is to @Autowire the property like this:

@Autowired
private final RiskAssessor riskAssessor

But, where I work, for these two methods to work, I have been told that I need to use this method:

 applicationContext.getAutowireCapableBeanFactory().autowireBean(Object.class)

They have told me that I need this in order for the @Autowired annotation to work.

Now my question to you is: why is there no simple annotation that allows the @Autowire to function correctly? (Something like @AutowiredClass). The above method is too verbose and hard to remember, so surely there must be a better way to make @Autowired work on classes in order to inject services, just like we do in Grails where we just say def someService and it is automatically injected.

like image 390
Greg Avatar asked Sep 18 '17 20:09

Greg


People also ask

Can we use @autowired in service class?

Spring @Autowired Annotation - Service ClassWe will use the same service class for perform spring autowiring byName, byType and by constructor. The setter method will be used for spring autowiring byName and byType whereas constructor based injection will be used by constructor autowire attribute.

What is @autowire in Spring boot?

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.

Can we use @autowired in Spring?

In Spring, you can use @Autowired annotation to auto-wire bean on the setter method, constructor , or a field . Moreover, it can autowire the property in a particular bean. We must first enable the annotation using below configuration in the configuration file. We have enabled annotation injection.

What is @autowired and @inject?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application.


1 Answers

If you want properly use @Autowired in your spring-boot application, you must do next steps:

  1. Add @SpringBootApplicationto your main class
  2. Add @Service or @Component annotation to class you want inject
  3. Use one of two ways that you describe in question, to autowire
like image 51
Pavlo Pastushok Avatar answered Sep 24 '22 02:09

Pavlo Pastushok