基于Maven的Spring开发环境搭建

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

搭建步骤

1.创建基于Maven的java项目

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

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

 <!-- Spring-->
         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>5.2.2.RELEASE</version>
         </dependency>

注:

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

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

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

 <!--junit-->
         <!-- https://mvnrepository.com/artifact/junit/junit -->
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.12</version>
             <scope>test</scope>
         </dependency>

4.创建类的信息

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

 package com.ffyc.springdemo.model;
 ​
 public class Admin {
 ​
     private int id;
     private String name;
 ​
     public Admin() {
         System.out.println("Admin无参构造");
     }
 ​
     public Admin(int id, String name) {
         this.id = id;
         this.name = name;
     }
 ​
     public int getId() {
         return id;
     }
 ​
     public void setId(int id) {
         this.id = id;
     }
 ​
     public String getName() {
         return name;
     }
 ​
     public void setName(String name) {
         this.name = name;
     }
 ​
     @Override
     public String toString() {
         return "Admin{" +
                 "id=" + id +
                 ", name='" + name + '\'' +
                 '}';
     }
 }
 ​

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

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

 <?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
 http://www.springframework.org/schema/beans/spring-beans.xsd">
 ​
     <!--把需要Spring管理的对象进行配置-->
     <bean id="admin" class="com.ffyc.springdemo.model.Admin">
 ​
         <!--通过get,set方法注入-->
         <property name="id" value="1"></property>
         <property name="name" value="飞飞"></property>
         
     </bean>
 </beans>
 ​
  • 注:

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

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

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

6.编写测试类

 package com.ffyc.springdemo.test;
 ​
 import com.ffyc.springdemo.model.Admin;
 import org.junit.jupiter.api.Test;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 ​
 public class Test1 {
 ​
     @Test
     public  void test() {
 ​
         ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
         Admin admin = app.getBean("admin",Admin.class);
         System.out.println(admin);
     }
 }
 ​

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

猜你喜欢

转载自blog.csdn.net/weixin_52629592/article/details/125844146