图文教你整合最新版本搭建SSH框架之一:spring(非常详细)

来源:互联网 发布:死神虚圈篇配乐知乎 编辑:程序博客网 时间:2024/06/06 12:40

SSH框架搭建

由于配置过程会使用到很多图片,如果都写在一篇的话,会显得过于冗长,因此搭建过程分为几个博文来写,最终的框架项目我放到整合的那一篇博文中

配置spring

1.首先找到官网,百度输入spring下载搜索即可
这里写图片描述

2.打开进入网站,找到下图的这个链接
这里写图片描述

3.点击后进入在网页中搜索Zip Files,找到如图链接
这里写图片描述
4.进入后即可看到各个版本的spring框架包,选择最新版本

5.选择 dist.zip结尾的文件
这里写图片描述

6.解压后得到的文件
这里写图片描述

7.下载完文件包,下面开始进行框架搭建,我个人用的是Eclipse Java EE版本,打开IDE,创建一个Dynamic Web Project(项目名叫SSHDemo),Tomcat根据版本设置,我用的是Tomcat7.0
这里写图片描述
8.在WEB-INF->lib下新建文件夹spring-framework-4.3.7,从我们刚才的文件包的libs目录下找到如下的jar加入我们Eclipse项目的lib目录下的spring文件夹中
这里写图片描述

9.然后还需要将这些jar包配置到我们的项目下,具体步骤是右键项目,选择Build Path->Configure Build Path…
这里写图片描述

10.然后点击Add Jar选择lib目录下的jar加入,最后Apply即可
这里写图片描述

11.接下来我们在src建立一个包pers.zzf.test,下面建一个SpringTest.java,用来测试Spring框架

package pers.zzf.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class SpringTest {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public static void main(String... args){        ApplicationContext context = new FileSystemXmlApplicationContext("src\\applicationContext.xml");        SpringTest test = (SpringTest) context.getBean("springTest");        System.out.println(test.getName());    }}

12.由于Spring框架还需要一个配置文件applicationContext.xml,这个模板我们可以到我们之前下载的文件包里面找到spring-framework-reference.pdf这个pdf文件
这里写图片描述

13.打开这个pdf文件后,搜索XML-based configuration metadata,找到如图的地方可以看到一个配置文件模板
这里写图片描述

14.在src建一个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"    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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>

修改后的模板:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="springTest" class="pers.zzf.test.SpringTest">        <!-- collaborators and configuration for this bean go here -->        <property name="name">            <value>ZZF</value>        </property>    </bean>    <!-- more bean definitions go here --></beans>

15.配置到这一步项目结构如下
这里写图片描述
15.现在可以运行SpringTest.java可以看到控制台输出ZZF

0 0
原创粉丝点击