Какая польза от аннотации @Autowired в Spring Boot?JAVA

Программисты JAVA общаются здесь
Anonymous
Какая польза от аннотации @Autowired в Spring Boot?

Сообщение Anonymous »

Я новичок в Spring Boot. Я создал демонстрационный REST API в Spring Boot. В следующем коде, даже если удалить аннотацию @Autowired, контроллер все равно будет работать, и я смогу вызвать API.

Код: Выделить всё

import com.abhishek.demo2.entity.Question;
import com.abhishek.demo2.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController
{
QuestionService questionService;

@Autowired
public QuestionController(QuestionService questionService)
{
this.questionService = questionService;
}

@GetMapping("/all")
public List getAllQuestions()
{
return questionService.getAllQuestions();
}
}
Итак, теперь мне интересно, для чего нужна эта аннотация @Autowired и как она работает.
Спасибо.

Подробнее здесь: https://stackoverflow.com/questions/776 ... pring-boot

Вернуться в «JAVA»