1) 설정파일


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


<context:component-scan base-package="spring.anno.board" />


</beans>




2) DTO 클래스


package spring.anno.board;

import org.springframework.stereotype.Component;


@Component // DTO 기능의 어노테이션

public class BoardDTO {


private int no = 1;

private String name = "홍길동";

private String title = "축하";

private String contents = "게시판 오픈 축하합니다.";

public int getNo() {

return no;

}

public void setNo(int no) {

this.no = no;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getContents() {

return contents;

}

public void setContents(String contents) {

this.contents = contents;

}

}



3) 서비스 인터페이스


package spring.anno.board;

public interface BoardService {

public void insert(BoardDTO boardDTO);

}



4) 서비스 클래스

package spring.anno.board;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service // 서비스 기능의 어노테이션
public class BoardServiceImpl implements BoardService {

@Autowired 
private spring.anno.board.boardDAO boardDAO; // DAO 안에 있는 insert를 호출하기 위함
@Override
public void insert(BoardDTO boardDTO) {
System.out.println("Service 클래스 호출");
boardDAO.insert(boardDTO);
}

}


5) DAO 인터페이스

package spring.anno.board;

public interface boardDAO {

public void insert(BoardDTO boardDTO);

}



6) DAO 클래스

package spring.anno.board;


import org.springframework.stereotype.Repository;


@Repository // DAO 기능의 어노테이션

public class BoardDAOImpl implements boardDAO {


@Override

public void insert(BoardDTO boardDTO) {

System.out.println("BoardDAO 클래스 호출");

System.out.println("===== BoardDTO =====");

System.out.print(boardDTO.getNo());

System.out.print("\t"+boardDTO.getName());

System.out.print("\t"+boardDTO.getTitle());

System.out.println("\t"+boardDTO.getContents());

System.out.println("=======================");

}

}




7) 컨트롤러
package spring.anno.board;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller // 컨트롤러 기능의 어노테이션
public class BoardController {
@Autowired
private BoardService boardService;
public void insert(BoardDTO boardDTO){
boardService.insert(boardDTO);
}
}



8) 메인


package spring.anno.board;


import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class BoardMain {

public static void main(String[] args) {

AbstractApplicationContext context = 

new ClassPathXmlApplicationContext("board.xml");

context.registerShutdownHook();


BoardController controller  = 

context.getBean("boardController", BoardController.class);

BoardDTO boardDTO  = 

context.getBean("boardDTO", BoardDTO.class);

controller.insert(boardDTO);

context.close();

}

}


Service 클래스 호출

BoardDAO 클래스 호출

===== BoardDTO =====

1 홍길동 축하 게시판 오픈 축하합니다.

=======================



Posted by netyhobby
,