首页 > 编程语言 > 详细

Spring框架环境搭建

时间:2017-04-25 22:54:38      阅读:353      评论:0      收藏:0      [点我收藏+]

环境要求:jdk  1.7 及以上、Spring版本:4.3.2

1、建立普通的java 工程

技术分享

 

2、新建lib目录,并将一下5个核心jar包拷贝过来,并加入classpath中

技术分享

下载地址: http://repo.spring.io/libs-release-local/org/springframework/spring/4.3.2.RELEASE/

 

3、spring  配置文件的编写

在src下新建xml文件,并拷贝官网文档提供的模板内容到xml中,配置bean到xml中

技术分享

4、 验证spring框架环境是否搭建成功  

  验证的方式通过junit4 进行验证

  加载xml文件的方式有两种一般使用第一种

  I.通过当前类路径的方式加载xml文件,启动spring容器框架

  II.根据文件系统方式寻找配置文件,启动spring容器框架(不推荐)

  

 1 package com.wisezone.test;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 import com.wisezone.service.UserService;
10 
11 public class TestUserService {
12 
13     @Test
14     public void test() {
15         /**
16          * 1、启动容器
17          * 2、获取bean
18          * 3、调用bean方法,打印信息
19          */
20         
21         //1、传入xml  启动spring 容器
22         ApplicationContext ac  = new ClassPathXmlApplicationContext("beans.xml");
23         
24         //2、获取bean 
25         UserService userService = (UserService) ac.getBean("userService");
26         
27         //3、调用bean方法,打印信息
28         System.out.println(userService.save());
29         
30     }
31 
32 }
1 package com.wisezone.service;
2 
3 public class UserService {
4     
5     public String save(){
6         return "hello spring";
7     }
8 }

技术分享

 第二种方式:通过绝对路径的方式

 

 1 @Test
 2     public void test02() {
 3         /**
 4          * 1、启动容器
 5          * 2、获取bean
 6          * 3、调用bean方法,打印信息
 7          */
 8         //1、传入xml  启动spring 容器    通过xml的绝对路径
 9         ApplicationContext ac  = new FileSystemXmlApplicationContext("E:\\Myeclipse2014WorkSpace02\\spring01_helloworld\\src\\beans.xml");
10         
11         //2、获取bean 
12         UserService userService = (UserService) ac.getBean("userService");
13         
14         //3、调用bean方法,打印信息
15         System.out.println(userService.save());
16     }

 

技术分享

 

Spring框架环境搭建

原文:http://www.cnblogs.com/wdh1995/p/6764966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!