哈哈哈哈,我的Spring的简单框架终于搭建成功了,所以迫不及待的来分享了,虽然网上已经有了很多搭建方法,但是在搭建的过程中还是遇到了很多磕磕碰碰,急死人呀!
在写一个常规的搭建方法时,我会把我遇到的一些小坑也写上来,首先声明,我这次的搭建也是参考网上的信息而成功的,如果有雷同还请见谅!
01声明
在我寻找网上的搭建方法时,找到3种Spring的搭建方法,最后只成功了一种。下面我将会把成功的搭建方法和不成功的搭建方法写出来,让大家迅速的能够搭建出框架,不要浪费太多的时间尝试。
顺便声明一下,失败的搭建方法也许不代表这种方法的是错误的,也许是我没有搞懂作者的方法,请大家不要对号入座的去寻找作者。
02搭建成功的方法
1、下载所有的包
下载地址:http://commons.apache.org/proper/commons-logging/download_logging.cgi
```下载springframework包
下载地址:https://spring.io/projects/spring-framework
网页点击下载步骤参考:https://jingyan.baidu.com/album/455a99505b399da16627783b.html?picindex=3
```下载log4j包
http://logging.apache.org/log4j/2.x/download.html
网页点击下载步骤参考:https://zhinan.sogou.com/guide/detail/?id=316513705936
下载成功后,把所有的包汇在一个文件夹,然后后手动导入eclipse,需要注意的是springframework下载后所有的包并不需要全部导入eclipse,只要选取几个特定的就可以了。
下面是我把需要导入的包汇总的截图:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024170856978.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024171038406.png)
**2、把下载好的包导入到Eclipse**
0.1 首先创建Java project,我把它命名为**springfirst**,然后在**springfirst**下面创建一个lib文件夹,把上面所有的包通过import导入到了lib里面。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024201322753.png)
0.2 通过点击**Configure Build Path,**![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024201939614.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RhbmdzaHVzaGVuZw==,size_16,color_FFFFFF,t_70)
通过点击Add JARs..把所有的包添入到Libraries,形成了**Referenced Libraries**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024202334916.png)
**Referenced Libraries **
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024204203141.png)
0.3创建com.spring.entity的包,然后在包下创建Person类。
代码如下:
package com.spring.entity;
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;
}
@Override
public String toString() {
return "person [name = "+ name +", age =" + age + "]";
}
}
0.4再创建com.spring.test包,在包下面创建SpringTest类。
代码如下:
package com.spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.entity.Person;
public class SpringTest {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Person person = context.getBean("person",Person.class);
System.out.println(person.toString());
}
}
0.5创建xml文件,我把它命名为spring-config.xml
xml的原始文件:在docs——>spring-framework-reference——>htmlsingle——>index.html
打开这个index.html后,按ctrl+F键搜关键词:<?xml 就可以找到那段官方的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 id="person" class="com.spring.entity.Person">
<property name="name" value="鹏北海"></property>
<property name="age" value="18"></property>
</bean>
</beans>
注意:上面的赋值是我自己写的,读者可以自己更改,然后测试一下是否搭建成功。
通过单元测试的运行结果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191024210619639.png)
恭喜你,你的Sping框架搭建成功了。
## 03没有成功的方法
**01、利用Maven仓库的搭建方法**
由于还没有学Maven,所以不知道如何用其直接搭建。
**02、利用Spring Tool和junit.test的搭建方法**
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019102513221322.png)
这个方法最终搭建没有成功,运行结果没有出来,原因未知,后面等我彻底掌握之后再进行分析。