@Controller : Controller의 역할을 하여 @Controller로 명시후 사용
@Service : Service 역할을 하는 클래스를 보통 @Service로 명시하여 사용
@Repsository : DAO, mapper, JPA등 데이터 저장소를 맵핑 또는 호출해주는 곳들을 보통 @Repository로 선언하고 사용함
@RequestMappting: class, method에 url맵핑을 제공함.
장점
단점
dispatcher.servlet.xml 변경
spring에서 제공하는 component-scan 기능은 어노테이션으로 선언된 패키지를 찾아서 스캔해주는 역할을 함.
base-package를 넣지 않는 경우 모든 패키지를 대상으로 스캔
기입하는 경우는 해당 패키지 시작점~ 패키지 하위까지 모두 스캔함.
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-2.5.xsd"
>
<!-- example 패키지를 스캔 (@controller, @service, @repository, @component) -->
<context:component-scan base-package="example" />
<!-- JSP View resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
application-context.xml
- spring-orm에서 ibatis를 지원해주는 sqlMapClientTemplate를 사용하도록 빈 등록
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>
<!-- 프로퍼티 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/config/properties/database.properties"/>
<property name="fileEncoding" value="UTF-8" />
</bean>
<!-- dataSource bean 설정 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<!-- ibatis -->
<!-- class 이름 바뀜 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:config/ibatis/sql-map-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- dao에서 사용할 빈 등록 -->
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<constructor-arg index="0" ref="dataSource"/>
<constructor-arg index="1" ref="sqlMapClient"/>
</bean>
<!-- 주입 삭제됨 -->
</beans>
BoardListController.java
@Controller 어노테이션을 선언하면 컨트롤러대상으로 스캔이된다.
추후에 DispatcherServlet으로 요청이 /board/list URL이면 BoardController에 list메소드가 호출된다.
JSP / Servlet을 사용할때는request.setAttribute로 key, value를 저장해서 사용했지만 controller 사용시 modelMap.setAttribute를사용하면 jsp에서 해당 value가 사용가능하다
package example.controller;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import example.service.BoardService;
@Controller
public class BoardController {
final Logger logger = LogManager.getLogger(getClass());
@Autowired
private BoardService boardService;
/**
* 게시물 목록 조회 및 화면
* @param model
* @return
*/
@RequestMapping(value = "/board/list", method = RequestMethod.GET)
public String list(ModelMap model) {
// 게시물 목록 조회 후 model에 boardList key로 저장
model.addAttribute("boardList", boardService.selectBoardList());
// jsp를 호출
return "/board/list";
}
}
package example.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class BoardDao {
@Autowired
private SqlMapClientTemplate sqlMapClientTemplate;
@SuppressWarnings("unchecked")
public List<Board> selectBoardList(){
return sqlMapClientTemplate.queryForList("selectBoardList");
}
public Board selectBoard(int boardSeq){
return (Board) sqlMapClientTemplate.queryForObject("selectBoard", boardSeq);
}
public void insertBoard(Board board){
sqlMapClientTemplate
.insert("insertBoard", board);
}
public void deleteBoard(int boardSeq){
sqlMapClientTemplate
.insert("deleteBoard", boardSeq);
}
public void updateBoard(Board board){
sqlMapClientTemplate
.update("updateBoard", board);
}
}