2022-11-14 1. 방법 최초 실행하고 싶은 메서드를 우선적으로 만든 후 아래와 같이 코드를 작성한다. import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; import lombok.RequiredArgsConstructor; @Component @RequiredArgsConstructor public class ServiceInit implements InitializingBean{ private final ServiceInitTest serviceInitTest; //자신이 사용하고자 하는 클래스를 작성 @Override public void afterPro..
2022-10-28 1. 방법 test.properties 파일 내용 test=test1;test2;test3 properties 파일의 value에 구분하고 싶은 문자열을 두고 ; 를 구분 기호로 사용하였다. @Configuration 파일 내용 @Configuration @PropertySources({ @PropertySource(name = "app", value = "classpath:config/test.properties", encoding = "UTF-8") }) @Getter public class PropertiesConfig implements EnvironmentAware { @Override public void setEnvironment(Environment env) { } //..
2022-08-09 @Bean과 @Component는 두 개다 스피링 IOC의 객체를 생성하는 기능을 한다. 이 둘의 차이점을 알아보자. 1. @Bean vs @Component 우선 @Bean 은 @Configuration와 세트로 사용된다. @Configuration은 기존의 config (XML에서 작성하던) 형식의 파일들을 java.class 형태로 작성할 수 있도록 도와주는 어노테이션이다. @Configuration 안에는 IOC에 들어간 bean이 필요한데, 이러한 객체를 생성하는 메서드에게 @Bean 어노테이션을 적어준다. 예로는 아래와 같이 DB 접속 정보가 들어간 config 파일에 사용한다. @Configuration public class DatasourceConfig { @Bean ..
2022-06-10 정적이 파일 properties 에는 주로 config(설정 관련 값)들이 들어 있다. 이들을 하나의 class화 시켜 getter로 사용하는 방법을 알아보자. 1. test.properties TEST_KEY=test123 우선 자신의 properties의 파일의 정확한 경로와 가져와야 하는 키/값을 알고 있어야 한다. 2. PropertiesConfig import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Configuration; import org.sp..
2022-06-08 자신이 서비스하는 웹 애플리케이션에서 쓸데없이 로봇들이 크롤링하는 것들을 막을 수 있는 방법이 있다. robots.txt를 사용하는 건데 이를 spring에서는 어떻게 적용하는지 알아보자. - 방법 사실 방법은 매우 간단하다. /** * 봇 크롤링 막기 */ @RequestMapping(value = "/robots.txt") @ResponseBody public void robotsBlock(HttpServletRequest request, HttpServletResponse response) { try { response.getWriter().write("User-agent: *\nDisallow: /\n"); } catch (IOException e) { log.info("exc..
2022-05-06 기존에 @RequestBody 어노테이션을 사용하다가, content-type이 text/plain 형식의 json 데이터를 처리해야 하는데, [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported] 같은 오류가 발생하였으며, 이를 해결하는 방법을 찾아 공유하고자 한다. 방법은 아래와 같다. - 코드 @RequestMapping(value="/") public void test(HttpServletRequest request){ //body에 있는 제이슨을 받은 객체 String bodyJson = ""; StringBuilder str..