[44일 차] 스프링 프레임워크 (Spring DI/AOP)

2025. 3. 31. 11:25·LG 유플러스 유레카/스프링 프레임워크

25/03/31 (월)

큰일났습니다.

오늘 진짜진짜진짜 너무 피곤해서 쉬는 시간마다 틈틈이 자려고합니다.

으아아아아아


 

강의

강의 목차

  • Spring DI
    • XML
    • Annotation
    • Java Configuration
  • Spring AOP
    • Logging

 

Spring DI (의존성 주입)

- Spring Dependency Injection

  • 객체를 직접 생성하지 않고ㅡ 외부에서 주입하는 개념
  • 객체 간 결합도를 낮춰 유지보수성과 테스트 용이성을 높임

 

- XML 기반 DI

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="person" class="com.example.Person">
        <property name="name" value="홍길동"/>
    </bean>

</beans>
-----
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person.getName()); // 홍길동 출력
  • XML 파일에서 객체 정보를 설정하여 주입

 

- Annotation 기반 DI

@Component // Spring이 자동으로 객체 생성
public class Person {
    private String name = "홍길동";

    public String getName() { return name; }
}
---
@Component
public class PersonService {
    
    @Autowired
    private Person person; // 자동 주입
    
    public void printName() {
        System.out.println(person.getName());
    }
}
---
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
PersonService service = context.getBean(PersonService.class);
service.printName(); // 홍길동 출력
  • 어노테이션(@)을 사용하여 Spring이 자동으로 객체를 주입

 

- Java Configuration 기반 DI

@Configuration
public class AppConfig {
    
    @Bean
    public Person person() {
        return new Person("홍길동");
    }
    
    @Bean
    public PersonService personService() {
        return new PersonService(person());
    }
}
---
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
PersonService service = context.getBean(PersonService.class);
service.printName(); // 홍길동 출력
  • Java 코드로 직접 Bean을 설정

 

Spring AOP (관점 지향 프로그래밍)

- Spring Aspect-Oriented Programming

  • 공통 기능(로깅, 보안 등)을 분리하여 코드 중복을 줄이는 기술
  • 핵심 로직과 부가 기능(로깅, 트랜잭션)을 분리할 수 있음
  • AOP를 사용하면 핵심 로직에 영향을 주지 않고 특정 기능을 추가할수있음

 

- Logging

@Aspect
@Component
public class LogAspect {
    
    @Before("execution(* com.example.PersonService.*(..))")
    public void beforeMethod() {
        System.out.println("메서드 실행 전: 로그 출력!");
    }

    @After("execution(* com.example.PersonService.*(..))")
    public void afterMethod() {
        System.out.println("메서드 실행 후: 로그 출력!");
    }
}

 

 

마무리

- WorkShop

  • SpringDI 프로젝트 강의 진행된 순서에 따라 calculator 대신 다른 기능을 가진 class (interface)를 정의해서 진행
  • 조원들이 작성한 개인 코드를 다른 조원들에게 발표 진행
  • 발표 1인이 본인 또는 조에서 가장 잘한 코드

 

- 정리

  • Spring DI
    • XML 기반 → beans.xml에서 Bean 설정
    • Annotation 기반 → @Component, @Autowired로 자동 주입
    • Java Configuration 기반 → @Configuration, @Bean으로 직접 설정
  • Spring AOP
    • 공통 기능(로깅, 보안 등)을 따로 관리
    • @Aspect, @Before, @After 어노테이션 사용
    • 핵심 로직과 보조 기능을 분리하여 코드 유지보수성 향상

'LG 유플러스 유레카 > 스프링 프레임워크' 카테고리의 다른 글

[45일 차] 스프링 프레임워크 (Spring MVC)  (0) 2025.04.01
[42일 차] 스프링 프레임워크 (BookManager Servlet + JSP(MVC))  (0) 2025.03.26
[41일 차] 스프링 프레임워크 (HTTP,Servlet,JSP)  (0) 2025.03.25
'LG 유플러스 유레카/스프링 프레임워크' 카테고리의 다른 글
  • [46일 차] 스프링 프레임워크 (SpringBoot MVC DB + Ajax)
  • [45일 차] 스프링 프레임워크 (Spring MVC)
  • [42일 차] 스프링 프레임워크 (BookManager Servlet + JSP(MVC))
  • [41일 차] 스프링 프레임워크 (HTTP,Servlet,JSP)
문태신
문태신
3대500 백엔드 개발자가 꿈입니다.
  • 문태신
    별 될 시간
    문태신

  • 전체
    오늘
    어제
    • 전체 글 (80) N
      • LG 유플러스 유레카 (76) N
        • 강의 정리 (5)
        • 소프트웨어 엔지니어링 (8)
        • 알고리즘 (13)
        • 데이터베이스 활용 (5)
        • 미니 프로젝트 1 (3)
        • 데이터베이스 심화 (5)
        • 프론트엔드 이해 (3)
        • 깃허브 특강 (2)
        • 취업 특강 (2)
        • 스프링 프레임워크 (16)
        • 후기 (1)
        • REST API (9)
        • 미니 프로젝트 2 (4) N
      • 자격증 (1)
      • 디자인 (2)
      • 감상문 (1)
        • 책 (0)
        • 영화 (1)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
문태신
[44일 차] 스프링 프레임워크 (Spring DI/AOP)
상단으로

티스토리툴바