100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Java Spring Boot Basics Cheat Sheet

Java Spring Boot Basics Cheat Sheet

Covers the application entry point, REST controllers, service and repository layers, core annotations, and application.properties configuration.

2 PagesIntermediateMar 25, 2026

Application Entry Point

The bootstrap class that starts a Spring Boot application.

java
@SpringBootApplication // combines @Configuration, @EnableAutoConfiguration, @ComponentScanpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

REST Controller

Map HTTP requests to Java methods and return JSON responses.

java
@RestController@RequestMapping("/api/users")public class UserController {    private final UserService userService; // constructor injection (preferred)    public UserController(UserService userService) {        this.userService = userService;    }    @GetMapping("/{id}")    public ResponseEntity<User> getUser(@PathVariable Long id) {        return userService.findById(id)            .map(ResponseEntity::ok)            .orElse(ResponseEntity.notFound().build());    }    @PostMapping    public ResponseEntity<User> create(@RequestBody @Valid User user) {        User saved = userService.save(user);        return ResponseEntity.status(HttpStatus.CREATED).body(saved);    }}

Service & Repository Layers

Separate business logic from data access using Spring Data JPA.

java
@Servicepublic class UserService {    private final UserRepository repo;    public UserService(UserRepository repo) { this.repo = repo; }    public Optional<User> findById(Long id) { return repo.findById(id); }    public User save(User user) { return repo.save(user); }}@Repositorypublic interface UserRepository extends JpaRepository<User, Long> {    Optional<User> findByEmail(String email); // Spring Data derives the query from the method name}

Core Spring Boot Annotations

The annotations you'll see in nearly every Spring Boot project.

  • @SpringBootApplication- Bootstraps the app; enables component scanning and auto-configuration
  • @RestController- @Controller + @ResponseBody; return values are serialized directly to the response body (usually JSON)
  • @Service- Marks a class as a business-logic bean, picked up by component scanning
  • @Repository- Marks a data-access bean; also translates persistence exceptions into Spring's DataAccessException hierarchy
  • @Component- Generic stereotype for any Spring-managed bean
  • @Autowired- Injects a dependency; constructor injection (no annotation needed on the constructor itself) is the recommended style
  • @Value- Injects a property value from application.properties/.yml: @Value("${server.port}")
  • @Configuration / @Bean- Defines Java-based configuration classes and manually registered beans

application.properties Configuration

Externalize datasource, port, and JPA settings, with profile support.

properties
# application.propertiesserver.port=8081spring.datasource.url=jdbc:postgresql://localhost:5432/appdbspring.datasource.username=postgresspring.datasource.password=secretspring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=true# Environment-specific: application-prod.properties, activated with# spring.profiles.active=prod
Pro Tip

Prefer constructor injection over field injection (@Autowired on a field) - it makes dependencies explicit, allows fields to be final, and makes the class trivially testable without needing a Spring context to instantiate mocks.

Was this cheat sheet helpful?

Explore Topics

#JavaSpringBootBasics#JavaSpringBootBasicsCheatSheet#Programming#Intermediate#ApplicationEntryPoint#RESTController#ServiceRepositoryLayers#CoreSpringBootAnnotations#APIs#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet