Sobes.tech
Junior — Middle

How is the service configured and launched within the Spring Framework?

sobes.tech AI

Answer from AI

In Spring Framework, setting up and running a service is usually done using annotations and configuration classes.

  1. Creating a service — a class with business logic, marked with the @Service annotation:
@Service
public class MyService {
    public String greet() {
        return "Hello, Spring!";
    }
}
  1. Injecting the service — into a controller or another component via @Autowired:
@RestController
public class MyController {
    private final MyService myService;

    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
    }

    @GetMapping("/greet")
    public String greet() {
        return myService.greet();
    }
}
  1. Running the application — with a class annotated with @SpringBootApplication and a main method:
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Thus, Spring automatically scans components, creates beans, and manages their lifecycle, making it easy to configure and run services.