[kt-ds-university] Springboot

peter park·2019년 8월 27일
0

static : 정적파일 (html, css ..)
template : 동적파일 (jsp, thymeleaf ..)

@SpringBootApplication = @SpringBootConfiguration + @ComponentScan + @EnableAutoConfiguration

  1. @ComponentScan : 프로젝트 생성시 정해준 default 패키지 부터 스캔
  2. @EnableAutoConfiguration

@SpringBootConfiguration

클래스는 configuration용 클래스

@ComponentScan

@componentScan이 붙은 클래스가 위치해있는 현재 패키지 기준으로 그 아래 패키지를 찾아 스프링 빈으로 등록. @ComponentScan이 있는 클래스 하위의 패키지에 모든 자바파일을 생성

@EnableAutoConfiguration

스프링 프레임워크에서 많이 쓰이는 스프링 bean들을 자동적으로 컨테이너에 등록

spring-boot-autoconfigure-2.1.1.RELEASE.jar\META-INF\spring.factoies org.springframework.boot.autoconfigure.EnableAutoConfiguration=\아래 @Configuration 있는 클래스를 모두 Load

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
...

war는 무조건 톰캣에 배포해야한다. 스프링부트는 jar로 배포가 가능한데 jsp로 작성해면 실행이 안된다.

SpringBoot vs Spring

  • 스프링부트는 thirdparty 라이브러리로 이미 dependancy에 들어가 있어서 바로 사용할 수 있음
  • actuator 모니터링 기능
  • Pivotal회사
  • OLTP : 온라인상에서 사용하는 프로그램
  • dependency가 starter라는 이름으로 제공
  • thirdparty 라이브러리 제공
  • XML로 설정하지 않는다.

Reactive spring

비동기 통신

application.properties

server.port=8000

scope이 테스트인 디펜던시는 모두 test 폴더 아래에서만 작성해야 한다.

<scope>test<scope>

모든 버전 관리
pom.xml

<properties>
	<java.version>1.8</java.version>
	<spring.version>5.1.8.RELEASE</spring.version>
</properties>

@SpringBootApplication가 존재하는 패키지 아래에 자바클래스를 만들어서 작성해야 한다. 스타팅 포인트가 되는데 내부적으로 @componentScan을 가지고 있어서 스캔을 하지 못할 수 있다.

@SpringBootApplication
public class MyspringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyspringbootApplication.class, args);
    }
}

IoC

제어의 역전
빈의 제어권을 개발자에서 프레임워크가 가져감
IoC 컨테이너를 제공

DL

dependency lookup

DI

dependency injection

SpringApplication

public static void main(String[] args) {
	SpringApplication.run(MyspringbootApplication.class, args);
}
웹어플리케이션 타입요소
WebApplicationType.SERVLET동기적(서블렛)
WebApplicationType.REACTIVE비동기적
WebApplicationType.NONEx
public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyspringbootApplication.class);       //스프링 어플리케이션 객체 생성
        application.setWebApplicationType(WebApplicationType.SERVLET);							  //웹 어플리케이션 타입 결정                       
        application.run(args);
    }

외부 설정 변수 사용하기

application.property

bgpark.name="Byeonggil Park"
bgpark.age=${random.int(1,100)}
bgpark.fullName=${bgpark.name}

MyRunner.java

@Value("${bgpark.name}")
    private String name;
    @Value("${bgpark.age}")
    private int age;

IntelliJ terminal

f12

error

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

ORM

javaee api
javaee의 특징은 모두 인터페이스로 되어있어 구현체가 없다. tmax,제우스나 IBM에서 오라클에게 인증을 받으면서 구현체를 만든다.

Consumer

함수형 인터페이스
추상메소드 하나 가지고 있는 인터페이스

  • functional 인터페이스의 이름, 메소드의 이름은 중요하지 않음
  • 그냥 functional 인터페이스에 argument만 중요
    람다식으로 바꿀 수 있음
    모든 functional 인터페이스는 모두 람다식으로 바꿀 수 있음
function say(arg) {
	return value
}

(arg) => value
Thread th = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("익명클래스");
            }
        });
	th.start();
        
Thread th02 = new Thread(() -> System.out.println("람다식 "));

Consumer
인자 하나만 받고 void하는 클래스

(arg) -> void

Supplier
인자 없이 객체 반환

() -> Exception

JSON 대신 XML 형식으로 출력하기

dependency 추가
Jackson 컨버터 dependency 추가

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.6</version>
</dependency>

UserController

@RequestMapping(value="/usersxml",produces = {"application/xml"})
    public List<User> getUserXml(){
        return userRepository.findAll();
    }
profile
Let's make something happen 👨🏻‍💻🌿

0개의 댓글