learn/framework

Spring

사겅이 2023. 9. 22. 04:54

출처 : https://dev.to/urunov/spring-framework-architecture-and-runtime-components-31id

 

Java 기반의 오픈 소스 애플리케이션 프레임워크로, 엔터프라이즈급 애플리케이션을 빌드하기 위한 다양한 모듈과 기능을 제공

 

IoC 컨테이너 (Inversion of Control Container)

출처 : https://velog.io/@wickedev/IoC-DIP-IoC-Container-DI-DI-Framework-%EB%8F%84%EB%8C%80%EC%B2%B4-%EA%B7%B8%EA%B2%8C-%EB%AD%94%EB%8D%B0

  • IoC 컨테이너는 객체의 생성, 의존성 주입 및 라이프사이클 관리를 담당
  • @Component, @Service, @Repository 등의 어노테이션을 사용하여 클래스를 Bean으로 등록하고, 의존성 주입(Dependency Injection)을 통해 객체 간의 관계를 설정
@Component
public class MyService {
    private final MyRepository repository;

    @Autowired
    public MyService(MyRepository repository) {
        this.repository = repository;
    }

    // ...
}

 

AOP (Aspect-Oriented Programming)

출처 : https://programforlife.tistory.com/103

  • AOP는 공통 관심 사항(Cross-cutting Concerns)을 분리하여 재사용 가능한 모듈로 만듬
  • @Aspect 어노테이션과 포인트컷(Pointcut), 어드바이스(Advice) 등을 사용하여 횡단 관심사를 정의하고 적용
@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.service.*.*(..))")
    public void beforeMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Entering method: " + methodName);
    }

    // ...
}

 

Spring MVC (Model-View-Controller)

출처 : https://kotlinworld.com/161

  • Spring MVC는 웹 애플리케이션 개발을 위한 모듈로, 요청과 응답 처리를 담당
  • @Controller, @RequestMapping, @GetMapping 등의 어노테이션을 사용하여 컨트롤러와 URL 매핑을 정의하고, 데이터 바인딩 및 뷰 렌더링을 지원
@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public String getUser(@PathVariable("id") Long id, Model model) {
        User user = userService.getUserById(id);
        model.addAttribute("user", user);
        return "user-details";
    }

    // ...
}

 

데이터 액세스 (Data Access)

출처 : https://velog.io/@koseungbin/Spring-JDBC

  • Spring은 JDBC, JPA, MyBatis 등 다양한 데이터 액세스 기술과 통합
  • JdbcTemplate, EntityManager, CrudRepository 등의 인터페이스와 구현체를 제공하여 데이터베이스와의 상호작용을 간소화
@Repository
public class UserRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public User getUserById(Long id) {
        String sql = "SELECT * FROM users WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new UserRowMapper());
    }

    // ...
}

 

트랜잭션 관리 (Transaction Management)

출처 : https://www.javatpoint.com/hibernate-transaction-management-example

  • Spring은 선언적 트랜잭션 관리를 지원하여 데이터베이스 트랜잭션을 간편하게 처리.
  • @Transactional 어노테이션을 사용하여 메서드 또는 클래스에 트랜잭션 범위를 설정
@Service
@Transactional
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public User getUserById(Long id) {
        return userRepository.getUserById(id);
    }

    // ...
}

'learn > framework' 카테고리의 다른 글

ORM (Object-Relational Mapping)  (0) 2023.09.22
Struts  (0) 2023.09.22