진취적 삶
2.1 스프링 프로젝트 시작하기 본문
1.2 메이븐 설정
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sp5</groupId>
<artifactId>sp5-chap02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2.1 메이븐 의존 설정
메이븐은 한 개의 모듈을 아티팩트라는 단위로 관리한다.
의존을 추가한다는 뜻은 일반적인 자바 어플리케이션에서 클래스 패스에 spring-context 모듈을 추가한다는 것을 뜻한다.
1.2.2 메이븐 리포지토리
spring-context-5.0.2.RELEASE 파일은 어디서도 다운로드 하지 않았다.
이 파일을 구할려면 어떻게 해야 할까? 답은 원격 리포지토리와 로컬 리포터지 토리에 있다.
메이븐은 코드를 컴파일 하거나 실행할때
- 메이븐 local repository 에서 [그룹ID][아티팩트ID][버전] 폴더에 아티팩트ID-버전.jar 형식의 이름을 갖는 파일이 있는 지 검사 하고 있으면 사용
- local repository에 파일이 없으면 maven 원격 중앙 repository로부터 해당 파일을 다운로드하여 local repository에 복사 한뒤 파일을 사용한다.
- dependency 설정만 알맞게 추가하면 필요한 jar 파일을 손쉽게 메이븐 프로젝트에 추가할수 있다 .
1.2.3 의존 전이
mvn compile 을 실행하면 spring-context-5.0.2.RELEASE 파일 외에 다양한 아티팩트 파일을 다운로드 하는것을 볼수 있다. spring-context-5.0.2.RELEASE 파일을 사용하기위해서는 다른 아티팩트 파일 또한 추가로 필요하기 때문에 다운로드 하는것
spring-context 는 spring -core ,spring-beans- spring-aop ,aspectjweaver 에 의존하고
따라서 의존하는 이 아티팩트 또한 다운로드 하게 되는것이다.
1.2.4 메이븐 기본 폴더 구조
XML 이나 프로퍼티 파일은 src/main/resources
웹 애플레케이션 개발할 때에는 src/main/webapp
1.2.5 메이븐 프로젝트 임포트
file → import ..누르고
maven → existing maven projects 누르고 pox.xml에 하고finish 하면 jar 파일 다운로드
1.3 그레이들 프로젝트 생성
메이븐과 거의 비슷하다 .
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding = "UTF-8"
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework:spring-context:5.0.2.RELEASE'
}
wrapper {
gradleVersion = '4.4'
}
gradlew compileJava
1.4 예제 코드 작성
- Greeter.java : 콘솔에 간단한 메시지를 출력하는 자바 클래스
- AppContent.java : 스프링 설정 파일
- Main.java : main()메서드를 텅해 스프링과 Greeter 를 실행하는 자바 클래스
@Configuration
public class AppContext {
@Bean
public Greeter greeter() {
Greeter g =new Greeter();
g.setFormat("%s,안녕하세요 !");
return g ;
}
}
@Configuration 은 해당 클래스를 설정 클래스로 지정
스프링이 생성하는 객체를 bean 객체
@bean 을 메서드에 붙이면 해당 메서드가 생성한 객체를 스프링이 관리하는 빈 객체로 등록
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AppContext.class);
Greeter g =ctx.getBean("greeter",Greeter.class);
String msg = g.greet("스프링");
System.out.println(msg);
ctx.close();
}
}
AnnotationConfigApplicationContext
자바 설정에서 정보를 읽어와 빈 객체를 생성하고 관리
AnnotationConfigApplicationContext(AppContext.class)
: AppContext 클래스를 생성자 파라미터로 전달 AppContext에서 정의한 @Bean 설정 정보를 읽어와 Greeter 객체를 생성하고 초기화
getBean
: AnnotationConfigApplicationContext
가 자바 설정을 읽어와 생성한 빈 객체를 검색할때 사용
getBean() 메서드의 첫 번째 파라미터는 @bean 메서드 이름인 빈 객체의 이름
두 번째 파라미터는 검색할 빈 객체의 타입 .
'스프링 5 프로그래밍 입문 > 2.스프링 시작하기' 카테고리의 다른 글
2.2 스프링은 객체 컨테이너 (0) | 2023.08.08 |
---|