2-1. 스프링 프로젝트 시작하기
- 메이븐과 그래들 차이점
- 프로젝트를 생성하는 과정에서pom.xml 파일 대신 build.gradle 을 작성한단 것. 작성방식의 차이 (사용자입장)
어노테이션
- @Configuration : 해당 클래스를 스프링 설정 클래스로 지정
- @Bean: 해당 객체가 스프링이 관리하게 됨
@Configuration
public class AppContext {
@Bean
public Greeter greeter() {
Greeter g = new Greeter();
g.setFormat("%s, 안녕하세요!");
return g;
}
}
public class Greeter {
private String format;
public String greet(String guest) {
return String.format(format, guest);
}
public void setFormat(String format) {
this.format = format;
}
}
- 등록된 빈을 가져와 사용하기
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppContext.class);
Greeter g = ctx.getBean("greeter", Greeter.class);
String msg = g.greet("스프링"); // 스프링, 안녕하세요!
ctx.close(); // 닫아주어야 함
- AnnotationConfigApplicationContext : 자바클래스에서 스프링 핵심 객체를 생성
스프링 = 객체 컨테이너
- 스프링 컨테이너 : greeter를 가짐
2-2 싱글톤 객체
- 싱글톤 : 단일 객체(single object). 한 개의 @Bean에 한 개의 빈 객체만을 생성
'공부 > Spring' 카테고리의 다른 글
[chap4] 의존 자동 주입 (0) | 2022.05.20 |
---|---|
[chap3] 스프링 DI (0) | 2022.05.20 |
JdbcTemplate를 사용하는 이유 (3) | 2022.05.08 |
[Spring] DTO의 사용 범위 (0) | 2022.05.07 |
[Java] @Transaction(readOnly=true)을 사용하면 성능이 향상되는 이유 (4) | 2022.05.07 |
댓글