λ‘œμΌ“πŸΎ
article thumbnail

 

0.0.1. μŠ€ν”„λ§μ˜ κΈ°λŠ₯듀쀑 ν•˜λ‚˜μΈ DI와 AOPλ₯Ό μ‚¬μš©ν•˜λ €λ©΄ λ”°λ‘œ XML νŒŒμΌμ—μ„œ 섀정을 ν•΄μ•Όν–ˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ XMLνŒŒμΌμ— μ΄λŸ¬ν•œ 섀정듀을 ν•˜κΈ°μ—” λ„ˆλ¬΄λ‚˜ λ³΅μž‘ν•˜κ³  μ–΄λ €μ› μŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ μŠ€ν”„λ§μ—μ„œλŠ” DI 같은 μžλ°” μ½”λ“œμ™€ κ΄€λ ¨λœ 섀정은 μ½”λ“œμ—μ„œ 직접할 수 μžˆλ„λ‘ ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έ κΈ°λŠ₯을 μ• λ„ˆν…Œμ΄μ…˜(Annotation)이라고 ν•©λ‹ˆλ‹€. 

0.0.2.  

0.0.3. ν˜„μž¬ μŠ€ν”„λ§μ—μ„œλŠ” XML파일 직접 μ„€μ •κ³Ό μ• λ„ˆν…Œμ΄μ…˜ 2개λ₯Ό ν˜Όν•©ν•΄μ„œ μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

 


1. l μŠ€ν”„λ§ μ• λ„ˆν…Œμ΄μ…˜ 제곡 클래슀

 

1.0.1. μ• λ„ˆν…Œμ΄μ…˜μ„ μ΄μš©ν•˜λ €λ©΄ XML νŒŒμΌμ—μ„œ λ‹€μŒ 2개의 클래슀λ₯Ό 빈으둜 μ„€μ •ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

 

클래슀 κΈ°λŠ₯
DefaultAnnotationHandlerMapping 클래슀 λ ˆλ²¨μ—μ„œ @RequestMapping을 μ²˜λ¦¬ν•©λ‹ˆλ‹€.
AnnotationMethodHandlerMapping λ©”μ„œλ“œ λ ˆλ²¨μ—μ„œ @RequestMapping을 μ²˜λ¦¬ν•©λ‹ˆλ‹€.

2. l νƒœκ·Έ

 

<context:component-scan base-package="νŒ¨ν‚€μ§€ 이름">

 

2.0.1. μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ‹€ν–‰ μ‹œ νŒ¨ν‚€μ§€ 이름 내에 μžˆλŠ” ν΄λž˜μŠ€λ“€μ„ μžλ™μœΌλ‘œ 빈으둜 λ§Œλ“€μ–΄ μ€λ‹ˆλ‹€.

 

μ• λ„ˆν…Œμ΄μ…˜ κΈ°λŠ₯
@Controller μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ component-scan에 μ˜ν•΄ μ§€μ •ν•œ 클래슀λ₯Ό 컨트둀러 빈으둜 μžλ™ λ³€ν™˜ν•©λ‹ˆλ‹€.
@Service μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ component-scan에 μ˜ν•΄ μ§€μ •ν•œ 클래슀λ₯Ό μ„œλΉ„μŠ€ 빈으둜 μžλ™ λ³€ν™˜ν•©λ‹ˆλ‹€.
@Repository μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ component-scan에 μ˜ν•΄ μ§€μ •ν•œ 클래슀λ₯Ό DAO 빈으둜 μžλ™ λ³€ν™˜ν•©λ‹ˆλ‹€.
@Component μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ component-scan에 μ˜ν•΄ μ§€μ •ν•œ 클래슀λ₯Ό 빈으둜 μžλ™ λ³€ν™˜ν•©λ‹ˆλ‹€.

3. l μ˜ˆμ‹œ

4.  

<html />
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/test/" /> <!-- <property name="prefix" value="/WEB-INF/views/member/" /> --> <property name="suffix" value=".jsp"/> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 클래슀 레벨 맡핑--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- λ©”μ„œλ“œ 레벨 맡핑--> <context:component-scan base-package="com.spring"/> <!-- ν•΄λ‹Ή νŒ¨ν‚€μ§€μ— μžˆλŠ” ν΄λž˜μŠ€μ— μ• λ„ˆν…Œμ΄μ…˜μ΄ μ μš©λ˜λ„λ‘ μ„€μ • --> </beans>

 

4.0.1. action-servlet.xml을 μœ„μ™€ 같이 μ„€μ •ν•΄μ€λ‹ˆλ‹€.

4.0.2. component-scan νŒ¨ν‚€μ§€ 내에 μžˆλŠ” 클래슀만이 빈으둜 μžλ™ μ„€μ •λ©λ‹ˆλ‹€.

 

<java />
package com.spring.ex01; //ν•΄λ‹Ή 클래슀λ₯Ό 빈으둜 μ„€μ •ν•˜κ³  μ‹ΆμœΌλ©΄ //<component-scan>μ—μ„œ μ„€μ •ν•œ νŒ¨ν‚€μ§€λ‚˜ ν•˜μœ„ νŒ¨ν‚€μ§€μ— μ‘΄μž¬ν•΄μ•Όν•©λ‹ˆλ‹€. import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller("mainController")//μ• λ„ˆν…Œμ΄μ…˜μ„ μ΄μš©ν•΄ 빈으둜 μžλ™ λ³€ν™˜ @RequestMapping(value="/test")//클래슀 λ‹¨κ³„μ˜ url 맡핑 public class MainController { @RequestMapping(value="/main1.do", method = RequestMethod.GET)//λ©”μ„œλ“œ λ‹¨κ³„μ˜ url 맡핑 public ModelAndView main1(HttpServletRequest request, HttpServletResponse response) throws Exception{ ModelAndView mv = new ModelAndView(); mv.addObject("msg", "main1"); mv.setViewName("main"); return mv; } @RequestMapping(value="/main2.do", method = RequestMethod.GET) public ModelAndView main2(HttpServletRequest request, HttpServletResponse response) throws Exception{ ModelAndView mv = new ModelAndView(); mv.addObject("msg", "main2"); mv.setViewName("main"); return mv; } }

 

4.0.3. @Controllerλ₯Ό μ΄μš©ν•˜μ—¬ idκ°€ mainControllerλΌλŠ” 빈으둜 μžλ™ μ„€μ •λ˜κ²Œ ν•΄μ€λ‹ˆλ‹€.

localhost:8080/test/main1.do ν˜Ήμ€ localhost:8080/test/main2.do

4.0.4. μœ„μ™€ 같은 uri둜 접속이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

 

uri 접속

 

μ½μ–΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.

μ§ˆλ¬Έμ€ μ–Έμ œλ‚˜ ν™˜μ˜ν•©λ‹ˆλ‹€.

 

"λ‚œ λ°˜λ“œμ‹œ λ°±μ—”λ“œ 왕이 될거야"