zoukankan      html  css  js  c++  java
  • spring4 之 helloworld

    1、从官网下载相关JAR包

    spring-framework-4.2.1.RELEASE-dist(下载地址:http://maven.springframework.org/release/org/springframework/spring/

    commons-logging-1.2-bin(下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi)

    2、新建JAVA PROJECT项目

    3、从项目中引入(1)内的JAR包

    4、SRC内新建2个类文件Helloworld.java & Main.java代码如下。

    package com.bai.spring.beans;
    public class Helloworld {
        private String name;
        public void setName(String name){
            this.name=name;
        }
        public void hello(){
            System.out.println("hello:"+name);
        }
    }
    package com.bai.spring.beans;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Main {
        public static void main(String []args){
            /*    不使用springde 情况
             * //创建一个HelloWorld对象
            HelloWorld helloworld=new HelloWorld();
            //为对象赋值
            helloworld.setName("baixl");
            //调用hello()方法
            */
            ApplicationContext ctx=new ClassPathXmlApplicationContext("SPRING.xml");
            Helloworld helloworld=(Helloworld) ctx.getBean("helloworld");
            helloworld.hello();
        }
    }

    5、新建SRPING.XML文件

    在SRC根目录建立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 -->
        <bean id="helloworld" class="com.bai.spring.beans.Helloworld">
            <property name="name" value="Spring"></property>
        </bean>
    </beans>

    6、RUN AS -> JAVA APPLICATION 运行。

    7、完成。正确结果显示如下

    十月 13, 2015 3:51:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19a5dff: startup date [Tue Oct 13 15:51:16 CST 2015]; root of context hierarchy
    十月 13, 2015 3:51:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [applicationcontext.xml]
    hello:Spring

    --------------------------------

    重点提示:

    1、在我们运行的时候很多时候会提示”Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogF“这个信息,很多时候是因为我们没有引用commons-logging-1.2-bin的JAR包,引用上即可。

    2、如上为SRPING的基本入门。

    运行流程:

    首先会运行main()语句,Spring框架使用ClassPathXmlApplicationContext()首先创建一个容器。
    这个容器从Beans.xml中读取配置信息,并根据配置信息来创建bean(也就是对象),每个bean有唯一的id。
    然后通过context.getBean()找到这个id的bean,获取对象的引用。
    通过对象的引用调用printMessage()方法来打印信息。
    好了,这是你的第一个Spring应用。你已经学会用Eclipse新建Java项目,导入Spring和commons-logging库,编写Java源代码和XML配置文件,并且成功运行了。如果要更改输出,只需要修改XML文件中的value值,而不需要更改Java源文件。

  • 相关阅读:
    在微信移动端input file拍照或从相册选择照片后会自动刷新页面退回到一开始网站进入的页面
    华为手机点击按钮跳转链接不生效!!!
    css content 如何自定义生成图标?
    快速上手制作Icon Font
    一款全兼容的播放器 videojs
    video.js使用教程API
    jquery ajax 请求参数详细说明 及 实例
    前端性能优化
    Bootstrap3 CSS样式基本用法总结
    h5页面的公共css
  • 原文地址:https://www.cnblogs.com/fan-yuan/p/4874904.html
Copyright © 2011-2022 走看看