카테고리 없음

5. Spring 비지니스 계층

Handy Smurf 2020. 9. 9. 14:30

고객의 요그를 반영하는 계층으로 프레젠테이션 계층과 영속 계층의 중간 다리 역할을 한다.

영속 계층은 데이스 베이스를 기준으로 설계 구현

비지니스 계층은 로직을 기준으로 해서 처리하게 된다,

일반적으로 비지니스 영역에 있는 객체들은 서비스라는 용어를 많이 사용한다.

 

1. 아래처럼 BoardService / BoardServiceImpl (인터페이스) 생성

1. BoardService

 

package org.zerock.service;

import java.util.List;

import org.zerock.domain.BoardVO;

public interface BoardService {

	public void register(BoardVO board);
	public BoardVO get(Long bno);
	public boolean modify(BoardVO board);
	public boolean remove(Long bno);
	public List<BoardVO> getList();
}

2. BoardServiceImpl

 

package org.zerock.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zerock.domain.BoardVO;
import org.zerock.mapper.BoardMapper;

import lombok.AllArgsConstructor;
import lombok.Setter;


@Service
@AllArgsConstructor
public class BoardServiceImpl implements BoardService {
	@Setter(onMethod_ = @Autowired )
	private BoardMapper mapper;
	
	@Override
	public void register(BoardVO board) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BoardVO get(Long bno) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean modify(BoardVO board) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean remove(Long bno) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<BoardVO> getList() {
		// TODO Auto-generated method stub
		return null;
	}

}

3.root-context.xml

	<context:component-scan base-package="org.zerock.service"></context:component-scan>

 

 

4. org.zerock.service.BoardServiceTests

 

package org.zerock.service;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import lombok.Setter;
import lombok.extern.log4j.Log4j;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class BoardServiceTests {

@Setter(onMethod_ = @Autowired )
 private BoardService service;


@Test
	public void testExist() {
		log.info(service);
		assertNotNull(service);

}

}

5. 결과 확인

 

INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7085bdee, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1ce92674, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5700d6b1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6fd02e5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5bcab519, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@e45f292]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:12:49 KST 2020]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.zerock.service.BoardServiceTests - org.zerock.service.BoardServiceImpl@655ef322
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:12:49 KST 2020]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.

6. 등록 작업의 구현과 테스트 

  6-1 BoardServiceImpl

 

	@Override
	public void register(BoardVO board) {
	
		mapper.insertSelectKey(board);
		
	}

   6-2 BoardServiceTests

 

@Test
	public void testRegister() {
	
	BoardVO board = new BoardVO();
	 board.setTitle("새로 작성하는 글 select key");
	 board.setContent("새로 작성하는 내용 select key");
	 board.setWriter("newbie");
	 
	 service.register(board);
	 log.info("생성된 게시물의 번호:" + board.getBno());
}

 

INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7085bdee, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1ce92674, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5700d6b1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6fd02e5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5bcab519, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@e45f292]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:25:52 KST 2020]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.zerock.service.BoardServiceTests - org.zerock.service.BoardServiceImpl@7e276594
INFO : org.zerock.service.BoardServiceTests - 생성된 게시물의 번호:31
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:25:52 KST 2020]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
2	테스트 제목	테스트 내용	user00	20/08/26	20/08/26
30	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
4	테스트 제목	테스트 내용	user00	20/08/26	20/08/26
5	테스트 제목	테스트 내용	user00	20/08/26	20/08/26
6	수정된 제목	수정된 내용	user00	20/08/26	20/09/09
28	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
29	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/09	20/09/09
24	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/08	20/09/08
25	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/08	20/09/08
31	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
23	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/08	20/09/08
26	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
27	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/09	20/09/09

7.목록 리스트 작업의 구현과 테스트

 7-1  BoardServiceImpl

@Override
	public List<BoardVO> getList() {
		return mapper.getList();
	}

