programing

web.xml의 applicationContext.xml 파일 대신 Spring@Configuration 주석이 달린 클래스를 등록하는 방법은 무엇입니까?

linuxpc 2023. 9. 3. 12:13
반응형

web.xml의 applicationContext.xml 파일 대신 Spring@Configuration 주석이 달린 클래스를 등록하는 방법은 무엇입니까?

저는 웹 애플리케이션에서 jsf와 spring을 함께 사용하고 있습니다.다음과 같은 주석을 사용하는 하나의 구성 클래스에서 데이터 소스 및 세션 팩토리를 구성했습니다.@Configuration, @ComponentScan구성 클래스에서 컨텍스트 xml의 모든 항목을 처리하고 있기 때문에 프로젝트에 applicationContext.xml 파일이 없습니다.테스트 사례는 성공적으로 작동하지만 웹 애플리케이션을 배포할 때 오류가 발생합니다.

자바.java.java잘못된 상태 예외:웹 응용 프로그램 컨텍스트를 찾을 수 없습니다. ContextLoaderListener가 등록되지 않았습니까?

이제 web.xml에서 청취자 클래스를 제공하면,

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

그것은 나에게 오류를 줍니다.

/WEB-INF/applicationContext.xml을 찾을 수 없습니다.

의 문서에 따라ContextLoaderListener만약 내가 주지 않는다면 사실입니다.contextConfigLocation에 있어서의 변수.web.xml명시적으로 이름이 지정된 기본 스프링 컨텍스트 파일을 검색합니다.applicationContext.xmlweb.xml이제 스프링 컨텍스트 파일을 사용하지 않고 주석으로 모든 구성을 수행하려면 어떻게 해야 합니까?수신기 클래스를 등록하려면 어떻게 해야 합니까?ContextLoaderListenerxml 파일을 사용하지 않고 주석만 사용하여 spring 및 jsf로 웹 애플리케이션을 실행할 수 있습니까?

web.xml다음을 사용하여 컨텍스트를 부트스트랩해야 합니다.AnnotationConfigWebApplicationContext:

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            org.package.YouConfigurationAnnotatedClass
        </param-value>
    </init-param>
</servlet>

그리고 사용하는 것을 잊지 마세요.@EnableWebMvcMVC 주석을 시작할 수 있습니다.

추가 정보:

EDIT를 "순수한 후속 조치"로 => 튜링 완료:

네, 물론 청취자가 필요합니다.위의 내용은 "web.xml의 applicationContext.xml 파일 대신 Spring@Configuration 주석이 달린 클래스를 등록하는 방법"이라는 질문에 완전히 답하지만, 여기 전체를 레이아웃하는 Spring 공식 문서의 예가 있습니다.web.xml:

<web-app>
  <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
       instead of the default XmlWebApplicationContext -->
  <context-param>
      <param-name>contextClass</param-name>
      <param-value>
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
  </context-param>

  <!-- Configuration locations must consist of one or more comma- or space-delimited
       fully-qualified @Configuration classes. Fully-qualified packages may also be
       specified for component-scanning -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.acme.AppConfig</param-value>
  </context-param>

  <!-- Bootstrap the root application context as usual using ContextLoaderListener -->
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- Declare a Spring MVC DispatcherServlet as usual -->
  <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
           instead of the default XmlWebApplicationContext -->
      <init-param>
          <param-name>contextClass</param-name>
          <param-value>
              org.springframework.web.context.support.AnnotationConfigWebApplicationContext
          </param-value>
      </init-param>
      <!-- Again, config locations must consist of one or more comma- or space-delimited
           and fully-qualified @Configuration classes -->
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>com.acme.web.MvcConfig</param-value>
      </init-param>
  </servlet>

  <!-- map all requests for /app/* to the dispatcher servlet -->
  <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/app/*</url-pattern>
  </servlet-mapping>
</web-app>

여기서 오래된 질문을 제기하지만, Spring(v3.0+)의 최신 버전에서는 Servlet 3.0+를 지원하는 웹 컨테이너에 앱을 배포한다면 web.xml을 모두 제거할 수 있습니다.

스프링의 인터페이스를 구현하여 web.xml에서 수행하는 것과 동일한 구성을 수행할 수 있습니다.이 구현 클래스는 서블릿 3.0+ 컨테이너에서 실행되는 Spring 3.0+ 앱에 의해 자동으로 감지됩니다.

설정이 단순한 경우 아래와 같이 Spring에서 제공하는 다른 클래스를 사용할 수 있습니다.여기서 수행하는 모든 작업은 @Configuration 클래스를 설정하고 서블릿 매핑을 나열하는 것입니다.설정을 매우 단순하게 유지합니다.

public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {

        return new Class[] {AppConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {
                 "*.html"
                ,"*.json"
                ,"*.do"};
    }
}

언급URL : https://stackoverflow.com/questions/8075790/how-to-register-spring-configuration-annotated-class-instead-of-applicationcont

반응형