Spring 使用 STS 3.6.4 的示例

Spring Example using STS 3.6.4

来自 Spring Tool Suite 3.6.4 我正在尝试开发一个 Spring 项目 我已经完成了所有工作,例如添加存在于 STS lib 文件夹中的 jars 和 apache common-logging 1.1.3 以及1.2.

STS 在主 class 中显示错误 新建ClassPathXmlApplicationContext("Beans.xml");

当我尝试解决它时,STS 正在显示配置 BuildPath 我已经通过观看 Youtube 中的教程添加了所有 jar。

我使用的是JAVA1.6版本,OSUbuntu,STS 3.6.4 下面列出的罐子我已经添加到构建路径

/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/aws-java-sdk-1.7.5.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.beanutils-1.8.3.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.cli-1.2.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.codec-1.5.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.collections-3.2.1.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.configuration-1.8.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.digester-1.8.1.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.jxpath-1.3.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.lang-2.6.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.apache.commons.logging-1.1.1.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.bouncycastle.jce-1.46.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.bouncycastle.mail-1.46.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/com.springsource.org.jdom-1.0.0.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/commons-codec-1.6.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/commons-logging-1.1.3.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/httpclient-4.3.3.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/httpcore-4.3.2.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/jackson-annotations-2.1.1.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/jackson-core-2.1.1.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/jackson-databind-2.1.1.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/joda-time-2.7.jar
/home/axxera/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/lib/tc-runtime-instance-3.1.0.RELEASE.jar
/home/axxera/Venkat/Libraries/org-apache-commons-logging.jar
/home/axxera/Venkat/Libraries/org.springframework.context-3.0.4.RELEASE.jar
/home/axxera/Venkat/Libraries/spring-context-4.1.5.RELEASE.jar
/home/axxera/Venkat/Libraries/jbehave-spring-3.6.4.jar
/home/axxera/Venkat/Libraries/commons-logging-1.2/commons-logging-1.2.jar
/home/axxera/Venkat/Libraries/commons-logging-1.2/commons-logging-1.2-javadoc.jar

下面是我的代码

主要Class

    package com.spring;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main {

        public static void main(String[] args) {

           ClassPathXmlApplicationContext appContext = 
                 new ClassPathXmlApplicationContext("Beans.xml");

       }

     }


    package com.spring;

    public class SpringSample {

       void printMessage() {

        System.out.println("Hello World");
       }
    }

Beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="SampleBean" class="com.spring.SpringSample"></bean>

    </beans>

看来您错过了 spring-beans-<version>.jar。下载它并将其添加到您的类路径中。