spring环境搭建(简单实例)

Posted 羊不吃草

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring环境搭建(简单实例)相关的知识,希望对你有一定的参考价值。

1使用Maven导入需要的依赖(在project标签下)

  <properties>
    <spring_version>3.2.2.RELEASE</spring_version>
  </properties>


    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring_version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring_version}</version>
    </dependency>
  </dependencies>

 

2创建测试的bean

package bean;

/**
 * Created by computer on 2017/9/12.
 */
public class Student {
    public String id;
    public String name;
    public String intro;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getIntro() {
        return intro;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }

    public void eat(){
        System.out.println("学生吃饭");
    }

}

 

3创建spring.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="bean.Student"></bean>
</beans>

 

4测试

package test;

import bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * Created by computer on 2017/9/12.
 */
public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new FileSystemXmlApplicationContext("classpath:spring.xml");
        Student student=(Student)applicationContext.getBean("student");
        student.eat();

    }
}

控制台截图

项目目录结构

 

以上是关于spring环境搭建(简单实例)的主要内容,如果未能解决你的问题,请参考以下文章

maven+spring boot搭建简单微服务

spring 简单入门实例

spring+ibatis环境搭建

node 环境下简单web服务器搭建代码

activiti自己定义流程之Spring整合activiti-modeler实例:环境搭建

Spring笔记