手把手教你搭建Spring开发环境(IDEA)

矫情吗;* 2022-12-08 05:15 392阅读 0赞

首先打开IDEA,HAHAHA

  1. 创建Maven项目,我一般习惯用使用maven-archetype-webapp骨架。(为啥使用骨架: maven骨架的使用能够帮我们快速的构建结构一致的项目,省时省力。)

\[外链图片转存失败,源站可能有防盗链机   制,建议将图失败,源站可能有防盗链机制,建议将图片保存下来直接上传片保存下来(img-5UFYsb4p-1600396874553)(C:\\Users\\apple\\AppData\\Roaming\\Typora\\typora-user-images\\image-20200918093209083.png)(C:\\Users\\apple\\AppData\\Roaming\\Typora\\typora-user-images\\image-20200918093209083.png)\]

  1. 改下项目名称和GroupId

    \[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8jRZlWql-1600396874556)(C:\\Users\\apple\\AppData\\Roaming\\Typora\\typora-user-images\\image-20200918093351817.png)\]

  2. 在pom.xml中添加依赖(记得刷新maven)

    1. <dependencies>
    2. <!-- 单元测试的依赖-->
    3. <dependency>
    4. <groupId>junit</groupId>
    5. <artifactId>junit</artifactId>
    6. <version>4.11</version>
    7. <scope>test</scope>
    8. </dependency>
    9. <!-- spring的依赖-->
    10. <dependency>
    11. <groupId>org.springframework</groupId>
    12. <artifactId>spring-context</artifactId>
    13. <version>5.0.2.RELEASE</version>
    14. </dependency>
    15. <!-- 使用Lombok-->
    16. <dependency>
    17. <groupId>org.projectlombok</groupId>
    18. <artifactId>lombok</artifactId>
    19. <version>1.16.20</version>
    20. <scope>provided</scope>
    21. </dependency>
    22. </dependencies>
  3. 完成目录结构

    • 一般你右击new一个Directory的时候,缺失的都会显示出来,你直接添加就可以了

    \[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MZSMvwVq-1600396874560)(C:\\Users\\apple\\AppData\\Roaming\\Typora\\typora-user-images\\image-20200918093548420.png)\]

  4. 然后需要添加spring的配置文件了。当然是再resoures下啦,我一般创建的文件名为bean.xml(看你自己喽,我是为了方便记忆)

    • 基本约束

      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <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">
      3. </beans>
    • 所有约束

      1. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      2. </beans>
  5. Let`s go来使用一下

    1. 创建一个实体类

      1. import lombok.Getter;
      2. import lombok.Setter;
      3. import lombok.ToString;
      4. @Getter
      5. @Setter
      6. @ToString
      7. public class Student {
      8. private Integer id;
      9. private String name;
      10. private Integer age;
      11. private Integer score;
      12. }
    2. 创建对象交给spring来管理吧(在我们刚才的配置文件中添加)

      1. <?xml version="1.0" encoding="UTF-8"?>
      2. <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">
      3. <bean id="student" class="model.Student">
      4. <property name="id" value="1"/>
      5. <property name="name" value="mxh"/>
      6. <property name="age" value="21"/>
      7. <property name="score" value="85"/>
      8. </bean>
      9. </beans>
    3. 使用一哈看看效果

      1. import org.junit.Test;
      2. import org.springframework.context.ApplicationContext;
      3. import org.springframework.context.support.ClassPathXmlApplicationContext;
      4. public class SpringTest {
      5. @Test
      6. public void f(){
      7. //1.获取核心容器
      8. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
      9. //2.根据id获取对象
      10. Student s = (Student) ac.getBean("student");
      11. //3.处理对象
      12. System.out.println(s);
      13. System.out.println("test1 "+s.hashCode());
      14. }
      15. @Test
      16. public void f1(){
      17. //1.获取核心容器
      18. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
      19. //2.根据id获取对象
      20. Student s = (Student) ac.getBean("student");
      21. //3.处理对象
      22. System.out.println(s);
      23. System.out.println("test2 "+s.hashCode());
      24. }
      25. }

      在这里插入图片描述

我们可以看出是同一个对象,都来自于spring容器。

发表评论

表情:
评论列表 (有 0 条评论,392人围观)

还没有评论,来说两句吧...

相关阅读