zoukankan      html  css  js  c++  java
  • 配置运行Spring 入门级Demo 和常见故障解决 (Spring in Action)

    spring demo编写过程中可以遇到以下错误

    避免常见故障:

    (1)java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

    Spring依赖于Apache的通用日志组件包,下载并加入到build path

    (2) No default constructor found

    为实现类田间无参数构造函数

    (3) java.io.FileNotFoundException

    配置文件的路径有问题,放在工程的根目录可避免

    1.编写接口类

    package com.springinaction.chapter1.hello;

     

    public interface GreetingService{

        void sayGreeting();

    }

    2.编写实现类

    package com.springinaction.chapter1.hello;

     

    public class GreetingServiceImpl implements GreetingService {

     

        private String greeting;

     

        // 一个无参数的构造函数

        public GreetingServiceImpl() {

     

        }

     

        public GreetingServiceImpl(String greeting) {

           this.greeting = greeting;

        }

     

        public void sayGreeting() {

           System.out.println(greeting);

        }

     

        public void setGreeting(String greeting) {

           this.greeting = greeting;

        }

    }

    3.编写依赖注入配置文件:

    <?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-2.0.xsd">

      <bean id="greetingService"

          class="com.springinaction.chapter1.hello.GreetingServiceImpl">

        <property name="greeting" value="Buenos Dias!" />

      </bean>

    </beans>

    4..主函数

    package com.springinaction.chapter1.hello;

     

    import org.springframework.beans.factory.BeanFactory;

    import org.springframework.beans.factory.xml.XmlBeanFactory;

    import org.springframework.core.io.FileSystemResource;

     

    public class HelloApp {

        public static void main(String[] args) {

     

           BeanFactory factory = new XmlBeanFactory(new FileSystemResource(

                  "config.xml"));

     

           GreetingService service = (GreetingService) factory

                  .getBean("greetingService");

     

           service.sayGreeting();

        }

    }

    运行结果:

    Buenos Dias!

     

    5.为了使程序能够正常运行需要注意一下几点

    (1) 设置好build path:加入spring包的dist下的jar

    (2) 由于spring还依赖于apachecommon logging,还要加入commons-logging包的jar

    (3) 配置文件的位置应该放在工程的根目录,否则文件路径不能这么写,会找不到文件发生IOException.

    (4) 要为具体的实现类提供一个无参数的构造函数

    配置好以上,一个一个简单的spring demo就完成了.

     

    注意编程方式的特点.首先要编写一个接口类.然后真实的业务逻辑在实现接口的实体类中完成.

    运行结果:在主函数中的程序设计完全面向接口编程,和具体实现完全没有依赖关系.依赖的注入通过读取config.xml文件获得.并通过springBeanFactory解析得到具体的实现.如果实现类更改了,只需要重新编译实现类和重新配置文件config.xml即可.完全面向接口编程的测试主函数类不需要重新编译.

  • 相关阅读:
    'Undefined symbols for architecture i386,clang: error: linker command failed with exit code 1
    The codesign tool requires there only be one 解决办法
    XCode iOS project only shows “My Mac 64bit” but not simulator or device
    Provisioning profile XXXX can't be found 的解决办法
    UIView 中的控件事件穿透 Passthrough 的实现
    Xcode4.5出现时的OC新语法
    xcode 快捷键(持续更新)
    打越狱包
    php缓存与加速分析与汇总
    浏览器的判断
  • 原文地址:https://www.cnblogs.com/oyjj/p/2132936.html
Copyright © 2011-2022 走看看