新新新新新新到不行的新手手向:Spring在Eclipse中的配置方法

一、软件准备

Spring Framework:

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

为了能够使测试类能在控制台输出结果我们还需要下载commons-logging,

下载地址如下:

http://commons.apache.org/proper/commons-logging/download_logging.cgi

二、软件安装

将下载好的Spring-framework 解压到固定文件夹如:H:\java\Spring

如图为解压完成的文件夹内容:

commons-logging同上 我把他们都放在了H:\java文件夹中

   

三、配置Eclipse:

打开Eclipse-window-Preference

找到java-Build Path-User Libraries


填入自己的库名:



添加上spring离线包里libs内的jar包以及下载的commons-logging  jar包,完成后我们就可以测试我们的项目了。

四、测试项目

我们需要几个类来完成测试

在测试之前我们需要把测试代码的类和配置文件建好:

首先新建java project


然后在src文件夹中新建几个类:


(这里的Person上面的小S是我添加完bean Xml之后出现的,)

然后在src文件夹下新建bean.xml


完成后是这个样子的:


然后需要配置项目类库:

右键项目properties-java Build Path选择Add Library


选择userlibrary:


然后选定我们刚才添加的类库就好了

Person类代码

<span style="font-size:18px;">package com.test.bean;
public class 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;
    }
    public void info(){
        System.out.println("一起来吃麻辣烫!");
        System.out.println("name:"+getName()+" age:"+getAge());
    }
}</span>

test类 代码

<span style="font-size:18px;">package testSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.bean.Person;
public class test {
    public static void main(String[] args){
     ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//读取bean.xml中的内容
     Person p = ctx.getBean("person",Person.class);
//创建bean的引用对象
     p.info();
    }
}
</span>

然后是bean.xml 中的代码

<span style="font-size:18px;"><?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 id="person" class="com.test.bean.Person">
              <property name="name" value="xingoo"/>
              <property name="age" value="12"/>
          </bean>
</beans>
</span>

然后跑起来的输出结果是这样的:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值