Java SpringBoot

Java [Java ] Java


SpringBoot Spring Spring

SpringBoot "" Spring

SpringBoot

  1. jar Spring
  2. TomcatJetty Undertow WAR
  3. starter Maven/Gradle
  4. XML

SpringBoot

1. SpringBoot Starter

SpringBoot Starter

  • spring-boot-starter-web web
  • spring-boot-starter-data-jpa JPA
  • spring-boot-starter-test

2. SpringBoot AutoConfiguration

SpringBoot classpath jar classpath H2 SpringBoot

3. SpringBoot Actuator

  • HTTP

SpringBoot

1. Spring Initializr

start.spring.io

  • Maven
  • Java
  • SpringBoot
  • "Web"

2.

SpringBoot

src/
  main/
    java/
      com/example/demo/
        DemoApplication.java   # 
    resources/
      application.properties  # 
  test/
    java/
      com/example/demo/
        DemoApplicationTests.java # 

3. REST

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, SpringBoot!";
    }
}

4.

DemoApplication main 8080

http://localhost:8080/api/hello "Hello, SpringBoot!"


SpringBoot

1.

SpringBoot

  • application.properties
  • application.yml

application.properties

server.port=9090
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret

2.

  • application-dev.properties
  • application-prod.properties

spring.profiles.active

spring.profiles.active=dev

SpringBoot

1. Spring Data JPA

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String email;
    // getters and setters
}

Repository

public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByName(String name);
}

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
   
    public List<User> getUsersByName(String name) {
        return userRepository.findByName(name);
    }
}

SpringBoot

  1. Controller-Service-Repository
  2. @ControllerAdvice
  3. SLF4J
  4. API Swagger API

@ControllerAdvice
public class GlobalExceptionHandler {
   
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleResourceNotFound(ResourceNotFoundException ex) {
        ErrorResponse error = new ErrorResponse(
            "NOT_FOUND",
            ex.getMessage()
        );
        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }
}

SpringBoot

  1. Spring Security
  2. Spring Cloud
  3. Spring Batch
  4. WebFlux
  5. Docker

SpringBoot Java Java

Java [Java ] Java