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.
- Creating a service — a class with business logic, marked with the
@Serviceannotation:
@Service
public class MyService {
public String greet() {
return "Hello, Spring!";
}
}
- 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();
}
}
- Running the application — with a class annotated with
@SpringBootApplicationand amainmethod:
@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.