Spring学习01_HelloWorld
1. 使用idea创建项目
1.1 启动idea选择创建新项目





2. 配置pom.xml文件
2.1 引入spring5
2.1.1 在pom.xml文件中添加依赖
<!--依赖-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
</dependencies>
2.1.2 开启自动导入功能

3. 创建模块
3.1 删除src目录(略)
3.2 创建新的module
右键SrpingStudy ----> new ----> Module

修改配置

继续修改配置

4. 编写HelloWorld类
4.1 解决右键中无法创建class文件的问题
我们在指定目录下,点击右键观察可以创建的文件类型,发现无法创建class文件

搜索相关问题,发现是由于idea没有识别到module的结构造成,修改方法如下:
工具栏File----> Project Structure ---> Modules ----> 选择module名称
标记src/main/java 为Sources
标记src/main/resouces 为Resources


4.2 创建HelloWorld类
4.2.1 在spring-study-01-HelloWorld下的src/main/java下面创建HelloWorld类
package top.shuaiguo.pojo;
/**
* @author : Hyman
* @company : www.shuaiguo.top
* @date : 2020/7/20
*/
public class HelloWorld {
// 声明一个变量
private String msg;
// 无参构造方法
public HelloWorld(){
}
// 带参数的构造方法
public HelloWorld(String msg){
this.msg = msg;
}
// 声明一个方法
public void sayHello(){
System.out.println("Hello " + msg);
}
}
4.2.2 对于HelloWorld类进行测试
- 直接在该类中编写main方法进行测试
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld("World");
helloWorld.sayHello();
}
-
使用Junit测试框架
首先在pom.xml文件中引入junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
在test/java目录下创建测试类top.shuaiguo.pojo.MyTest
package top.shuaiguo.pojo;
import org.junit.Test;
/**
* @author : Hyman
* @company : www.shuaiguo.top
* @date : 2020/7/20
*/
public class MyTest {
@Test
public void testCase001(){
//实例化对象
HelloWorld helloWorld = new HelloWorld("World");
//测试对象方法
helloWorld.sayHello();
}
}
现在看两种方法没有多大区别,只不过第二种借用了测试框架
5. 编写applicationContext.xml
写在最前面,查看公司项目未发现该文件,所以推测,可以使用其他方式实现该文件的功能
5.1 创建文件
5.1.1 所在目录:src/main/resouces目录下
5.1.2 导入约束模版
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
ps:
模版可以在spring官网找到,地址如下:
https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/core.html#spring-core 中的Configuration Metadata条目中找到
5.2 创建NewHelloWorld类
5.2.1 创建类文件(方式方法和HelloWorld一致)
package top.shuaiguo.pojo;
/**
* @author : Hyman
* @company : www.shuaiguo.top
* @date : 2020/7/20
*/
public class NewHelloWorld {
// 声明一个变量
private String msg;
// 必须有get和set方法
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void sayHello(){
System.out.println("Hello " + msg);
}
}
5.2.2 修改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/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="newHelloWorld" class="top.shuaiguo.pojo.NewHelloWorld">
<property name="msg" value="New World"></property>
</bean>
</beans>
5.2.3 创建一个测试方法,进行测试
老的测试方法testCase002
@Test
public void testCase002(){
//实例化对象
NewHelloWorld newHelloWorld = new NewHelloWorld();
// 设置msg
newHelloWorld.setMsg("New World");
//测试对象方法
newHelloWorld.sayHello();
}
新的测试方法testCase003
@Test
public void testCase003(){
// 通过xml获取上下文对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过上下文获取对象
NewHelloWorld newHelloWorld = applicationContext.getBean("newHelloWorld",NewHelloWorld.class);
// 调用对象方法
newHelloWorld.sayHello();
}
6. 总结
- 创建maven项目
- 配置pom文件,导入spring框架
- 创建module
- 创建类
- 私有属性
- 含有get/set方法
- 创建测试方法
- 获取上下文对象
- 从上下文中获取applicationContext.xml配置的bean
- 调用测试方法
- 创建类