• 基于Maven的Spring开发环境搭建


        基于Maven的Spring开发环境搭建也很简单,和基于Maven的Mybatis开发环境搭建类似.下面我们来进行介绍:

    搭建步骤

    1.创建基于Maven的java项目

    2.在pom.xml文件中添加Spring核心jar包的依赖:

    Spring核心框架在Maven中的项目坐标如下:

    1.  
    2.          
    3.          <dependency>
    4.              <groupId>org.springframeworkgroupId>
    5.              <artifactId>spring-contextartifactId>
    6.              <version>5.2.2.RELEASEversion>
    7.          dependency>

    注:

    此处我们只需要导入上述Spring的核心jar包即可,无需导入其他多余Spring有关jar包.因为Maven会为我们自动导入Spring核心jar包所需的其他必要jar包.

    3.导入单元测试jar包方便我们的测试

    junt jar包在Maven中的项目坐标如下:

    1.  
    2.          
    3.          <dependency>
    4.              <groupId>junitgroupId>
    5.              <artifactId>junitartifactId>
    6.              <version>4.12version>
    7.              <scope>testscope>
    8.          dependency>

    4.创建类的信息

    我们在创建时一定要生成类中的get(),set()方法,以及写出它的无参构造和有参构造方法

    1.  package com.ffyc.springdemo.model;
    2.  ​
    3.  public class Admin {
    4.  ​
    5.      private int id;
    6.      private String name;
    7.  ​
    8.      public Admin() {
    9.          System.out.println("Admin无参构造");
    10.     }
    11.  ​
    12.      public Admin(int id, String name) {
    13.          this.id = id;
    14.          this.name = name;
    15.     }
    16.  ​
    17.      public int getId() {
    18.          return id;
    19.     }
    20.  ​
    21.      public void setId(int id) {
    22.          this.id = id;
    23.     }
    24.  ​
    25.      public String getName() {
    26.          return name;
    27.     }
    28.  ​
    29.      public void setName(String name) {
    30.          this.name = name;
    31.     }
    32.  ​
    33.      @Override
    34.      public String toString() {
    35.          return "Admin{" +
    36.                  "id=" + id +
    37.                  ", name='" + name + '\'' +
    38.                  '}';
    39.     }
    40.  }
    41.  ​

    5.在resource文件夹下创建spring.xml

    该文件用于描述Bean的定义信息,Spring根据这些信息创建和管理对象

    1.  "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
    3.  http://www.springframework.org/schema/beans/spring-beans.xsd">
    4.  ​
    5.      
    6.      <bean id="admin" class="com.ffyc.springdemo.model.Admin">
    7.  ​
    8.          
    9.          <property name="id" value="1">property>
    10.          <property name="name" value="飞飞">property>
    11.          
    12.      bean>
    13.  beans>
    14.  ​
    • 注:

      xml文件中用到的属性暂时简单介绍如下,之后会在SpringBean文章中详细介绍

      • class: 指定Bean的实现类,它必须使用类的全限定名

      • id: Bean的唯一标识符,Spring容器对Bean的配置,管理都通过该属性进行.

    6.编写测试类

    1.  package com.ffyc.springdemo.test;
    2.  ​
    3.  import com.ffyc.springdemo.model.Admin;
    4.  import org.junit.jupiter.api.Test;
    5.  import org.springframework.context.ApplicationContext;
    6.  import org.springframework.context.support.ClassPathXmlApplicationContext;
    7.  ​
    8.  public class Test1 {
    9.  ​
    10.      @Test
    11.      public  void test() {
    12.  ​
    13.          ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
    14.          Admin admin = app.getBean("admin",Admin.class);
    15.          System.out.println(admin);
    16.     }
    17.  }
    18.  ​

    这样我们的第一个基于Spring框架的HelloWorld程序就完成了!!!

  • 相关阅读:
    记一次栈溢出异常问题的排查
    2021年PHP-Laravel面试题问卷题 答案记录
    P1443 马的遍历
    数据可视化:Echarts和Tableau简介
    【Linux】基本指令合集
    HCNP Routing&Switching之ARP安全
    对GPT-4o的评价:技术革新与未来展望
    线性代数第6章
    .NET BackgroundWorker
    Docker快速上手:使用Docker部署Drupal并实现公网访问
  • 原文地址:https://blog.csdn.net/weixin_52629592/article/details/125844146