Spring Boot [組件學習-Spring]

導讀:


在上篇文章的結尾提到了Spring Boot 提供了一系列的框架整合(Starter POMs)幫助咱們提高開發效率,可是這並不意味着咱們不須要學習這些框架,反而更須要去學習,經過學習這些框架可使咱們更好的去解決生產中遇到的問題。 在這篇文章中主要以Spring Framework做爲起步點,帶你快速瞭解Spring Framework (下面將以 Spring 代指 Spring Framework)。html

Spring Framework:

Spring 是什麼?


簡要介紹:
Spring 框架是一個開源的 Java 平臺,它爲容易而快速的開發出耐用的 Java 應用程序提供了全面的基礎設施。
Spring 框架最初是由 Rod Johnson 編寫的,而且 2003 年 6 月首次在 Apache 2.0 許可下發布。spring

詳細介紹:
Spring Framework 是一個開源的Java/Java EE全功能棧(full-stack)的應用程序框架,以Apache許可證形式發佈,也有.NET平臺上的移植版本。該框架基於 Expert One-on-One Java EE Design and Development(ISBN 0-7645-4385-7)一書中的代碼,最初由Rod Johnson和Juergen Hoeller等開發。Spring Framework提供了一個簡易的開發方式,這種開發方式,將避免那些可能導致底層代碼變得繁雜混亂的大量的屬性文件和幫助類。express

引自:維基百科app

Spring 有什麼好處?


Spring 框架提供約 20 個模塊,能夠根據應用程序的要求來使用,從而提升開發效率。
查看更多介紹:
Spring IoC有什麼好處呢? 知乎
spring 好處與優勢
Spring的做用到底體如今哪裏框架

Spring 怎麼用?


快速一覽:
Spring 的體系結構:體系結構介紹描述
圖片描述yii

快速上手Spring: 函數

經濟學中有個概念叫作2 8 定理,在咱們這篇文章中咱們也以這個定理做爲指導思想,列舉Spring中最經常使用的20%的配置與註解。
在Spring中最核心的兩個概念是IOC 與 AOP,咱們將分別以它兩個爲例列舉出經常使用的註解,在開始以前咱們先了解一下Spring IOC 的概念AOP的概念,瞭解完這些概念以後咱們開始以一個簡單的Hello World 爲例:學習

要求:spa

  • 將Spring導入項目.net

1.Spring 上下文的配置類:

//自定義的bean類型
public class Hello {

    private String hello;
    
    /**省略get/set**/
}

//配置上下文
@Configuration
public class Configer {

    //註冊Hello 示例
    @Bean
    public Hello getHello(){
        return new Hello();
    }
    
}

2.獲取Spring 上下文中的Bean:

public class MainApp {

    public static void main(String[] args) {
        //建立Spring 上下文對象
        ApplicationContext applicationContext =   new AnnotationConfigApplicationContext(Configer.class);
        //獲取Spring 上下文對象中的 Hello的示例
        Hello hello = applicationContext.getBean(Hello .class);
        hello.setHello("Hello World");
        System.out.println(hello.getHello());
    }
}

控制檯信息:

Hello World

一.Spring IOC 中經常使用的註解:
註冊上下文
屬性基本註解:

  • @Bean :用於表示建立的bean示例

類級別註解:

  • @Configuration 配置上下文

  • @Service用於標註業務層組件、

  • @Controller用於標註控制層組件(如struts中的action)、

  • @Repository用於標註數據訪問組件,即DAO組件。

  • @Component泛指組件,當組件很差歸類的時候,咱們可使用這個註解進行標註。 這四種註解僅僅是角色不一樣,但本質都同樣。

用於自動裝配
字段及方法註解 :

  • @Autowired 註解能夠應用到 bean 屬性的 setter 方法,非 setter 方法,構造函數和屬性。

  • @Resource 註解能夠應用到 bean 屬性的 setter 方法。

二.AOP中經常使用的註解:
爲列方便理解舉一個簡單的例子:

@Component
@Aspect
public class MyAdvice {

    @Pointcut("execution(* cn.sunxyz.spring.aop.*Service.*(..))") // expression
    private void businessService() {
    }

    // 在一個方法執行以前,執行通知。
    @Before("businessService()")
    public void doBeforeTask() {
        System.out.println("doBeforeTask.");
    }

    // 在一個方法執行以後,不考慮其結果,執行通知。
    @After("businessService()")
    public void doAfterTask() {
        System.out.println("doAfterTask.");
    }

    // 在一個方法執行以後,只有在方法成功完成時,才能執行通知。
    @AfterReturning(pointcut = "businessService()", returning = "retVal")
    public void doAfterReturnningTask(JoinPoint joinPoint, Object retVal) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("doAfterReturnningTask " + methodName + " return with " + retVal);
    }

    // 在一個方法執行以後,只有在方法退出拋出異常時,才能執行通知
    @AfterThrowing(pointcut = "businessService()", throwing = "ex")
    public void doAfterThrowingTask(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("doAfterThrowingTask " + methodName + " occurs exception: " + ex);
    }

    // 在建議方法調用以前和以後,執行通知。
    @Around("businessService()")
    public Object doAroundTask(ProceedingJoinPoint jpoint) {
        Object result = null;
        String methodName = jpoint.getSignature().getName();
        // 執行目標方法
        try {
            // 前置通知
            System.out.println("The method " + methodName + " begins with " + Arrays.asList(jpoint.getArgs()));
            result = jpoint.proceed();
            // 返回通知
            System.out.println("The method " + methodName + " ends with " + Arrays.asList(jpoint.getArgs()));
        } catch (Throwable e) {
            // 異常通知
            System.out.println("The method " + methodName + " occurs expection : " + e);
            throw new RuntimeException(e);
        }
        // 後置通知
        System.out.println("The method " + methodName + " ends");
        return result;

    }
}

類級註解:

  • @Aspect 聲明這是一個切面對象
    方法級註解:

  • @Pointcut 聲明一個切點規則

  • @Before 方法執行以前,執行通知。

  • @After 方法執行以後,不考慮其結果,執行通知。

  • @AfterReturning 方法執行以後,只有在方法成功完成時,才能執行通知

  • @AfterThrowing 方法執行以後,只有在方法退出拋出異常時,才能執行通知

  • @Around 在方法調用以前和以後,執行通知 等價於上面四個相加

想要了解更多詳細信息 能夠查看下面的學習資料。

學習資料:


官方文檔:
Spring Framework Reference Documentation -4.3.6.RELEASE
學習教程:
Spring 教程 - 極客學院 基於spring 4.1.6版
Spring 教程 - 簡書 基於spring 4.1.6版
Spring初學快速入門 - 一百教程網 基於spring 4.0.4版

結語:


Spring 做爲一個優秀的開發框架幫助咱們提升了開發效率,並且其中的IOC 與 AOP的思想能夠幫助咱們將項目構建的更健壯,在下一篇文章中將簡要的介紹一下Spring MVC 經常使用的註解。

相關文章
相關標籤/搜索