01. Java
02. git
03. Database
04. Jsp [Server]
05. HTML,CSS
07. JS
06. 미니프로젝트-2W
08. SpringFramework , SrpingBoot (v)
09. React JS [Front-end]
10. 중간프로젝트 (1M)
11. Linux 명령어
12. AWS 클라우드
13. DevOps - Docker
14. App - Android
15. 최종프로젝트 (1M)
스프링 | 스프링부트 | |
---|---|---|
프로젝트의 구조 | MVC2 | MVC2 |
빌드 툴 | MAVEN | GRADLE |
빌드 설정파일 | Pom.xml | Build.gradle |
스프링 설정파일 | xml파일 | java파일 & Application.properties |
설정 방식 | 수동 | 자동 |
JDBC | Mybatis | Mybatis & JPA |
View에 대한 처리 | JSP | Thymleaf or JSP 등 |
어노테이션 | 기존 | 기존 + @ |
롬복(Lombok)
- VO(DTO)의 생성자, getter, setter를 자동으로 생성해주는 라이브러리
- IntelliJ에 적용 :
File > Settings > Plugins에서 Lombok 설치 후 재시작
- Lombok설정 확인 :
Annotation Processors > Enable annotation processing을 체크
Lombok 사용
@NoArgsConstructor
@AllArgsConstructor
@Data
= @Getter + @Setter + @ToString
@NoArgsConstructor // 기본생성자 @AllArgsConstructor // 멤버변수를 받는 생성자 //@Getter //@Setter //@ToString @Data // getter + setter + toString를 합침 public class TestVO { // 단축키 alt + insert private String id; private String pw; private String name; private int salary; private String address; private LocalDateTime hiredate; }
- 스프링부트는 View가 없음
- 뷰가 없는 서버의 형태로 사용 가능
- 기존의 JSP/JSTL를 사용 가능
JSP를 뷰로 적용
- gradle 파일
- application.properties 파일
- 프로젝트의 구조를 Spring과 동일하게 구성
src > main > webapp > WEB-INF > views > jsp파일
@Configuration
: 스프링 설정 파일임을 의미@Bean
: 빈으로 등록@PropertySource
: Application.properties를 참조 가능@Value
: Application.properties에 값을 직접 참조@Configuration // 이 클래스를 스프링의 자바설정파일로 씀 public class WebConfig implements WebMvcConfigurer { @Bean // 스프링이 이 메서드를 호출 시켜서 반환되는 값을 bean으로 등록 시킴 public void myTest(){ System.out.println("설정 파일 동작함"); } @Bean public TestServiceImpl testServiceImpl(){ return new TestServiceImpl(); } }
- 스프링 애플리케이션 전반에 걸쳐 모든 구성요소의 제어 작업을 담당하는 IoC엔진
- applicationContext안에는 @Controller로 생성한 Bean정보를 확인 가능
- @Value로 application.properties값을 곧바로 참조 가능
- 개별적인 빈 설정을 커스터마이징 할 수 있음
@Value("${server.port}") private String port; // application-properties 참조 @Value("${my.example.port}") private String myport; // application-production.properties참조 @Autowired private ApplicationContext applicationContext; // IOC컨테이너로 동작하는 객체 @Bean // 스프링이 이 메서드를 호출 시켜서 반환되는 값을 bean으로 등록 시킴 public void myTest(){ System.out.println("설정 파일 동작함"); System.out.println("빈의 개수"+ applicationContext.getBeanDefinitionCount()); HomeController controller= applicationContext.getBean(HomeController.class); // 이 타입 bean객체를 찾음 System.out.println("ioc컨테이너안에 컨트롤러 객체:"+controller); System.out.println("프로퍼티파일의 port값: "+port); System.out.println("프로덕션-프로퍼티파일의 port값:"+myport); }
- 작은 단위 별로 테스트코드를 작성하고, 해당 테스트를 통과하기 위한 코드를 작성해 나가는 것
- 테스트코드 작성시 반드시 basic의 하위패키지에 작성
- 빌더 패턴을 사용하면 어느 필드에 어떤 값을 채워야 할지 명확하게 알 수 있음
- 코드의 가독성이 올라감
- 기존 클래스에는 setter메서드와 public 생성자가 없기 때문에 객체를 얻기 위해서는 오직 Builder 클래스를 통해서만 가능
- 시작시 내부 클래스 builder()를 호출하고, setter를 사용할 때 다시 나 자신을 반환시킴
- 객체 생성시 build()를 사용하면 외부클래스를 반환시키고 값을 저장시킴
- 객체 불변성을 보장
내부클래스
- 클래스안에 클래스
- 내부 클래스 종류
- inner클래스 : 안에서 밖으로 접근이 가능(외부참조 가능)
- static inner 클래스 : 안에서 밖으로 접근이 불가능(외부참조 불가능)
@NoArgsConstructor @AllArgsConstructor @Data @Builder public class TestBuilderVO { // 빌더패턴이란 - 객체의 불변성을 위해서 사용하는 스프링이 애용하는 문법 // 1. 멤버변수 private String name; private int age; /* // 3. 외부클래스가 생성될때 내부클래스인 Builder를 매개변수로 받음 private TestBuilderVO(Builder builder){ this.name = builder.name; this.age = builder.age; } // 4. 외부에서 객체 생성을 요구할 때 builder메서드를 호출하도록 함 public static Builder builder(){ return new Builder(); // 내부클래스 반환 } //7. toString의 오버라이딩 @Override public String toString() { return "BuilderVO [name=" + name + ", age=" + age + "]"; } // 2. 내부클래스 public static class Builder{ private String name; private int age; // 생성자 제한 private Builder(){} // 5. 빌더클래스에서는 set메서드만 생성하고, 자신을 반환시킴 public Builder setName(String name){ this.name = name; return this; } public Builder setAge(int age){ this.age = age; return this; } // 6. build메서드를 호출하면 3번의 생성자를 통해서 멤버변수를 외부에 저장시킴 public TestBuilderVO build(){ return new TestBuilderVO(this); } } */ }
public class Car { private final String color; private final String engine; private final int seats; // private 생성자 (빌더 패턴을 통해서만 생성 가능) private Car(Builder builder) { this.color = builder.color; this.engine = builder.engine; this.seats = builder.seats; } // Getter public String getColor() { return color; } public String getEngine() { return engine; } public int getSeats() { return seats; } @Override public String toString() { return "Car [color=" + color + ", engine=" + engine + ", seats=" + seats + "]"; } // Builder 클래스 public static class Builder { private String color; private String engine; private int seats; // 각 필드를 설정하는 메서드 public Builder setColor(String color) { this.color = color; return this; } public Builder setEngine(String engine) { this.engine = engine; return this; } public Builder setSeats(int seats) { this.seats = seats; return this; } // 최종 객체를 생성하는 build 메서드 public Car build() { return new Car(this); } } public static void main(String[] args) { // Builder 패턴으로 객체 생성 Car car = new Car.Builder() .setColor("Red") .setEngine("V8") .setSeats(4) .build(); System.out.println(car); } }
자바 라이브러리이며, 텍스트, HTML, XML, Javascript, CSS 그리고 텍스트를 생성할 수 있는 템플릿 엔진
스프링 MVC와의 통합 모듈을 제공하며, 애플리케이션에서 JSP로 만든 기능들을 완전히 대체 가능
출력
<h3 th:text="${'hello World'}"></h3> <h3>[[${'hello World'}]]</h3>
th:with
<div th:with="a=10"> [[${a}]] </div>
th:if
,th:unless
<div th:with="a=230"> <span th:if="${a==20}">20입니다</span> <span th:unless="${a==20}">20이 아닙니다</span> </div>
<div th:with="a=10"> [[${a==10?'같음':'다름'}]] </div>