什么是最简单的Spring(3)应用程序可能有效?

时间:2014-04-15 00:20:24

标签: java spring

我正在使用Spring 3并尝试创建可能有效的最基本的应用程序。

这是我到目前为止所做的:

HeyDude.java

package test;                                                                      

import org.springframework.stereotype.Controller;                               
import org.springframework.web.bind.annotation.RequestMapping;                  
import org.springframework.web.bind.annotation.RequestParam;                    
import org.springframework.web.bind.annotation.ResponseBody;                    

@Controller                                                                     
public class HeyDude {                                                          

    @RequestMapping("/hello")                                                   
    public @ResponseBody String hello () {                                      
        return "Such cool. Many awesome.";                                      
    }                                                                           
}  

的web.xml

<web-app version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http:/java.sun.com/dtd/web-app_2_3.dtd">

    <display-name>Spring test for awesome!</display-name>                       
    <servlet>                                                                   
        <servlet-name>spring</servlet-name>                                     
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>                                                                  

    <servlet-mapping>                                                           
        <servlet-name>spring</servlet-name>                                     
        <url-pattern>/hello</url-pattern>                                       
    </servlet-mapping>                                                          
<!--                                                                            
-->                                                                             
</web-app> 

的build.xml

<property name="name" value="api"/>                                         
<property name="lib.dir" value="${name}/WEB-INF/lib"/>                      
<property name="build.dir" value="${name}/WEB-INF/classes"/> 

<target name="testbuild" description="Compile main source tree java files"> 
    <mkdir dir="${lib.dir}"/>                                                  
    <copy todir="${lib.dir}" preservelastmodified="true">                      
        <fileset dir="/usr/share/tomcat7/lib/">                                
            <include name="servlet-api.jar"/>                                  
        </fileset>                                                             
        <fileset dir="/path/to/other/libs">
            <include name="spring-core-3.2.2.RELEASE.jar" />                   
            <include name="spring-web-3.2.2.RELEASE.jar" />                    
            <include name="spring-context-3.2.2.RELEASE.jar" />                
            <include name="spring-servlet-3.2.2.RELEASE.jar" />                
        </fileset>                                                             
    </copy>                                                                    
    <javac destdir="${build.dir}" source="1.6"                                 
        includeantruntime="false"                                              
        target="1.6" debug="true"                                              
        deprecation="false" optimize="false" failonerror="true">               
        <src path="${build.dir}/test"/>                                        
        <classpath refid="master-classpath"/>                               
    </javac>                                                                
</target>

<target name="testwar" depends="testbuild" description="Build a WAR">          
    <war basedir="api/" warfile="my-test.war">                         
        <exclude name="${build-dir}" />                                     
        <exclude name="downloads/*" />                                      
    </war>                                                                  
</target>  

然而,我仍然收到以下错误:

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:701)

我需要做些什么才能让它发挥作用,为什么?或者,如果我能创建一个更简单的应用程序,我很乐意看到它!

2 个答案:

答案 0 :(得分:1)

首先,你缺少一些依赖项( spring-beans spring-context-supprt ,可能还有 spring-expression )有不存在的依赖项( spring-servlet ,可能必须是 spring-webmvc )。

接下来你的@Controller错误,它当前映射到/hello,这将导致网址/hello/helloDispatcherServlet永远不会触及它,因为它映射到{ {1}}而不是/hello但我希望您希望它映射到/hello/*,以便将您的servlet映射到/hello/

最后我强烈建议你再次开始看Maven(或Gradle)。您不想在互联网上搜索需要包含在项目中的罐子。如果你真的想继续使用ant,至少要查看Ivy的依赖关系管理。

对于maven,以下就足够了(并且大致是/*文件的翻译。)

build.xml

当您执行<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>my-test</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.8.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- Provided Dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project> 时,这将下载所有necesarry依赖项并创建war文件。

答案 1 :(得分:1)

微米。 Deinum的回答让我想到了我想要的东西:

将这些依赖项添加到 build.xml

            <include name="spring-core-3.2.2.RELEASE.jar" />                
            <include name="spring-web-3.2.2.RELEASE.jar" />                 
            <include name="spring-context-3.2.2.RELEASE.jar" />             
            <include name="spring-context-support-3.2.2.RELEASE.jar" />     
            <include name="spring-webmvc-3.2.2.RELEASE.jar" />              
            <include name="spring-beans-3.2.2.RELEASE.jar" />               
            <include name="org_apache_commons_logging.jar" />               
            <include name="spring-expression-3.2.2.RELEASE.jar" />  

我更改了 web.xml

<display-name>Spring test for awesome!</display-name>                       
<servlet>                                                                   
    <servlet-name>spring</servlet-name>                                     
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>                                    
</servlet>                                                                  

<servlet-mapping>                                                           
    <servlet-name>spring</servlet-name>                                     
    <url-pattern>/*</url-pattern>                                           
</servlet-mapping>       

使用正确的依赖关系,它暴露了 spring-servlet.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                

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                   


http://www.springframework.org/schema/context                                      


http://www.springframework.org/schema/context/spring-context-3.0.xsd">             

    <context:component-scan base-package="test" />                                 
</beans>     

HeyDude.java 现在看起来像这样:

@Controller                                                                        
public class HeyDude {                                                             

    @RequestMapping("/")                                                           
    public @ResponseBody String hello () {                                         
        return "Such cool. Many awesome.";                                         
    }                                                                              

}    

所以Spring有几个要求(包括Apache commons logging)。 Spring还需要spring-servlet.xml,其中可能需要context:component-scan元素(与其他地方相比。这绝对是必需的)

这是我能提出的最简单的Spring应用程序。