7-2 org.zerock.service.BoardServiceTests

 

	@Test
	public void testGetList() {
		
		service.getList().forEach(board -> log.info(board));

	}
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7085bdee, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1ce92674, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5700d6b1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6fd02e5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5bcab519, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@e45f292]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:36:55 KST 2020]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=2, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Aug 26 22:14:28 KST 2020, updateDate=Wed Aug 26 22:14:28 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=4, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Aug 26 22:14:30 KST 2020, updateDate=Wed Aug 26 22:14:30 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=5, title=테스트 제목, content=테스트 내용, writer=user00, regdate=Wed Aug 26 22:14:32 KST 2020, updateDate=Wed Aug 26 22:14:32 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=6, title=수정된 제목, content=수정된 내용, writer=user00, regdate=Wed Aug 26 22:14:32 KST 2020, updateDate=Wed Sep 09 10:37:26 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=23, title=새로 작성하는 글, content=새로 작성하는 내용, writer=newbie, regdate=Tue Sep 08 18:23:24 KST 2020, updateDate=Tue Sep 08 18:23:24 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=24, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Tue Sep 08 18:31:52 KST 2020, updateDate=Tue Sep 08 18:31:52 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=25, title=새로 작성하는 글, content=새로 작성하는 내용, writer=newbie, regdate=Tue Sep 08 18:31:52 KST 2020, updateDate=Tue Sep 08 18:31:52 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=26, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Wed Sep 09 09:35:00 KST 2020, updateDate=Wed Sep 09 09:35:00 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=27, title=새로 작성하는 글, content=새로 작성하는 내용, writer=newbie, regdate=Wed Sep 09 09:35:00 KST 2020, updateDate=Wed Sep 09 09:35:00 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=28, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Wed Sep 09 09:44:34 KST 2020, updateDate=Wed Sep 09 09:44:34 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=29, title=새로 작성하는 글, content=새로 작성하는 내용, writer=newbie, regdate=Wed Sep 09 09:44:34 KST 2020, updateDate=Wed Sep 09 09:44:34 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=30, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Wed Sep 09 12:25:03 KST 2020, updateDate=Wed Sep 09 12:25:03 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=31, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Wed Sep 09 12:25:54 KST 2020, updateDate=Wed Sep 09 12:25:54 KST 2020)
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=32, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Wed Sep 09 12:36:28 KST 2020, updateDate=Wed Sep 09 12:36:28 KST 2020)
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:36:55 KST 2020]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.

8. 조회 작업의 구현과 테스트

 8-1  BoardServiceImpl

 

	@Override
	public BoardVO get(Long bno) {
		
		return mapper.read(bno);
	}

 8-2 org.zerock.service.BoardServiceTests

 

@Test
	public void testGet() {
		
		log.info(service.get(31L));

	}
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7085bdee, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1ce92674, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5700d6b1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6fd02e5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5bcab519, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@e45f292]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:44:51 KST 2020]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.zerock.service.BoardServiceTests - BoardVO(bno=31, title=새로 작성하는 글 select key, content=새로 작성하는 내용 select key, writer=newbie, regdate=Wed Sep 09 12:25:54 KST 2020, updateDate=Wed Sep 09 12:25:54 KST 2020)
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 12:44:51 KST 2020]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.

9. 삭제 / 수정 작업의 구현과 테스트

 9-1  BoardServiceImpl

	@Override
	public boolean modify(BoardVO board) {
		return mapper.update(board) == 1;
	}

	@Override
	public boolean remove(Long bno) {
		return mapper.delete(bno) == 1;
	}

 9-2 org.zerock.service.BoardServiceTests

 

	//@Test
	public void testDelete() {
		
		log.info("REMOVE RESULT : " + service.remove(4L));

	}
	
	@Test
	public void testUpdate() {
		BoardVO board = service.get(5L);
		
		if(board == null ) {
			
			return;
		}
		board.setTitle("제목을 수정합니다");
		log.info("MODIFY RESULT : " + service.modify(board));

	}
}

 

INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7085bdee, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1ce92674, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5700d6b1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6fd02e5, org.springframework.test.context.transaction.TransactionalTestExecutionListener@5bcab519, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@e45f292]
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 14:29:03 KST 2020]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
INFO : org.zerock.service.BoardServiceTests - REMOVE RESULT : false
INFO : org.zerock.service.BoardServiceTests - MODIFY RESULT : true
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@3dd4520b: startup date [Wed Sep 09 14:29:03 KST 2020]; root of context hierarchy
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
INFO : com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.

 

2	테스트 제목	테스트 내용	user00	20/08/26	20/08/26
30	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
5	테스트 제목	테스트 내용	user00	20/08/26	20/08/26
6	수정된 제목	수정된 내용	user00	20/08/26	20/09/09
28	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
29	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/09	20/09/09
24	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/08	20/09/08
25	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/08	20/09/08
31	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
32	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
23	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/08	20/09/08
26	새로 작성하는 글 select key	새로 작성하는 내용 select key	newbie	20/09/09	20/09/09
27	새로 작성하는 글	새로 작성하는 내용	newbie	20/09/09	20/09/09