스프링목차
Environment
프로파일과 프로퍼티를 다루는 인터페이스
Environment | ApplicationContext extends EnvironmentCapable profile 활성화여부 확인 및 설정 프로퍼티 소스 설정 및 프로퍼티 값 가져오기 |
profile | Environment 에서 프로파일 확인 및설정. -클래스- @Configuration @Profile(“test”) @Component @Profile(“test”) -메소드- @Bean @Profile(“test”) 사용 설정추가. -Dspring.profiles.avtive=”test,A,B,...” 테스트 ; ActiveProfiles 설정방법 ! (not) & (and) | (or) |
Property | StandardServletEnvironment의 우선순위 - ServletConfig 매개변수 - ServletContext 매개변수 - JNDI (java:comp/env/) - JVM 시스템 프로퍼티 (-Dapp.key=”value”) enviroment.getProperty("app.key"); - JVM 시스템 환경 변수 (운영 체제 환경 변수) |
@PropertySource | Environment를 통해 프로퍼티 추가하는 방법 @PropertySource("classpath:/app.properties") >> 스프링 부트 어플리케이션 지정한 클래스에 추가 >> enviroment에 추가됨. |
스프링 부트 프로퍼티 | 기본 프로퍼티 소스 지원 (application.properties) 프로파일까지 고려한 계층형 프로퍼티 우선 순위 제공 (yml) |
MessageSource | 국제화(i18n) 기능 제공하는 인터페이스 ApplicationContext extends MessageSource message.properties messages_ko_kr.properties >> 스프링 프로퍼티스. @Bean public MessageSource messageSource() { var messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:/messages"); messageSource.setDefaultEncoding("UTF-8"); messageSource.setCacheSeconds(3); return messageSource; } 운영중 빌드해주면.. 바로 반영. |
ApplicationEventPublisher | 이벤트 프로그래밍에 필요한 인터페이스 제공. 옵저버 패턴 구현체. ApplicationContext extends ApplicationEventPublisher publishEvent(ApplicationEvent event) 이벤트 처리하는 방법 ● ApplicationListener<이벤트> 구현한 클래스 만들어서 빈으로 등록하기. ● 스프링 4.2 부터는 @EventListener를 사용해서 빈의 메소드에 사용할 수 있다. ● 기본적으로는 synchronized. ● 순서를 정하고 싶다면 @Order와 함께 사용. ● 비동기적으로 실행하고 싶다면 @Async와 함께 사용. 스프링이 제공하는 기본 이벤트 ● ContextRefreshedEvent: ApplicationContext를 초기화 했더나 리프래시 했을 때 발생. ● ContextStartedEvent: ApplicationContext를 start()하여 라이프사이클 빈들이 시작 신호를 받은 시점에 발생. ● ContextStoppedEvent: ApplicationContext를 stop()하여 라이프사이클 빈들이 정지 신호를 받은 시점에 발생. ● ContextClosedEvent: ApplicationContext를 close()하여 싱글톤 빈 소멸되는 시점에 발생. ● RequestHandledEvent: HTTP 요청을 처리했을 때 발생. |
ResourceLoader | 리소스를 읽어오는 기능을 제공하는 인터페이스 ApplicationContext extends ResourceLoader 리소스 읽어오기 Resource resource = resourceLoader.getResource("classpath:test.txt"); ● 파일 시스템에서 읽어오기 ● 클래스패스에서 읽어오기 ● URL로 읽어오기 ● 상대/절대 경로로 읽어오기 |
Resource 추상화 | java.net.URL 추상화., 스프링 내부 많이 사용 인터페이스 org.springFramework.core.io.Resouce - 클래스 패스 기준 읽어오는 기능 부재 - ServletContet를 기준으로 상대경로로 읽어오는 기능 부재 - 새로운 핸들러 등록 URL 접미사 만들면 구현복잡. >> 불러오는 방법 통일. ○ getInputStream() ○ exitst() !!! 항상체크해서 사용. ○ isOpen() ○ getDescription(): 전체 경로 포함한 파일 이름 또는 실제 URL 구현체 ● UrlResource: java.net.URL 참고. ● ClassPathResource: 지원하는 접두어 classpath: ● FileSystemResource ● ServletContextResource: 웹 애플리케이션 루트에서 상대 경로로 리소스 찾는다. 리소스 읽어오기 ● Resource의 타입은 locaion 문자열과 ApplicationContext의 타입에 따라 결정 된다. ○ ClassPathXmlApplicationContext -> ClassPathResource ○ FileSystemXmlApplicationContext -> FileSystemResource ○ WebApplicationContext -> ServletContextResource ● ApplicationContext의 타입에 상관없이 리소스 타입을 강제하려면 java.net.URL 접두어(+ classpath:)중 하나를 사용할 수 있다. ○ classpath:me/whiteship/config.xml -> ClassPathResource ○ file:///some/resource/path/config.xml -> FileSystemResource *접두어 추천. 다양한개발자들을 위해.. 명시. |
'JAVA > spring' 카테고리의 다른 글
스프링 목차 (0) | 2021.04.29 |
---|---|
[1028 FORM 백기선 스프링프레임워크 핵심기술] SpEL, AOP,Null-safety (0) | 2021.04.29 |
[1028 FORM 백기선 스프링프레임워크 핵심기술] Validation, 데이터 바인딩. (0) | 2021.04.28 |
[1028 FORM 백기선 스프링프레임워크 핵심기술] IoC 컨테이너와 빈 (0) | 2021.04.28 |
[1028 from 백기선 스프링 JPA] 릴레이션 vs JPA, postgres using docker (0) | 2021.04.27 |