IDEA中使用Spring的入门操作(Maven创建Web项目)

 

本次小案例在IDEA下,使用maven管理jar包方式实现spring的简单使用

 

 

  1. 创建maven项目:
    1. 点击fileànewàproject

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 选择Mavenàmaven-archetype-webapp,下一步

 

 

    1. 输入自己的定义的信息

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 选择maven版本信息,本地jar包仓库,继续下一步

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 设置项目名称以及项目存储路径

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 创建完成后目录结构如下:

IDEA中使用Spring的入门操作(Maven创建Web项目)

  1. 将项目的基本目录结构添加完善:
    1. 选择FileàProject Structure

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 选择Modules,选择我们创建的项目,依次设置

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 在src目录下创建test目录,添加Tests标识;在src的main目录下添加java和sources目录,添加标识

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 点击完成即可
    2. 添加完后的项目结构如下:

IDEA中使用Spring的入门操作(Maven创建Web项目)

  1. 导入spring需要的jar包:在pom.xml文件中添加maven库文件
内容如下:在<dependencies> </dependencies>标签内添加
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-context</artifactId>

      <version>4.3.18.RELEASE</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-core</artifactId>

      <version>4.3.14.RELEASE</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-beans</artifactId>

      <version>4.3.14.RELEASE</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring</artifactId>

      <version>2.5.6</version>

    </dependency>

IDEA中使用Spring的入门操作(Maven创建Web项目)

  1. 在resources目录下创建spring的配置文件:beans-config.xml:
    1. 在resources目录下鼠标右键点击,选择NewàXML Configuration FileàSpring Config:

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

    1. 输入文件名beans-config.xml即可:

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

  1. 在java目录下创建项目需要的jar包(采用MVC模式层次)

 

IDEA中使用Spring的入门操作(Maven创建Web项目)

  1. 在pojo中创建student对象类

类的内容如下:

package com.sw.pojo;

public class Student {

    private int id;

    private String name;

    private int age;

    private String sex;

    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;

    }

    public int getAge() {

        return age;

    }

    public void setAge(int age) {

        this.age = age;

    }

    public String getSex() {

        return sex;

    }

    public void setSex(String sex) {

        this.sex = sex;

    }

    @Override

    public String toString() {

        return "Student{" +

                "id=" + id +

                ", name='" + name + '\'' +

                ", age=" + age +

                ", sex='" + sex + '\'' +

                '}';

    }

}
  1. 在dao目录中创建StudenTest测试类,类中定义一个add方法,用于测试

 

package com.sw.dao;

public class StudentTest {

    public void add(){

        System.out.println("用于测试student方法!");

    }

}

 

  1. 在service目录中创建StudenTestService服务测试类,类中实现了StudenTest类中的add方法:(一定要写dao实现类的set方法,用于在beans-config.xml文件中注册)

 

package com.sw.service;

import com.sw.dao.StudentTest;

public class StudentTestService {

    private StudentTest stuDao;

    public void setStuDao(StudentTest stuDao) {

        this.stuDao = stuDao;

    }

    public void add(){

        stuDao.add();

    }

}
 
  1. 在servlet目录中创建StudentTestServlet类,类中实现了StudenTestService类中的add方法:

 

package com.sw.servlet;
import com.sw.service.StudentTestService;
public class StudentTestServlet {
    private StudentTestService studentTestService;
    public void setStudentTestService(StudentTestService studentTestService) {
        this.studentTestService = studentTestService;
    }
    public void add(){
        studentTestService.add();
    }
}

 

  1. 在beans-config.xml文件中配置类的信息:

(从dao层一直配置到service,一直到servlet层)

<bean id="student" class="com.sw.pojo.Student">
    <property name="id" value="1"></property>
    <property name="name" value="张三"></property>
    <property name="sex" value="男"></property>
    <property name="age" value="20"></property>
</bean>

<!--student相关配置-->
<bean id="StudentTest" class="com.sw.dao.StudentTest"></bean>

<bean id="StudentTestService" class="com.sw.service.StudentTestService">
    <property name="stuDao" ref="StudentTest"></property>
</bean>

<bean id="StudentTestServlet" class="com.sw.servlet.StudentTestServlet">
    <property name="studentTestService" ref="StudentTestService"></property>
</bean>

  1. 测试类:在Test目录中配置SprinfTest类:

 

@Test
public void test(){
    //加载配置文件
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans-config.xml");
    StudentTestServlet stu = (StudentTestServlet) applicationContext.getBean("StudentTestServlet");
    stu.add();
    Student student = (Student) applicationContext.getBean("student");
    System.out.println(student);
}

 

 

 测试结果:

IDEA中使用Spring的入门操作(Maven创建Web项目)