zoukankan      html  css  js  c++  java
  • Spring-demo1(初学者的尝试,2015.03.19)

    项目结构:


    源代码如下:
    1 package com.bean;
    2 public interface Person {
    3     public void Speak();
    4 }
     1 package com.bean;
     2 public class AmericanImpl implements Person {
     3     private String name;
     4     private int age;
     5     
     6     public String getName() {
     7         return name;
     8     }
     9     public void setName(String name) {
    10         this.name = name;
    11     }
    12     public int getAge() {
    13         return age;
    14     }
    15     public void setAge(int age) {
    16         this.age = age;
    17     }
    18     
    19     @Override
    20     public void Speak() {
    21         // TODO Auto-generated method stub
    22         System.out.println("I'm American,My name is" + this.name + ",I'm" + this.age + " years old!");
    23     }
    24 }
    1.  1 package com.bean;
       2 public class ChineseImpl implements Person {
       3     private String name;
       4     private int age;
       5     
       6     public String getName() {
       7         return name;
       8     }
       9     public void setName(String name) {
      10         this.name = name;
      11     }
      12     public int getAge() {
      13         return age;
      14     }
      15     public void setAge(int age) {
      16         this.age = age;
      17     }
      18     @Override
      19     public void Speak() {
      20         // TODO Auto-generated method stub
      21         System.out.println("I'm Chinese,My name is" + this.name + ",I'm" + this.age + " years old!");
      22     }
      23 }
     
    1.  1 package com.spring;
       2 import org.springframework.context.ApplicationContext;
       3 import org.springframework.context.support.ClassPathXmlApplicationContext;
       4 import com.bean.Person;
       5 public class Test {
       6     public static void main(String[] args) {
       7         // TODO Auto-generated method stub
       8         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       9         Person person = (Person)context.getBean("chinese");
      10         person.Speak();
      11         person = (Person)context.getBean("american");
      12         person.Speak();
      13     }
      14 }

    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 中挣扎!
     





    附件列表

  • 相关阅读:
    Django view(视图)
    Django ORM
    Django 路由系统(URLconf)
    Django简介
    Ubuntu 18.04安装MySQL指南
    一只简单的网络爬虫(基于linux C/C++)————配置文件设计及读取
    一只简单的网络爬虫(基于linux C/C++)————开篇
    单例模式及单例类的两种实现
    对象析构不析构?
    C++11的mutex和lock_guard,muduo的MutexLock 与MutexLockGuard
  • 原文地址:https://www.cnblogs.com/zhanyao/p/4350704.html
Copyright © 2011-2022 走看看