码迷,mamicode.com
首页 > 编程语言 > 详细

spring环境搭建(简单实例)

时间:2017-09-12 22:04:16      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:[]   his   ext.get   span   maven   ima   pac   目录   void   

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环境搭建(简单实例)

标签:[]   his   ext.get   span   maven   ima   pac   目录   void   

原文地址:http://www.cnblogs.com/YangJM/p/7512275.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!