Spring學習手冊(1)—— HelloSpring

環(huán)境相關:
JDK : 1.8.0_121
IDE : IntelliJ IDEA 2016.3.4
Spring-Framework: 4.3.6.RELESE

Spring框架為基于JAVA的企業(yè)級應用提供了一套通用、可配置的編程模型,并使應用可較容易的部署在不同的平臺。Spring致力于企業(yè)應用“基礎設施”建設,以便于應用開發(fā)團隊專注于應用業(yè)務邏輯。Spring框架為我們提供了以下特性:

Spring特性:

  • 依賴注入(Dependency Injection)
  • 面向切面編程(Aspect-Oriented Programming)
  • Spring MVC應用和Spring RESTful 服務器框架
  • 對JDBC、JPA和JMS的支持
  • 更多......

一、 創(chuàng)建Java項目

  1. 使用IDEA創(chuàng)建Java應用:File->new->project,選擇Gradle項目,Project SDK選擇JDK1.8,Additional Libraries and Frameworks選擇Java,點擊Next。


    IDEA創(chuàng)建項目
  2. 填寫GroupId、ArtifactId選擇Next。
  3. 如下圖所示,??Use auto-import
    ??Create directories for empty content roots automatically
    選擇Next。


  4. 設置Project name、設置Project location,完成項目設置。

二、設置Gradle配置信息

在工程Project選項框中打開build.gradle,在dependencies中添加

compile 'org.springframework:spring-context:4.3.6.RELEASE'
修改Gradle配置

IDEA會自動將項目依賴的Spring jar包下載到本地(此處需要互聯(lián)網(wǎng))。

三、HelloSpring簡單版

創(chuàng)建接口文件MessageService.java

public interface MessageService {

    String  getMessage();
}

創(chuàng)建MessageService接口的實現(xiàn)類MessageServiceImpl

public class MessageServiceImpl  implements  MessageService{

    public String getMessage() {
        return "MessageServiceImpl:Hello Spring";
    }
}

創(chuàng)建MessagePrinter類

public class MessagePrinter {

   private MessageService  messageService;

   public MessagePrinter(){
   }

   public void printMessage(){
       System.out.println(this.messageService.getMessage());
   }

   public void setMessageService(MessageService messageService){
       this.messageService = messageService;
   }
}

在resources中創(chuàng)建bean.xml文件,配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageService" class="com.liangwei.learnspring.MessageServiceImpl"/>

    <bean id="messagePrinter" class="com.liangwei.learnspring.MessagePrinter">
        <property name="messageService" ref="messageService"/>
    </bean>
</beans>

創(chuàng)建應用文件Application,使用Spring的BeanFactory、Resource、Reader組裝各組件。

public class Application {

    public static void main(String[] args){


        //由于XmlBeanFactory已經(jīng)打標廢棄
        //這里使用DefaultListableBeanFactory 和 Resource

        Resource resource = new ClassPathResource("bean.xml");
        DefaultListableBeanFactory  beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(resource);

        MessagePrinter messagePrinter = beanFactory.getBean("messagePrinter",MessagePrinter.class);

        messagePrinter.printMessage();
    }
}

HelloSpring源代碼地址

四、HelloSpring--ApplicationContext版本

ApplicationContextBeanFactory的超集,一般情況下使用ApplicationContext替代BeanFactory。ClassPathXmlApplicationContextFileSystemXmlApplicationContext對接口ApplicationContext完成了實現(xiàn),這里我們先簡單用ClassPathXmlApplicationContext實現(xiàn)我們的HelloSpring應用。
在原有工程的基礎上,我們只需要更改Application.java類。

public class Application {

    public static void main(String[] args){

        ApplicationContext applicationContext = 
                        new ClassPathXmlApplicationContext("bean.xml");
        MessagePrinter messagePrinter = 
                 applicationContext.getBean("messagePrinter",MessagePrinter.class);
        messagePrinter.printMessage();
    }
}

HelloSpring--ApplicationContext源代碼地址

五、運行程序

前面我們已經(jīng)完成代碼的開發(fā)和配置工作,接下來我們就可以運行應用程序驗證結(jié)果。右鍵選中Application.java文件選擇"Run Application.main()"或快捷鍵control+R,可以在輸出框看到運行結(jié)果:

MessageServiceImpl:Hello Spring

六、總結(jié)

這樣我們就完成了一個基于Spring框架的應用程序?;仡櫞a你會發(fā)現(xiàn)我們通過bean配置文件(bean.xml)來將MessageServiceImpl)和MessagePrinter關聯(lián),MessagePrinter并不與具體的MessageService實現(xiàn)耦合,而是在運行時由Spring通過解析bean.xml文件將具體的MessageServiceImpl注入到MessagePrinter。通過這樣我們就完成了將業(yè)務代碼的解耦合,如果我們未來新開發(fā)了MessageService實現(xiàn),我們只需要修改Bean.xml文件就完成了項目改造。
總結(jié)起來我們的代碼具有如下優(yōu)點:

  • 業(yè)務代碼解耦合,使代碼擴展性增強;
  • 代碼面向接口開發(fā),使代碼具有可測試性;
  • ...

現(xiàn)在我們已初窺Spring框架,前面總結(jié)中我們也提到了“注入”、“依賴”等詞匯,我們會在后面的文章中逐步揭開Spring的面紗。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容