Spring 3 hello world example

This tutorial shows you how to create a simple hello world example in Spring 3.0.
Technologies used in this article :

  • Spring 3.0.5.RELEASE
  • Maven 3.0.3
  • Eclipse 3.6
  • JDK 1.6.0.13

P.S Spring 3.0, at least JDK 1.5 is required to work.

*Spring 3.0 dependencies
*In Spring 2.5.x, almost the entire Spring modules are grouped into a single spring.jar file. Since Spring 3.0, every modules are split into an individual jar file, for example, spring-core, spring-expression, spring-context, spring-aop and etc.

1. Generate project structure with Maven

Issue below Maven command to create a standard Java project structure.

mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=Spring3Example    -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2. Convert to Eclipse project

Convert Maven style project to Eclipse’s style project, and import into Eclipse IDE.

mvn eclipse:eclipse

3. Add Spring 3.0 dependency

Add the Spring 3.0 dependencies listed below in Maven’s pom.xml file. The Spring dependencies are available for download via Maven central repository.
File : pom.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/maven-v4_0_0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.mkyong.core</groupId>  
    <artifactId>Spring3Example</artifactId> 
    <packaging>jar</packaging>  
    <version>1.0-SNAPSHOT</version> 
    <name>Spring3Example</name> 
    <url>http://maven.apache.org</url>  
    <properties>        
        <spring.version>3.0.5.RELEASE</spring.version>  
    </properties>   
    <dependencies>      
        <!-- Spring 3 dependencies -->      
        <dependency>            
            <groupId>org.springframework</groupId>          
            <artifactId>spring-core</artifactId>            
           <version>${spring.version}</version>     
        </dependency>       
        <dependency>            
           <groupId>org.springframework</groupId>           
           <artifactId>spring-context</artifactId>          
           <version>${spring.version}</version>     
      </dependency> 
    </dependencies>
</project>

3. Spring bean

A simple Spring bean.

package com.mkyong.core;

/** * Spring bean * */
public class HelloWorld {   
    private String name;    
    public void setName(String name) {      
        this.name = name;   
    }   
    public void printHello() {      
        System.out.println("Spring 3 : Hello ! " + name);   
    }
}

4. Spring bean configuration file

Create a Spring configuration file, and declare all the available Spring beans.
File : SpringBeans.xml

<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="helloBean" class="com.mkyong.core.HelloWorld">        
        <property name="name" value="Mkyong" /> 
    </bean>
</beans>

5. Review project structure

Review directory structure as follows


Spring3-hello-world-example

6. Run It

Run it.

package com.mkyong.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {  
    public static void main(String[] args) {        
        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");      
        HelloWorld obj = (HelloWorld) context.getBean("helloBean");     
        obj.printHello();   
    }
}

7. Output

Spring 3 : Hello ! Mkyong
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末咸作,一起剝皮案震驚了整個濱河市罗侯,隨后出現(xiàn)的幾起案子船逮,更是在濱河造成了極大的恐慌荧琼,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異篱瞎,居然都是意外死亡,警方通過查閱死者的電腦和手機达罗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進店門坝撑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人粮揉,你說我怎么就攤上這事巡李。” “怎么了扶认?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵侨拦,是天一觀的道長。 經(jīng)常有香客問我辐宾,道長狱从,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任叠纹,我火速辦了婚禮季研,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘誉察。我一直安慰自己与涡,他們只是感情好,可當我...
    茶點故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著驼卖,像睡著了一般氨肌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上酌畜,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天怎囚,我揣著相機與錄音,去河邊找鬼檩奠。 笑死桩了,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的埠戳。 我是一名探鬼主播井誉,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼整胃!你這毒婦竟也來了颗圣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤屁使,失蹤者是張志新(化名)和其女友劉穎在岂,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛮寂,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡蔽午,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了酬蹋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片及老。...
    茶點故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖范抓,靈堂內(nèi)的尸體忽然破棺而出骄恶,到底是詐尸還是另有隱情,我是刑警寧澤匕垫,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布僧鲁,位于F島的核電站,受9級特大地震影響象泵,放射性物質(zhì)發(fā)生泄漏寞秃。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一单芜、第九天 我趴在偏房一處隱蔽的房頂上張望蜕该。 院中可真熱鬧,春花似錦洲鸠、人聲如沸堂淡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绢淀。三九已至萤悴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間皆的,已是汗流浹背覆履。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留费薄,地道東北人硝全。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像楞抡,于是被迫代替她去往敵國和親伟众。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理召廷,服務發(fā)現(xiàn)凳厢,斷路器,智...
    卡卡羅2017閱讀 134,652評論 18 139
  • This tutorial shows you how to create a simple hello worl...
    lovePython閱讀 254評論 0 1
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,806評論 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,152評論 2 7
  • 過早的睡眠對于同室的其他年輕囚犯來說幾乎是無法忍受的竞慢,但對于費迪南德和皮特而言先紫,身體的疲倦早早地就爬上了眼皮,皮...
    土啊土閱讀 391評論 0 3