Eclipse搭建Spring框架(绝对成功!)

花了许久才把Spring框架搭建完成,一把辛酸泪。。。。。。。。。。

一、首先下载好全部所需要的工具和jar包

1 安装 springsource-tool-suite:

(1)打开eclipse-help-marketplace

search中输入spring,下载

按install,这里我已经下载好了

**(2)**安装

然后点击下一步知道最后完成安装,重启Eclipse。

.最后Windows下的Preferences显示Spring选项即为安装成功。

****(**3)**下载springframework包

http://repo.spring.io/release/org/springframework/spring/4.3.9.RELEASE/spring-framework-4.3.9.RELEASE-dist.zip

****(**4)**下载spring依赖的日志包commons-logging

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

****(5) 将(3)和(4)解压后将相关jar包放在工程的lib目录下。

  • 编写Spring的配置文件,该配置文件模板可以从Spring的参考手册或Spring的例子中得到。配置文件的取名可以任意,文件可以存放在任何目录下,但考虑到通用性,一般放在类路径下。
    在spring-framework-4.2.5.RELEASEdocsspring-framework-referencehtmlsingle目录中打开index.html,以关键字**<bean**搜索,可查找到如下配置信息:

    <?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="..." class="...">
            <!-- collaborators and configuration for this bean go here -->
        </bean>
    
        <bean id="..." class="...">
            <!-- collaborators and configuration for this bean go here -->
        </bean>
    
        <!-- more bean definitions go here -->
    
    </beans>
    

    在spring_first的src目录下新建一个beans.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">
    
    </beans>
    
  • 编写测试代码,在src目录下新建一个junit.test包,并在该包下新建一个JUnit Test Case。
    这里写图片描述

    这里写图片描述
    在SpringTest.java的文件中加入测试代码,如下:

    public class SpringTest {
    
        @Test
        public void test() {
            // ApplicationContext是接口
            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 实例化Spring容器
        }
    
    }
    

    以上代码为实例化Spring容器。实例化Spring容器常用的两种方式:

    • 方法一:在类路径下寻找配置文件来实例化容器。

      ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
      
    • 方法二:在文件系统路径下寻找配置文件来实例化容器。

      ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"d:\beans.xml"});
      

    注意:Spring的配置文件可以指定多个,可以通过String数组传入

  • 运行test方法,Eclipse控制台打印如下:
    这里写图片描述
    可看到Eclipse控制台没有错误信息,即表示在Eclipse中搭建Spring4.2.5开发环境成功!!!

再一例子:

一、首先需要新建一个工程(我新建的是web工程):

file–>new–>other–>dynamic ** **,如图:

点击红色方框内容后,出现如下页面:

输入project name后一直点击Next,到最后一步时,建议把红色方框中的内容勾选起来,这样建好的工程会自带web.xml:

然后点击finish结束,刚创建好的工程结构如下图:

二、导入相关jar包,加入配置文件

搭建Spring框架,当然需要加入Spring的jar包,在WEB-INF的lib文件夹中复制spring框架所需的jar包(jar可以从网上或者是官网上下载),同时,还需要添加common-log.jar包,加入jar包后最好add一下:

选中lib下的所有jar包,然后右键–>build path–>add to build path

接下来就可以新建spring需要的配置文件了:

选中src文件夹,右键–>file:

创建完成后会多出一个xml文件,工程结构如图:

这个applicationContext.xml文件的配置先放着不管,我们先来建一个实体类

三、创建实体类,完整<在这是个动词>applicationContext.xml

3.1.以下是实体类:

新建实体类后就可以配置applicationCpontext.xml文件了:

3.2.以下是完整的applicationContext.xml文件,代码在图下面附上

xml文件中代码如下:

注意:

1.需要添加

2.上图中有书写错误的地方,各位客官请参考下面具体代码

[java] view plain copy

  1. <spanstyle=“font-size:16px;”><xmlversion="1.0"encoding=“UTF-8”>

  2. <beanid="Student"class=“entity.Student”>

  3. <propertyname=“name”>

  4. Tom

最后我们就可以来建立测试类了:

代码如下:

public static void main(String[] args) {
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml");

//读取xml配置文件

?

Student stu = (Student) ct.getBean("Student"); //得到spring管理的bean

System.out.println("name:"+stu.getName());}

最后run一下就行了:

选中Test类,右键,Run As --> java Application

运行结果就不发图片了,结果会在控制台上出现一个tom.

至此,一个简单spring框架就搭建完成了。


项目推荐:

2000多G的计算机各行业电子资源分享(持续更新)

2020年微信小程序全栈项目之喵喵交友【附课件和源码】

Spring Boot开发小而美的个人博客【附课件和源码】

Java微服务实战296集大型视频-谷粒商城【附代码和课件】

Java开发微服务畅购商城实战【全357集大项目】-附代码和课件

最全最详细数据结构与算法视频-【附课件和源码】


----------------------------- end ------------------------------------


  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值