2011-12-16 10 views
1

recent Spring 3.1 Releaseには、@WebServletアノテーションのサポートがあります。しかし、私はそれを使用することはできないようです。そのためのチュートリアルはありますか?これは私のWeb.xmlSpring 3.1およびServlet API 3.0。 @ WebServletアノテーションの使い方

<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0" > 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/beans.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 
</web-app> 

...と私のbeans.xmlの

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation=" 
http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 


    <context:annotation-config /> 
    <context:component-scan base-package="xxx.xxxxxxxx.xxx" /> 

    <!-- aop:aspectj-autoproxy aspectj-weaving="on"/--> 

    <bean 
     class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> 

    <!-- bean class="xxx.xxxx.xxx.xxx.MessageProfileLogger" 
     /--> 
</beans> 

サーブレットのソース...

@WebServlet(urlPatterns = { "/service/*" }) 
public class RequestServlet extends HttpServlet { 
    { 
     System.out.println("RequestServlet is initialized"); 
    } 
... 
} 

必要な動作である - 注釈付きサーブレットが初期化されます要求を待ち受けます。

実際には何も起こりません。サーブレットに注釈が付けられていないようなものです。

追加情報 - サーブレットへのパスはコンポーネントスキャンにあり、他のDI機能も有効です。

私はエンタープライズコンテナを持っていません。今まで働いていたSpring+Embeded Jettyのためにこのチュートリアルを使いました。

EDIT:以前のバージョンのJetty(7.0.2)を使用していたようです。桟橋は、コメントを1として、バージョン8

+0

"私は使用できません"と正確にはどういう意味ですか?代わりに正確に何が起こるのですか? – BalusC

+0

@BalusCそうです、私は質問を編集します。 – fiction

+0

ええと、私は春をしないので、 'AbstractServlet'が正しいかどうかは分かりませんが、これは普通のServlet API(通常は' HttpServlet'です)です。しかし、Tomcat 7やGlassfish 3などのServlet 3.0互換コンテナを実行していますか? – BalusC

答えて

4

以来@WebServlet 3.0をサポートしているようだ:

桟橋7.0.2

サーブレット3.0 APIを利用することができるようにするためには、次のものが必要Servlet 3.0 APIをサポートするコンテナ。少なくともTomcat 7、Glassfish 3、JBoss AS 6、WebSphere AS 8、Jetty 8などです。Servlet 3.0に準拠するようにweb.xmlだけを変更しても、Servlet 3.0 APIを魔法のようにサポートするようにコンテナの内部クラスは変更されません。ほとんどのコンテナは、サポートされていないバージョンがweb.xmlに記述されている場合、互換性モードを最小にすることさえあります。

埋め込みJetty 8を使用している場合は、注釈をスキャンするためにJetty configにAnnotationConfigurationを追加することを忘れないでください。

関連する問題