Declaration

Dev.Hammy·2024년 4월 2일
0

반응형 스택에서 이에 상응하는 내용 보기

Servlet의 WebApplicationContext에서 표준 Spring Bean 정의를 사용하여 컨트롤러 Bean을 정의할 수 있습니다. @Controller 스테레오타입은 클래스 경로에서 @Component 클래스를 감지하고 이에 대한 Bean 정의를 자동 등록하기 위한 Spring의 일반 지원에 맞춰 자동 감지를 허용합니다. 또한 웹 구성 요소로서의 역할을 나타내는 주석이 달린 클래스에 대한 스테레오타입 역할도 합니다.

이러한 @Controller Bean의 자동 감지를 활성화하려면 다음 예제와 같이 Java 구성에 구성 요소 검색을 추가할 수 있습니다.

@Configuration
@ComponentScan("org.example.web")
public class WebConfig {

	// ...
}

다음 예에서는 앞의 예와 동일한 XML 구성을 보여줍니다.

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		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">

	<context:component-scan base-package="org.example.web"/>

	<!-- ... -->

</beans>

@RestController는 모든 메서드가 유형 수준 @ResponseBody annotation을 상속하는 컨트롤러를 나타내기 위해 @Controller@ResponseBody로 자체적으로 메타 annotation이 붙은 composed annotation입니다. 따라서 응답 본문과 뷰 resolution 및 HTML 템플릿 렌더링에 직접 씁니다.

AOP Proxies

반응형 스택에서 이에 상응하는 내용 보기

어떤 경우에는 런타임에 AOP 프록시로 컨트롤러를 장식해야 할 수도 있습니다. 한 가지 예는 컨트롤러에 @Transactional annotation을 직접 포함하도록 선택한 경우입니다. 이 경우 특히 컨트롤러의 경우 클래스 기반 프록시를 사용하는 것이 좋습니다. 이는 컨트롤러에 직접 annotation이 있는 경우 자동으로 발생합니다.

컨트롤러가 인터페이스를 구현하고 AOP 프록시가 필요한 경우 클래스 기반 프록시를 명시적으로 구성해야 할 수도 있습니다. 예를 들어 @EnableTransactionManagement를 사용하면 @EnableTransactionManagement(proxyTargetClass = true)로 변경할 수 있고, <tx:annotation-driven/>를 사용하면 <tx:annotation-driven Proxy-target-class="true"/>로 변경할 수 있습니다.

[Note]
6.0부터 인터페이스 프록시를 사용하면 Spring MVC는 더 이상 인터페이스의 유형 수준 @RequestMapping annotation에만 기반한 컨트롤러를 감지하지 않습니다. 클래스 기반 프록시를 활성화하세요. 그렇지 않으면 인터페이스에도 @Controller annotation이 있어야 합니다.

0개의 댓글