歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 利用Maven搭建Spring開發環境

利用Maven搭建Spring開發環境

日期:2017/3/1 10:40:17   编辑:Linux編程

一、 概要說明

最近幾天在測試Spring3.0的AOP功能,在測試功能之前,首先是要搭建出Spring3.0的開發功能。開始去官網下載Spring的相關jar包,但是這些jar包中還是會需要其他的一些jar包,於是又手動的去下載其他的相關jar包。這樣也可以搭建出開發環境,但是需要頻繁的去下載缺少的jar包,很麻煩。這裡,我們可以還有一個更好的辦法,采用maven來管理我們的工程,讓maven來自動為我們去下載相關版本的jar包,具體的配置如下。

二、 下載並安裝maven

去網上下載maven安裝文件,我這裡使用的版本是3.0.1,具體的下載和安裝這裡不做詳細介紹。

三、 搭建Spring開發環境

1. 下載maven插件

要在eclipse中能夠正確使用maven工具來構建工程,需要eclipse中已經正確下載安裝了maven插件。

2. 編寫pom.xml

在工程的根目錄中新建一個名為“pom.xml”的文件,在文件中添加如下代碼,保存後eclipse會自動下載相關jar包,紅色部分為下載相關jar包的xml配置。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>TRSEKP</groupId>

<artifactId>TRSEKP-V6.6</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>TRSEKP-V6.6</name>

<description>TRSEKP V6.6測試工程</description>

<properties>

<project.build.sourceEncoding>GBK</project.build.sourceEncoding>

</properties>

<dependencies>

<!-- 引入Spring-AOP等相關Jar -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-orm</artifactId>

<version>3.0.6.RELEASE</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.6.1</version>

</dependency>

<dependency>

<groupId>aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.5.3</version>

</dependency>

</dependencies>

</project>

3. 編寫測試類

在eclipse中新建一個測試類,如“com.trs.components.mgr”,具體的代碼如下:

package com.trs.components.mgr;

import com.trs.components.persistent.Student;

public class StudentMgr implements IStudentMgr {

public Student saveOne(String _sName) throws Exception {

System.out.println("保存了一個學生對象..");

return null;

}

public void saveMany(String _sName) throws Exception {

System.out.println("保存了多個學生對象..");

}

}

4. 配置bean的xml文件

在工程的源碼目錄下添加一個名為“applicationContext.xml”的文件,這個文件中可以定義spring的bean文件,內容如下:

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id=" StudentMgr " class="com.trs.components.mgr.StudentMgr" />

</beans>

5. 驗證Spring是否配置正確

我們定義完spring的配置後,新建一個測試類,只需要按照下面的代碼即可獲取到“StudentMgr”實例對象,調用代碼如下:

// 使用ApplicationContext來初始化系統

ApplicationContext context = new ClassPathXmlApplicationContext(

"applicationContext.xml");

//通過spring獲取實例對象

StudentMgr studentMgr = (StudentMgr) context.getBean("StudentMgr");

System.out.println("-----------");

studentMgr.saveMany("wuguowei");

Copyright © Linux教程網 All Rights Reserved