프로그래밍 노트/SPRING BOOT

Srping Validation Spring에서는 AOP와 같은 방식으로 더 편리하게 유효성검사를 할 수 있다. Validated 스프링에서는 유효성검사에 진입하게되는 지점에 @Validated라는 애노테이션을 사용하는 방법을 제공한다. 필요한 클래스나 메소드에 @Validated 어노테이션을 추가해서 사용한다. @Service @Validated public class UserService{ public User add(@NotNull User user){ } } 만약 제약조건에 위반되면 ConstraintViolationException이 발생하게 된다. (※ @ControllerExceptionHanlder를 사용하여 요긴하게 써먹을 수 있음. ConstrainoViolationException에서..
Vlidation은 데이터를 검증할 때 사용한다. 데이터 검증으 여러 계층에 걸쳐서 이루어지게 되는데, 이 과정으로 인해 문제점이 발생할 수 있다. 코드의 중복 검증로직 불일치로 인한 오류 이 문제를 해결하기 위해서 Java에서는 2009년부터 Bean Validation이라는 데이터 유효성 검사 프레임워크를 제공하고 있다. Bean Validation은 다양한 제약을 어노테이션을 사용하여 데이터를 검증할 수 있게 하였다. Bean Validation 1.0 (JSR-303) Bean Validation 1.1 (JSR-349) Bean Validation 2.0 (JSR-380) Hibernate Validator Hibernate Validator는 Bean Validation 명세에 대한 구현체이다..
프로퍼티 우선순위 유저 홈 디렉토리에 있는 spring-boot-dev-tools.properties 테스트에 있는 @TestPropertySource @SpringBootTest 애노테이션의 properties 애트리뷰트 커맨드 라인 아규먼트 SPRING_APPLICATION_JSON (환경 변수 또는 시스템 프로티) 에 들어있는 프로퍼티 ServletConfig 파라미터 ServletContext 파라미터 java:comp/env JNDI 애트리뷰트 System.getProperties() 자바 시스템 프로퍼티 OS 환경 변수 RandomValuePropertySource JAR 밖에 있는 특정 프로파일용 application properties JAR 안에 있는 특정 프로파일용 application..
@Conditional spring4 부터 사용가능하며, Java configuration에서 조건적으로 Spring Bean을 등록할 수 있다. @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface Conditional { /** * All {@link Condition}s that must {@linkplain Condition#matches match} * in order for the component to be registered. */ Class? extends Condition[] value(); } // Condtion.class public interface C..
배너를 만드는법 1. org.springframework.boot.Banner 인터페이스를 구현하여 custom banner 개발 @SpringBootApplication public class SampleApplication { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SampleApplication.class); springApplication.setBanner((environment, sourceClass, out)->{ out.println("Spring Boot! Corn!"); }); springApplication.run(args); } } 하지만.. 단..
1. 의존성 추가 org.springframework.boot spring-boot-starter-freemarker 2. freemarker template 작성 /src/main/resource/templates 경로에 hello.ftl 작성 This is freemarker sample. ${message} 3. Controller 작성 @Controller public class FreemarkerController { @GetMapping("/welcome") public String hello(Map model){ model.put("message", "hello freemarker!"); return "hello"; } }4. properties 설정 spring.freemarker.temp..
ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean을 이용해서 등록할 수 있다. 1. 필터 생성 및 등록 LogFilter1 public class LogFilter1 extends GenericFilterBean { private static final Logger log = LoggerFactory.getLogger(LogFilter1.class); @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, Servlet..
2019/07/11 - [프로그래밍 노트/SPRING BOOT] - [Spring Boot] 자동 설정 이해하기 @EnableAutoConfiguration 이 전에 자동설정과 관련된 포스팅을 했었는데, 어떻게 동작하는지 더 자세히 코드를 들여다보자. @EnableAutoConfiguration Spring Boot의 자동 구성을 위한 메타 어노테이션 애플리케이션에서 필요한 Bean을 유추해서 구성해 주는 기능을 담당 (@SpringBootApplication에 포함되어 있음) EnableAutoConfiguration 어노테이션을 들어가보면 AutoConfigurationImportSelector를 사용하는 것을 볼 수 있다. => @Import(AutoConfigurationImportSelector..
단위/통합 테스트 전용 스타터 폼 spring-boot-starter-test 만 있으면 손쉽게 테스트가 가능하다. (Junit, Hamcrest, Mockito 등이 존재) pom.xml 추가 org.springframework.boot spring-boot-starter-test test spring-test, junit, hamcrest, objenesis, mockito JAR 파일 의존성 스프링 부트 테스트 기본 기본 스프링 데이터 테스트 코드 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframewor..
자동 설정 이해하기 Spring boot main 클래스의 상위에 보면 @SpringBootApplication이라는 어노테이션을 볼 수 있다. @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); // 이 class와 arguments를 넘겨준다. } } 이 어노테이션을 아래와 같이 설정이 되어 있는데, 여기서 @EnalbeAutoConfiguration이란 놈이 자동으로 설정을 해주는 녀석이다. @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @D..
깡냉쓰
'프로그래밍 노트/SPRING BOOT' 카테고리의 글 목록