μ€νλ§μ κΈ°λ₯λ€μ€ νλμΈ DIμ AOPλ₯Ό μ¬μ©νλ €λ©΄ λ°λ‘ XML νμΌμμ μ€μ μ ν΄μΌνμ΅λλ€. νμ§λ§ XMLνμΌμ μ΄λ¬ν μ€μ λ€μ νκΈ°μ λ무λ 볡μ‘νκ³ μ΄λ €μ μ΅λλ€. λ°λΌμ μ€νλ§μμλ DI κ°μ μλ° μ½λμ κ΄λ ¨λ μ€μ μ μ½λμμ μ§μ ν μ μλλ‘ νμ΅λλ€. κ·Έ κΈ°λ₯μ μ λν μ΄μ (Annotation)μ΄λΌκ³ ν©λλ€.
νμ¬ μ€νλ§μμλ XMLνμΌ μ§μ μ€μ κ³Ό μ λν μ΄μ 2κ°λ₯Ό νΌν©ν΄μ μ¬μ©νκ³ μμ΅λλ€.
l μ€νλ§ μ λν μ΄μ μ 곡 ν΄λμ€
μ λν μ΄μ μ μ΄μ©νλ €λ©΄ XML νμΌμμ λ€μ 2κ°μ ν΄λμ€λ₯Ό λΉμΌλ‘ μ€μ ν΄μ£Όμ΄μΌ ν©λλ€.
ν΄λμ€ | κΈ°λ₯ |
DefaultAnnotationHandlerMapping | ν΄λμ€ λ 벨μμ @RequestMappingμ μ²λ¦¬ν©λλ€. |
AnnotationMethodHandlerMapping | λ©μλ λ 벨μμ @RequestMappingμ μ²λ¦¬ν©λλ€. |
l <context:component-scan> νκ·Έ
<context:component-scan base-package="ν¨ν€μ§ μ΄λ¦">
μ ν리μΌμ΄μ μ€ν μ ν¨ν€μ§ μ΄λ¦ λ΄μ μλ ν΄λμ€λ€μ μλμΌλ‘ λΉμΌλ‘ λ§λ€μ΄ μ€λλ€.
μ λν μ΄μ | κΈ°λ₯ |
@Controller | μ€νλ§ μ»¨ν μ΄λκ° component-scanμ μν΄ μ§μ ν ν΄λμ€λ₯Ό 컨νΈλ‘€λ¬ λΉμΌλ‘ μλ λ³νν©λλ€. |
@Service | μ€νλ§ μ»¨ν μ΄λκ° component-scanμ μν΄ μ§μ ν ν΄λμ€λ₯Ό μλΉμ€ λΉμΌλ‘ μλ λ³νν©λλ€. |
@Repository | μ€νλ§ μ»¨ν μ΄λκ° component-scanμ μν΄ μ§μ ν ν΄λμ€λ₯Ό DAO λΉμΌλ‘ μλ λ³νν©λλ€. |
@Component | μ€νλ§ μ»¨ν μ΄λκ° component-scanμ μν΄ μ§μ ν ν΄λμ€λ₯Ό λΉμΌλ‘ μλ λ³νν©λλ€. |
l μμ
<?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>
action-servlet.xmlμ μμ κ°μ΄ μ€μ ν΄μ€λλ€.
component-scan ν¨ν€μ§ λ΄μ μλ ν΄λμ€λ§μ΄ λΉμΌλ‘ μλ μ€μ λ©λλ€.
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;
}
}
@Controllerλ₯Ό μ΄μ©νμ¬ idκ° mainControllerλΌλ λΉμΌλ‘ μλ μ€μ λκ² ν΄μ€λλ€.
localhost:8080/test/main1.do νΉμ localhost:8080/test/main2.do
μμ κ°μ uriλ‘ μ μμ΄ κ°λ₯ν©λλ€.
μ½μ΄μ£Όμ μ κ°μ¬ν©λλ€.
μ§λ¬Έμ μΈμ λ νμν©λλ€.
"λ λ°λμ λ°±μλ μμ΄ λ κ±°μΌ"
'...' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Spring] @Autowired μ¬μ©νκΈ° (0) | 2021.01.11 |
---|---|
[Spring] @RequestParam μ¬μ©νκΈ° (0) | 2021.01.11 |
[MyBatis] λ§μ΄λ°ν°μ€ include ꡬ문 μ¬μ©νκΈ° (0) | 2021.01.10 |
[MyBatis] λ§μ΄λ°ν°μ€ foreach ꡬ문 μ¬μ©νκΈ° (0) | 2021.01.10 |
[MyBatis] λ§μ΄λ°ν°μ€ choose λ¬Έ μ¬μ©νκΈ° (0) | 2021.01.10 |