zoukankan      html  css  js  c++  java
  • Spring配置与第一Spring HelloWorld

              林炳文Evankaka原创作品。

    转载请注明出处http://blog.csdn.net/evankaka

            本文将主讲了Spring在Eclipse下的配置,并用Spring执行了第一个HelloWorld.

    一、下载须要的文件

    这里我们已经配置好Java的执行环境和装好Eclipse了。


    下载Spring

    下载地址:http://maven.springframework.org/release/org/springframework/spring/

    下载commons-logging

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

    将它们下载后解压到自己想放的位置,下载之前记得要看清楚是32位还是64位

    二、配置Spring

    1、新建一个project,就叫SpringHelloworld。

    2、加入Spring3.x的包。网上有非常多不同的方法。这里我仅仅讲一种。

    在Window->Preferences->Java->Build Path->User Libraries->New加入一个用户包的库,这里这么做的原因是Spring包比較多,我们这样做,配置一次后,以后每一个project要用直接加入该库即可了


    命名为Spring3.2,点击OK


    点击Add External JARS.在跳出的窗体中选择Spring libs的包所在的位置(看你的解压位置),把用到的JAR都加入进来


    加入成功后

    加入到project中来:

    选择新建的project-》Properties->Java Build Path->Add library

    在跳出的窗体中选择User Library

    然后又会跳出一个窗体,这时就能够选择我们之前配置的用户库的包Spring3.2了,把沟打上。


    加入成功

    然后project中就能够看到加入进来的Spring3.2了

    三、加入commons-logging

    选择project-》Properties->Java Build Path->Add library

    然后选择commons-logging所在的包就能够了

    加入成功了

    四、開始Spring编程

    本文project下载

    好了,上面的配置都弄好后,我们就能够開始第一个HelloWorld了

    1.首先在当前包下新建一个HelloWorld.java

    package com.test;
    /**
     * Spring第一个HelloWorld
     * @author 林炳文(邮箱ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
     * @time 2015.4.1
     */
    public class HelloWorld {
    	private String info;
    
    	public String getInfo() {
    		return info;
    	}
    
    	public void setInfo(String info) {
    		this.info = info;
    	}
    	
    
    }
    

    2、编写配置文件applicationContext.xml

    在当前project下

    这就是加入成功后的

    然后把applicationContext.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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置须要被Spring管理的Bean(创建,创建后放在了Spring IOC容器里面)--> <bean id="hello" class="com.test.HelloWorld"> <!-- 配置该Bean须要注入的属性(是通过属性set方法来注入的) --> <property name="info" value="Happy New Year!"/> </bean> </beans>

    3、反转控制開始

    在Main.java中加入例如以下:

    /**
     * Spring第一个HelloWorld
     * @author 林炳文(邮箱ling20081005@126.com 博客:http://blog.csdn.net/evankaka)
     * @time 2015.4.1
     */
    package com.test;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Main {
    
    	private String who = null;
    
    	public static void main(String[] args) {
    		//获取Spring的ApplicationContext配置文件,注入IOC容器中
    		//(Map: key:String, bean标签的id属性值 ==>value:Object, bean标签class属性所指类的实例)
    		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
    		HelloWorld hw1 = (HelloWorld)factory.getBean("hello");//map.get("hello")
    		System.out.println(hw1.getInfo());
    		System.out.println(hw1);
    
    	}
    }
    

    然后选择project右键:

    接下来就是输出结果啦:


      本文project下载

    林炳文Evankaka原创作品。转载请注明出处http://blog.csdn.net/evankaka

    版权声明:这篇文章的博客林炳文Evankaka原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    大四实习有点晚[转载]
    .net2.0数据绑定语法
    明天要去南京了
    Master & Content Page Relation(Event Ordering)
    在验证中使用图像和声音(ErrorMessage)
    设置FLash透明
    Basic Skill in .net2.0
    教育研究方法
    程序员是如何捕猎大象的[转]
    My lost card
  • 原文地址:https://www.cnblogs.com/yxwkf/p/4720741.html
Copyright © 2011-2022 走看看