스프링 12/12
스프링에 대해서 배워보자.
중요한 구조
1) MVC 구조
2) MSA 구조 (REST API 사용)
지난 시간, jsp에 대해서 다뤄봤다.
JSP 에는 html과 java 영역을 모두 다뤘었다.
mvc구조랑 비교해서 이야기를 하면
jsp에서의 java 영역은 mvc 구조에서 controller 영역이고,
jsp에서의 html 영역은 mvc 구조에서 view 영역이라고 볼 수 있는 것이다.
일반적으로 java를 쓰는 이유? DB를 컨트롤 하기 위해서
스프링 프레임워크를 사용해서
메이븐 프로젝트를 만든다 ! (create simple project 체크)
메이븐에서 제공하고 있는 라이브러리 모음
https://repo1.maven.org/maven2/
Central Repository:
repo1.maven.org
메이븐 레포지토리
필요한 라이브러리 복사해서 사용할 수 있음 !
예시
https://repo1.maven.org/maven2/org/springframework/spring-context/
springframework - groupID
spring-context - artifactID
Dependency 추가 - pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
src/main/java
패키지 만들기 - pack01
src/main/java/pack01 안에
Hello.java 만들기
package Pack01;
import javax.security.auth.login.AppConfigurationEntry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericXmlApplicationContext;
/*
class Animal {
Animal() {
System.out.println("생성자 생성!");
}
void f1() {
System.out.println("f1 call ");
}
}
class Tiger extends Animal {
Tiger() {
System.out.println("생성자 생성!");
}
void f2() {
System.out.println("f2 call ");
}
}
public class Hello {
public static void main(String[] args) {
System.out.println(10);
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:Context.xml");
Animal ani1 = ctx.getBean("tiger",Animal.class);
ani1.f1();
Tiger ani2 = ctx.getBean("tiger", Tiger.class);
ani2.f2();
ctx.close();
}
}*/
class Apple{
Apple(){
System.out.println("생성자 ");
}
void f1() {
System.out.println("f1 함수 콜 ");
}
}
@Configuration
class Appconfig{
//설정
@Bean //객체를 생성하는 코드
public Apple apple() {
return new Apple();
}
}
public class Hello{
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Appconfig.class);
Apple apple = ctx.getBean("apple",Apple.class);
apple.f1();
ctx.close();
}
}