Spring学习01_HelloWorld

2020-07-20  本文已影响0人  茧城寒舍

1. 使用idea创建项目

1.1 启动idea选择创建新项目

启动界面
选择maven方式
配置maven
继续配置
启动完成

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


新建module

修改配置


修改配置

继续修改配置

继续修改

4. 编写HelloWorld类

4.1 解决右键中无法创建class文件的问题

我们在指定目录下,点击右键观察可以创建的文件类型,发现无法创建class文件


无法创建类

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

标记java
标记resouce

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类进行测试

    public static void main(String[] args) {

        HelloWorld helloWorld = new HelloWorld("World");
        helloWorld.sayHello();
    }
        <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. 总结

上一篇 下一篇

猜你喜欢

热点阅读