설치방법

npm install express --save 로 설치

 

express의 사용이유

어플리케이션

- 익스프레스 객체를 어플리케이션이라고 한다.

- 서버에 필요한 미들웨어를 추가한다.

- 라우팅 설정을 할 수 있다.

- 서버를 요청 대기상태로 한다.

미들웨어

- 서버에기능을 추가하고 싶을때 사용하는 함수들의 배열이다.

 

라우팅

- 여러가지 URL로 들어올때 응답을 설정한다.

 

요청객체

- request로 생각하면된다. request를 한번더 감싼다.

응답객체

- response로 생각하면된다. response를 한번더 감싼다.

 

use로 미들웨어를 사용한다.

 

//미들웨어 만들기

function logger(req, res, next){

    console.log("Test");

    next();

}



app.use(logger);

 

next(); 를 선언해야 다음 미들웨어도 동작한다.

 

express실행하기

Port번호를 설정하고 실행해야한다.

var express = require('express');
var app = express();
//중간 로직
app.listen('3000', function (){
    console.log('apiServerStart');
})

Environment

 

Application Context은 Bean Factory기능만 하는게 아니다.

Application Context의 기능은 여러가지가 있다.

 

그 중하나가 Environment이다.(환경설정)

 

Environment 프로파일(Profile) 

프로파일이란 Bean들의 묶음 특정 Bean들을 묶어 사용할때 유용하다.

 

@Profile로 ComponentScan으로 등록할 수 있고 

@Repository

@Profile("!prod")//ComponentScan에 등록하기

public class TestBookRepository implements BookRepository{



    @PostConstruct

    @Override

    public void run() {

        System.out.println("TestBookRepository 생성");

    }

}

 

@Configuration을 통해 Class형식으로 등록할 수있다 

//profile정하고 class로 등록하는방법

@Configuration

@Profile("test")//test profile일때만 사용

public class TestConfiguration {



    @Bean

    public BookRepository bookRepository(){

        return new TestBookRepository();

    }

}

또 특정 profile만 실행하는 것이 아닌 특정 프로파일이 아닐때도 사용가능하다. !not 사용

prod Profile이 아닐때 예제코드

@Repository
@Profile("!prod")//ComponentScan에 등록하기
public class TestBookRepository implements BookRepository{

    @PostConstruct
    @Override
    public void run() {
        System.out.println("TestBookRepository 생성");
    }
}

 

environment를 통해 properties 사용하기

Configuraion에 VMoption을 등록하거나

resources 경로 아래 Properties 파일을 생성하고

스프링시작부분에 @PropertySource("classpath:./App.Properties”)를 등록하고

꺼내올 수 있다.

@SpringBootApplication

@PropertySource("classpath:./App.Properties")

public class EnvironApplication {



    public static void main(String[] args) {

        SpringApplication.run(EnvironApplication.class, args);

    }

}

 

 

BeanFactory에 등록된 bean들은 scope가 있다.

 

기본 Scope : Singleton Scope

Singleton Scope 이란?

해당 인스턴스가 하나뿐이다. 같은 인스턴스를 재사용하는 것이다.

 

기본적으론 생성된 bean은 SingletonScope이라 같은 인스턴스을 바라본다.

@Scope("prototype”)를 설정하면 매번 다른 인스턴스를 생성할 수 있다.

 

@Scope(“prototype”)차이점 보기

1.Single Class는 기본 Singleton 스코프

2.Proto Class는 prototype스코프로 선언

 

출력해보면 결과가 다르게 나온다.

@Component

public class Single {



    @Autowired

    Proto proto;



    public Proto getProto() {

        return proto;

    }

}



@Component @Scope("prototype")

public class Proto {

}



@Override

public void run(ApplicationArguments args) throws Exception {

        System.out.println("single");
        System.out.println(context.getBean(Single.class));
        System.out.println(context.getBean("single"));
        System.out.println(single);

        System.out.println("poroto");
        System.out.println(context.getBean(Proto.class));
        System.out.println("----");
        System.out.println(context.getBean("proto"));
        System.out.println("---");
        System.out.println("single.getProto().getClass() : 1----");
        System.out.println(context.getBean(single.getProto().getClass()));
        System.out.println("single.getProto().getClass() : 2----");
        System.out.println(context.getBean(single.getProto().getClass()));
        System.out.println("----같은 인스턴스 보는 이슈");
        System.out.println(context.getBean(Single.class).getProto());
        System.out.println(context.getBean(Single.class).getProto());


}
single
com.choi.scope.Single@4b6e1c0
com.choi.scope.Single@4b6e1c0
com.choi.scope.Single@4b6e1c0
poroto
Proto 인스턴스 생성
com.choi.scope.Proto@26cb5207
----
Proto 인스턴스 생성
com.choi.scope.Proto@15400fff
---
single.getProto().getClass() : 1----
Proto 인스턴스 생성
com.choi.scope.Proto@18d910b3
single.getProto().getClass() : 2----
Proto 인스턴스 생성
com.choi.scope.Proto@1e7ab390
----같은 인스턴스 보는 이슈
com.choi.scope.Proto@11d4dbd6
com.choi.scope.Proto@11d4dbd6

ProtoType에서 Singleton을 Autowired 하면 큰 문제없다.

하지만 Singleton에서 Prototype을 Autowried 한다면?

계속 같은 주소를 불러오는 이슈가 있다.

이때는 @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)

설정으로 프록시로 감싸서 사용하면 된다.

 

@Component @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)

public class Proto {



    @PostConstruct

    public void main(){

        System.out.println("Test");

        System.out.println("생성자 주입");

        System.out.println("Proto 인스턴스 생성");

    }

}
single
com.choi.scope.Single@2f897dab
com.choi.scope.Single@2f897dab
com.choi.scope.Single@2f897dab
poroto
Proto 인스턴스 생성
com.choi.scope.Proto@39a87e72
----
Proto 인스턴스 생성
com.choi.scope.Proto@5d2828c9
---
single.getProto().getClass() : 1----
Proto 인스턴스 생성
com.choi.scope.Proto@3a082ff4
single.getProto().getClass() : 2----
Proto 인스턴스 생성
com.choi.scope.Proto@45acdd11
----같은 인스턴스 보는 이슈 해결
Proto 인스턴스 생성
com.choi.scope.Proto@3f0d6038
Proto 인스턴스 생성
com.choi.scope.Proto@237f7970

DB 연결을 자주 수정해야 할 때가 있다면 유용하게 사용할 수 있을 것 같다.

 

보통 Singletion 생명주기가 길다. 여러 스레드에서 참조하는 경우 값이 계속바뀔수 있기에 prototype으로 생성하는것이 좋을거 같다.

+ Recent posts