前言
由于我一开始学Java时,采用的是notepad+,所以学spring时需要再安装一个eclipse,我电脑上也有一个eclipse,只是他是我用来开发安卓的,因为对spring不了解,作为小白的我,还是从头来,重新搭一个eclipse+spring
- jdk1.8.0_151(这是我学Java时就有的)
- 64位
- Windows10
- CPU
下载eclipse
到官网下载
-
点击Download Packages(因为第一页显示的不是JavaEE使用的eclipse)
-
选择Eclipse IDE for Enterprise Java Developers,对应自己的电脑环境进行下载(这里我下载的是windows 64)
-
最好选择镜像下载比较快,我一开始没有用本地下载,很慢,而且下载好了我看了下才300kb(正常是380几M),重新下了3次依然下不完整,最后看到有提供镜像。
-
解压
-
以管理员的身份运行
eclipse.exe
-
选择项目存放的路径位置,最好不要放在c盘中
-
这样就进入到eclipse开发环境,开始项目开发
-
打开菜单栏中Window-Preferences
-
https://baijiahao.baidu.com/s?id=1630595805095144269&wfr=spider&for=pc
Spring
下载spring-tool-suite
官网拉到最底下
由于我没有用vpn,进入GitHub很慢。
进入后,由于我的eclipse是4.14.0,所以选择这个
此时先不下载,复制链接进行修改具体可以看下这篇
,我是将https://dist.springsource.com/release/STS/3.9.11.RELEASE/dist/e4.14/spring-tool-suite-3.9.11.RELEASE-e4.14.0-win32-x86_64.zip
更改为:
https://dist.springsource.com/release/TOOLS/update/3.9.11.RELEASE/e4.14/springsource-tool-suite-3.9.11.RELEASE-e4.14.0-updatesite.zip
修改后复制新路径到浏览器回车即可下载。
不用解压,具体可以看这篇
下载logging日志api文件
我下的是:commons-logging-1.2.jar,然后解压
下载Spring-framework开发的api
我下的是:spring-framework-5.1.4.RELEASE-dist.zip,然后解压
测试
D:\spring\spring\src
路径下有applicationContext.xml
的代码,我们打开
//在src文件夹下创建UserService类
package com.service;
public class UserService {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello() {
System.out.println("hello:"+name);
}
}
//在src文件夹下创建Test.java
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserService;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// UserService us=new UserService();
// us.setName("gyy");
// us.sayHello();
//这里慢一点,防止出错,我一开始applicationContext.xml写错了。
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us=(UserService)ac.getBean("userService");
us.sayHello();
}
}
}
//新建文件命名为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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 在容器文件中配置bean -->
<bean id="userService" class="com.service.UserService">
<property name="name">
<value>gyy</value>
</property>
</bean>
</beans>
运行后
hello:gyy
成功