SSH之spring,sshspring

來源:互聯網
上載者:User

SSH之spring,sshspring

   從第一次聽說spring到現在應該有一年多的時間了,對於這個陌生有熟悉的架構只能說又愛又恨,愛是因為它的強大恨是因為太強大,強大到我現在都說不明白spring到底是什麼,今天就從皮毛開始說起吧:

一、描述

一句話總結那麼:spring是以控制反轉和面向切面為核心的輕量級的容器架構。

不過這句話資訊量有些大,至於詳細的解釋我覺得我沒有百度解釋的明白,不過可以說一句的是:spring是為瞭解決公司專屬應用程式程式開發複雜性而建立的,它的優勢之一就是它是分層架構,分層架構允許選擇使用任一組件,且它的目的不是取代任何一個已經存在的架構而是很好的融合。

 

二、簡單應用

還是先從應用說起,其實spring的應用比struts應用起來比較簡單與struts不同的是spring不僅可以使用在web項目中在java後台項目中也是可以的,這個小的執行個體就以後台執行個體說明:

第一步:在eclipse中建立java項目:

 

第二步:添加必要的引用:

(註:建立的java項目可能沒有lib檔案夾,可以選擇自己建立)將必要的jar包複製到lib下:

 

在springdemo2右鍵-properties- Java build path-addlibrary-user library-new library-addjars最終添加


第三步:建立測試java類:

package com.tgb.spring;public class HelloWord {/** * 定義擷取值的屬性 */private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public void hello(){System.out.println("hello:" +name);}}

第四步:建立spring的設定檔--applicationcontext.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 -->    <bean id="helloworld" class="com.tgb.spring.HelloWord">        <property name="name" value="陳麗娜"></property>    </bean></beans>

第五步:測試回合結果:

 

package com.tgb.spring;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String []args){   ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationcontext.xml");   HelloWord helloworld=(HelloWord) ctx.getBean("helloworld");        helloworld.hello();}}

  這裡的代碼主要是不用直接建立helloworld執行個體,而是通過spring容器擷取helloworld執行個體,這正是spring容器的作用。

運行結果:


 

   是不是很神奇,spring容器不僅建立了執行個體,而且執行個體中的name屬性有了值,這一切都是因為spring容器,這種為對象設定屬性的方式也被稱為控制反轉。

     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.