Spring環境搭建

來源:互聯網
上載者:User

標籤:

第一步,建立一個動態WEB工程;

第二步,下載Spring相關的壓縮包,並解壓;:

spring-framework-3.0.2.RELEASE-dependencies.zip:http://yunpan.cn/ccFvNrZAMCzqj  訪問密碼 3dbf(Spring的依賴包)

spring-framework-3.0.6.RELEASE-with-docs.zip:http://yunpan.cn/ccFviEbKbgCrV  訪問密碼 b510(Spring的基礎包)

下載並解壓,如所示:

 

第三步,向工程中,引入相關的jar包;

需要引入的jar包列表及所在的目錄,如所示:

Spring基礎jar包

日誌jar包:

放入工程中的lib目錄下,如所示:

 

至此,Spring的環境搭建完畢。

 下面編寫一個簡單的測試程式,驗證環境的可用性。

第一步,建立一個User.java,如所示:

 

User.java中的源碼,如下:

package com.cvicse.utmp.user;public class User {private int id;private String name;private int age;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;}@Overridepublic String toString() {return "User [id=" + id + ", name=" + name + ", age=" + age + "]";}}

  第二步, 在src目錄下,建立Spring.xml檔案,如所示:

 

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-3.0.xsd"><bean id="user" class="com.cvicse.utmp.user.User"><property name="id" value="1"></property><property name="name" value="xiaowei"></property><property name="age" value="22"></property></bean></beans>

  

第三步,建立測試類別,測試代碼如下所示:

 

package com.cvicse.utmp.user;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestUser {public static void main(String[] args) {ApplicationContext apx = new ClassPathXmlApplicationContext("Spring.xml");User user = (User) apx.getBean("user");System.out.println(user);}}

  

運行結果,如所示:

 

至此,環境搭建完畢,簡單測試完畢。

 

Spring環境搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.