项目结构:


源代码如下:
 package com.bean;
public interface Person {
public void Speak();
}
 package com.bean;
public class AmericanImpl implements Person {
private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} @Override
public void Speak() {
// TODO Auto-generated method stub
System.out.println("I'm American,My name is" + this.name + ",I'm" + this.age + " years old!");
}
}
  1.  package com.bean;
    public class ChineseImpl implements Person {
    private String name;
    private int age; public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    @Override
    public void Speak() {
    // TODO Auto-generated method stub
    System.out.println("I'm Chinese,My name is" + this.name + ",I'm" + this.age + " years old!");
    }
    }
 
  1.  package com.spring;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.bean.Person;
    public class Test {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Person person = (Person)context.getBean("chinese");
    person.Speak();
    person = (Person)context.getBean("american");
    person.Speak();
    }
    }

applicationContext.mxl 是在 src 的目录下,文件代码如下:
<?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-3.2.xsd ">
  <!-- 配置 Chinese实例,其实现类为com.bean.ChineseImpl -->
  <bean id="chinese" class="com.bean.ChineseImpl">
    <property name="name">
      <value>小明</value>
    </property>
    <property name="age">
      <value>10</value>
    </property>
  </bean>
  <bean id="american" class="com.bean.AmericanImpl">
    <property name="name">
      <value>Tom</value>
    </property>
    <property name="age">
      <value>15</value>
    </property>
  </bean>
</beans>

下面问题来了:
遇到问题1:
上面的代码是正确的,我之前由于是用 eclipse 配置的 spring-beans-3.2.xsd;
然后我创建 xml 时引用 xsd,elipse 生成的代码如下,缺少了上面黄色部分的代码
<?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 "> </beans>

其余代码正确,会报如下错误:
 Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 1 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/beans/; lineNumber: 1; columnNumber: 55; 在 publicId 和 systemId 之间需要有空格。
    ……
Caused by: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/beans/; lineNumber: 1; columnNumber: 55; 在 publicId 和 systemId 之间需要有空格。
    …… 

这些 jar 包都是必须的,缺一不可;

Spring开发一定会引入下面这两个 jar 包:

pring-core.jar:
这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类
spring-beans.jar:
这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。
如果应用只需基本的IoC/DI支持,引入spring-core.jar及spring- beans.jar文件就可以了
 

下面问题来了,缺了会报什么错呢?

ApplicationContext cannot be resolved to a type    

ClassPathXmlApplicationContext cannot be resolved to a type
这是因为缺少了
 
spring-context.jar:
这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类,以及校验Validation方面的相关类

加入这三个jar包之后,运行报如下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    ……
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    ……
这是因为缺少了
 
commons-logging-1.2.jar 
Apache Commons包中的一个,包含了日志功能,必须使用的jar包。

现在加入了四个 jar 包,运行报如下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor
    ……
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.PropertyAccessor
    …… 
这是因为缺少了
 
spring-expression.jar:Spring表达式语言

好了,现在终于可以运行得到结果了:

 
 渣渣就得在 Bug 中挣扎!
 

附件列表

Spring-demo1(初学者的尝试,2015.03.19)的更多相关文章

  1. spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

    下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...

  2. iOS 学习笔记 六 (2015.03.28)常见错误

    2015.03.28 1. property's synthesized getter follows Cocoa naming convention for returning 'owned' ob ...

  3. Contest2071 - 湖南多校对抗赛(2015.03.28)

    Contest2071 - 湖南多校对抗赛(2015.03.28) 本次比赛试题由湖南大学ACM校队原创 http://acm.csu.edu.cn/OnlineJudge/contest.php?c ...

  4. Spring 之定义切面尝试(基于 XML)

    有些场景下只能基于 XML 来定义切面. [Spring 之定义切面尝试] 1.XML 下定义切面(首先是要有一个对应的类...显然要比基于注解的麻烦) <?xml version=" ...

  5. Spring 之定义切面尝试(基于注解)

    [Spring 之定义切面尝试] 1.标记为深红色的依赖包是必须的 <dependency> <groupId>org.springframework</groupId& ...

  6. 致Spring Boot初学者

    1.引言 Spring Boot是近两年来火的一塌糊涂,来这里的每一位同学,之前应该大致上学习了web项目开发方面的知识,正在努力成长过程中.因为最近有不少人来向我“请教”,他们大都是一些刚入门的新手 ...

  7. spring boot docker 初尝试

    Docker服务中进程间通信通过/var/run/docker.sock实现,默认服务不提供监听端口,因此使用docker remote api 需要手动绑定端口. 在centos7.2下,可以进行这 ...

  8. Spring IOC 源码简单分析 03 - 循环引用

    ### 准备 ## 目标 了解 Spring 如何处理循环引用 ##测试代码 gordon.study.spring.ioc.IOC03_CircularReference.java   ioc03. ...

  9. 2015.04.19,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 29

    1.the French drillmaster 法国国王路易十五手下的Jean Martinet将军,是Infantry(['infәntri] n. 步兵)的检察长,是一个非常严格的drillma ...

随机推荐

  1. 4.MVC框架开发(母版页的应用、按钮导致的Action处理、从界面向控制器传数据和HtmlHelper控件的实现(注册的实现))

    1.在视图里如何引入母版页 1)在视图里母版页都是放在View目录下面的Shared文件夹下面 2)母版页里的RenderBody()类似于ASP.NET里面的ContentPalceHolder占位 ...

  2. 我的PHP之旅--PHP的函数初步认识

    函数 函数主要是将一块代码封装起来方便多次使用,方便以后维护,节省代码. 先看一个简单的函数: <?php function myFirstFunc(){ echo "Hello PH ...

  3. sjtu1585 oil

    Description Crystal家的公司最近承包了一个大油田.整块油田为一个矩形区域,被划分为\(n \times m\)个小块. Crystal亲自调查了每个小块的石油储备量.这些数据表示为\ ...

  4. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

    点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...

  5. 利用动软代码生成器 自动生成LINQ需要用的数据实体类 (转)

    首先先建立一个模板 名称随意 我起的“生成数据实体.cmt” 代码如下: <#@ template language="c#" HostSpecific="True ...

  6. *[codility]Number-of-disc-intersections

    http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件, ...

  7. 判断微信内置浏览器的UserAgent

    要区分用户是通过"微信内置浏览器"还是"原生浏览器"打开的WebApp, 可以通过navigator.userAgent来进行判断. 以下是对各种平台上微信内置 ...

  8. Formatting is Specified but argument is not IFormattable

    private void DeviceSetText(TextBox textBox, string text) { //处理text的显示值 ") //小数位后保留2位 { //小数点后保 ...

  9. Import Items – Validation Multiple Languages Description

            ð  提交标准请求创建和更新物料,因语言环境与处理次序方式等因素,造成物料中英(更多语言)描述和长描述混乱刷新. 症状: >>> Submit Standard Op ...

  10. bzoj1797

    其实我觉得这种题目风格很像今天省选第三轮D1T1 都是在一个算法模型上去探索规律: 首先我们要做一遍最大流毫无疑问 第一问看起来很好想,只要是满流边就可以了? 错,反例不难找到 如:1--->2 ...