SpringBoot 게시판 만들기

스프링부트(SpringBoot) 게시판 만들기13 - 간단하게 application.properties 사용하기

abfc 2017. 9. 7.
반응형

application.properties 사용하기




간단하게 파일업로드 위치 같은 것들을 수정 했을뿐인데 프로젝트의 소스코드를 수정하고 재배포를 하는건 너무나 번거롭고, 운영을 하는 입장에서도 좋지 않겠죠~ 그럴때 설정파일로 등록해서 설정파일만 수정할 수 있도록 해줄수 있는 방법입니다.

설정파일로 뺄 수 있는것들은 최대한 빼는 것이 좋겠지만 이 예제에서는 파일업로드 위치를 설정파일로 등록하여 사용해보겠습니다.





1. application.properties 내용 추가

1
file.upload.directory = uploadFiles 폴더 위치
cs


file.upload.directory 는 제가 임의로 명시한것이지 원래 존재하는 설정명(?)이 아닙니다. fileDir로 해도 괜찮고, ffffff로 해도 괜찮습니다. 다만, 아래에 controller 에서 @Value라는 어노테이션에서 지정한 설정명으로 작성해줘야합니다.





2. BoardController.java에 사용할 설정 주입

1
2
@Value("${file.upload.directory}")
String uploadFileDir;
cs


*이와 같은 방식으로 application.properties에 설정한 내용을 주입시켜줄 수 있습니다. application.properties에 fffff로 설정해놨다면 어노테이션 내용에도 당연히 ${fffff}로 작성해야 설정파일을 읽어올 수 있습니다. 



3. BoardController.java에 boardInsertProc() 수정

 - boardInsertProc() 에서 fileUrl이라는 변수에 파일이 업로드 될 디렉토리path를 지정해놨는데 그 부분을 삭제하고, 설정파일에서 지정한 "uploadFileDir"을 사용합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@RequestMapping("/insertProc")
    private String boardInsertProc(HttpServletRequest request, @RequestPart MultipartFile files) throws Exception{
        
        BoardVO board = new BoardVO();
        FileVO  file  = new FileVO();
        
        board.setSubject(request.getParameter("subject"));
        board.setContent(request.getParameter("content"));
        board.setWriter(request.getParameter("writer"));
        
        
        if(files.isEmpty()){
            mBoardService.boardInsertService(board); //게시글 insert
        }else{
            String fileName = files.getOriginalFilename(); 
            String fileNameExtension = FilenameUtils.getExtension(fileName).toLowerCase(); 
            File destinationFile; 
            String destinationFileName; 
            //String fileUrl = "/Users/Documents/Workspace/MyProject/tistory/demo/src/main/webapp/WEB-INF/uploadFiles/"; 삭제
            
            do { 
                destinationFileName = RandomStringUtils.randomAlphanumeric(32+ "." + fileNameExtension; 
                destinationFile = new File(uploadFileDir+ destinationFileName); 
            } while (destinationFile.exists()); 
            
            destinationFile.getParentFile().mkdirs(); 
            files.transferTo(destinationFile); 
            
            mBoardService.boardInsertService(board); //게시글 insert
            
            file.setBno(board.getBno());
            file.setFileName(destinationFileName);
            file.setFileOriName(fileName);
            file.setFileUrl(uploadFileDir);
            
            mBoardService.fileInsertService(file); //file insert
        }
        
        
        return "redirect:/list";
    }
cs





4. 확인~








2017/09/05 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기12 - 부트스트랩 더 끼얹기2

2017/09/05 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기11 - 부트스트랩 더 끼얹기1

2017/09/05 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기10 - Multipart 파일 다운로드

2017/09/02 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기9 - Multipart 파일 업로드

2017/08/20 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기8 - 게시글 수정(update), 삭제(delete)

2017/08/20 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기7 - 게시글 상세(detail)

2017/08/20 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기6 - 게시글 목록(list) + jstl 태그 사용

2017/08/20 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기5 - 게시글 쓰기(Insert) + 부트스트랩 끼얹기

2017/08/20 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기4 - CRUD

2017/08/19 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기3 - MySQL, MyBatis, JSP 연동(2)

2017/08/19 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기2 - MySQL, MyBatis, JSP 연동(1)

2017/08/19 - [SpringBoot 게시판 만들기] - 스프링부트(SpringBoot) 게시판 만들기1 - 프로젝트 생성





반응형

댓글

💲 추천 글