SpringMVC
@ - Annotation
0. POM.xml API 다운로드
1. Tomcat start
2. web.xml에 기술된 내용을 보고 일을한다.
@RequestMapping - 요청 URL을 어떤 method가 처리할지 mapping해줌
@Controller - controller ( method의 반환결과를 JSON형태로 반환)
@Repository - DAO클래스, DB에 접근하는 method를 가지고있는 클래스에서 쓰임 (model)
★ DI -(의존성주입, Dependency Injection) - 의존성 약하게
각 객체간 의존성을 스프링컨테이너가 연결해주는것 ( bean(클래스)설정파일에 의존관계 정보추가)
setter주입방법, 생성자 주입방법
AOP??
@Autowired - 쓰고싶은 클래스 당겨쓰기 (목적 - setter방법, 생성자방법 쓰기위해) - inject로도 사용가능
●web.xml(환경설정파일)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
contextConfigLocation
Spring환경설정파일 web.xml - >/WEB-INF/spring/root-context.xml< DB connection기술하기
▲리딩 org.springframework.context.ContextLoaderListner ( 첫번째로시작)
▼리딩 org.springframework,web.servlet.DispatcherServlet (스프링에서 front controller역할) pojo는 만들어야함
contextConfigLocation
Spring환경설정파일 /WEB-INF/spring/appServlet/servlet-context.xml 뷰리졸버 등록, 컴포넌트scan
●servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="kr.smhrd.myapp" />
</beans:beans>
annotation-driven - @기호가붙어있는것들만 SpringContainer가 관리해줌
<resources mapping="/resources - resources의 위치를 mapping 하는부분
<bean ~ /bean> - viewResolver 하는부분 ( 내부에서 setter메소드 동작 )
component-scan - base-pakage에 써놓은곳을 스캔해서
@기호가있는 클래스를 자동으로 SpringContainer에 객체생성(클래스이름으로 )
( HomeController homecontroller = new HomeController 형식 )
*파일에 S기호가 있으면 스프링컨테이너에서 관리한다는 뜻
▼
◆당겨쓰기할때 예시
@Autowired
public HomeController homecontroller()
(변수명상관x , 타입맞추는게 중요)
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
model.addAttribute("name", "김민관" );
return "home";
}
}
@RequestMapping되어있으면 RequestmappingHandlerMapping정보가 console창에뜸(내부적으로 자동)
root-context.xml에 DB연결