Maven + Spring hello world example

This quick guide example uses Maven to generate a simple Java project structure, and demonstrates how to retrieve Spring bean and prints a “hello world” string.
Technologies used in this article :

  • Spring 2.5.6
  • Maven 3.0.3
  • Eclipse 3.6
  • JDK 1.6.0.13

1. Generate project structure with Maven

In command prompt, issue following Maven command :

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

Maven will generate all the Java’s standard folders structure for you (besides resources folder, which you need to create it manually)

2. Convert to Eclipse project

Type “mvn eclipse:eclipse” to convert the newly generated Maven style project to Eclipse’s style project.

mvn eclipse:eclipse

Later, import the converted project into Eclipse IDE.

Create a resources folder
Create a resources “/src/main/resources” folder, the Spring’s bean xml configuration file will put here later. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.

3. Add Spring dependency

Add Spring dependency in Maven’s pom.xml file.

File : pom.xml

<?xml version="1.0"?>
<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.common</groupId>
    <artifactId>SpringExamples</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>SpringExamples</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- Spring framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>
    </dependencies>
</project>

Issue “mvn eclipse:eclipse” again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.

4. Spring bean (Java class)

Create a normal Java class (HelloWorld.java) at “src/main/java/com/mkyong/common/HelloWorld.java”. Spring’s bean is just a normal Java class, and declare in Spring bean configuration file later.

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

5. Spring bean configuration file

Create an xml file (Spring-Module.xml) at “src/main/resources/Spring-Module.xml“. This is the Spring’s bean configuration file, which declares all the available Spring beans.
File : Spring-Module.xml

<?xml version="1.0"?>
<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-2.5.xsd">
    <bean id="helloBean" class="com.mkyong.common.HelloWorld">
        <property name="name" value="Mkyong" />
    </bean>
</beans>

6. Review project structure

Review it and make sure the folder structure as follows


spring-hello-world-example.png

7. Run It

Run App.java, it will load the Spring bean configuration file (Spring-Module.xml) and retrieve the Spring bean via getBean() method.

File : App.java

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

8. Output

Hello ! Mkyong
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市释液,隨后出現(xiàn)的幾起案子弄贿,更是在濱河造成了極大的恐慌尊惰,老刑警劉巖盼玄,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機恼布,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來规揪,“玉大人桥氏,你說我怎么就攤上這事∶颓Γ” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵凤藏,是天一觀的道長奸忽。 經(jīng)常有香客問我,道長揖庄,這世上最難降的妖魔是什么栗菜? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮蹄梢,結(jié)果婚禮上疙筹,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好而咆,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布霍比。 她就那樣靜靜地躺著,像睡著了一般暴备。 火紅的嫁衣襯著肌膚如雪悠瞬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天涯捻,我揣著相機與錄音浅妆,去河邊找鬼。 笑死障癌,一個胖子當著我的面吹牛凌外,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播涛浙,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼康辑,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蝗拿?” 一聲冷哼從身側(cè)響起晾捏,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哀托,沒想到半個月后惦辛,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡仓手,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年胖齐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嗽冒。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡呀伙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出添坊,到底是詐尸還是另有隱情剿另,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布贬蛙,位于F島的核電站雨女,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏阳准。R本人自食惡果不足惜氛堕,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望野蝇。 院中可真熱鬧讼稚,春花似錦括儒、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至痛倚,卻和暖如春规婆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝉稳。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工抒蚜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人耘戚。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓嗡髓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親收津。 傳聞我的和親對象是個殘疾皇子饿这,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

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