본문 바로가기

Spring12

스프링 HttpMessageConverters HttpMessageConverters 스프링 부트로 앱을 개발하다가 Get,Post방식으로 클라이언트쪽에서 보낸 데이터를 자동으로 자바빈으로 매핑을 해주는 것이 어떠한 방식으로 해주는지 문득 궁금해서 이번에 알아보았다. Spring MVC uses the HttpMessageConverter interface to convert HTTP requests and responses. Sensible defaults are included out of the box. For example, objects can be automatically converted to JSON (by using the Jackson library) or XML (by using the Jackson XML extension, i.. 2021. 1. 27.
Autowired Autowired Autowired는 필요한 의존 객체의 타입에 해당하는 Spring Bean을 찾아서 주입해준다 예를 들어서 BookService는 BookRepository를 필요로한다. public class BookService{ BookRepository bookRepository; } 여기에 BookRepository를 자동으로 등록을 해주는게 Autowired이다 public class BookService{ @Autowired BookRepository bookRepository; } Autowired를 사용하는 방법에는 3가지 방법이 있다. 생성자 Setter 필드 생성자 생성자는 필요한 의존 객체를 생성자의 매개변수로 등록해서 받는 방법이다. public class BookService.. 2020. 5. 11.
Environment Environment Spring에는 Environment 즉, 환경에 따라 앱을 바꿀 수 있는 기능이 있다. 무슨 소린가 하면 한가지 앱을 만들다보면 이런 상황에서는 이 Bean들만 사용되었으면 좋을 텐데 이런 생각이 들 수 있는데 그걸 해준다. 또는 IOC 컨테이너를 좀 분리한다고 생각하면 될 것 같다. @Configuration @Profile("test") // test라는 Profile에서만 유효한 설정 public class TestConfiguration { @Bean public BookRepository bookRepository(){ return new TestBookRepository(); } } 이렇게 하면 위의 클래스는 test라는 Profile에서만 유효한 설정 클래스가 된다. .. 2020. 5. 11.
Scope Scope Spring에서는 Spring Bean의 범위는 Singleton이 기본이다. 때로는 Springton 범위가 아닌 Prototype을 가져야할 때가 있다.(아주 가끔) Scope Singleton Prototype Request Session WebSocket … 스코프를 설정하는 방법은 Singleton은 기본이니 따로 설정 방법이 필요 없고 Prototype만 있다. @Component @Scope("prototype") public Class Proto{ } 위와 같이 하면 Scope가 prototype으로 정해진다. Prototype과 Singleton을 생각할때 주의해야할점이있다. Prototype Bean안에 Singleton이 있는건 큰문제가 없지만 Singleton Bean안에.. 2020. 5. 11.
Component ComponentScan의 주요 기능 스캔 위치 설정 필터링(어떤 Annotation을 스캔하지 않을지) ComponentScan은 안에 문자열로 스캔 위치를 지정할 수 있다. ComponentScan(basePackages), ComponentScan(basePackageClasses) 이렇게 2가지 방식이 있는데 ComponentScan(basePackages)는 특정 패키지안에서 @Component를 찾아서 Spring Bean으로 등록한다. Compnent(basePackageClasses)는 특정 클래스를 지정하고 그 클래스의 위치로 부터 하위 패키지까지 모두 스캔해서 @Compnent를 찾아 Spring Bean으로 등록한다. 위 두가지 방식에서의 중요한 차이점이 하나 더 있는데 basePac.. 2020. 5. 11.
IOC 컨테이너 IOC: Inversion of Control IOC의 단어 뜻을 보면 제어 역전이다. 그럼 무엇을 제어하는 것을 역전하는 것인가?? 바로 객체 생성이다. 우리는 자바에서 자신이 만든 클래스를 다른 곳에서 사용하기 위해 객체를 생성(인스턴스)를 한다. 바로 한번 예시 코드를 보자 // Student 클래스 public class Student{ private String name; } //School 클래스 public class School{ private Student student; public void init(){ this.student = new Student(); } } 일단 위의 코드를 보면 Student클래스를 School클래스가 사용하기 위해 init메소드 안에서 new Student(.. 2020. 5. 11.