日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《spring实战第四版》的读书笔记

發布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《spring实战第四版》的读书笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

《spring實戰第四版》的讀書筆記

1 概述

《Spring實戰第四版》描述了Spring4架構的設計,看完了以后,最大感覺是Spring的IOC與aop理念實在是太強大了,而且用注解來簡化系統配置的想法也非常棒,整個架構簡直就是MVC的典范

2 Spring之旅

2.1 Intellij IDEA

下載Intellij的15版本,然后將授權地址填寫為http://idea.iteblog.com/key.php

建立一個Spring項目

按圖建立對應的文件

配置啟動參數

建立Application的啟動項

將入口Class指向到KnightMain

2.2 IOC

1 2 3 4 5 6 7 8 package com.fishedee.knights;/*** Created by fishedee on 24/11/2016.*/ public interface Knight {void embarkOnQuest(); }

建立Kngiht接口

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.fishedee.knights;import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ public class BraveKnight implements Knight{private Quest quest;public BraveKnight(Quest quest){this.quest = quest;}public void embarkOnQuest(){quest.embark();} }

建立BraveKnight類

1 2 3 4 5 6 7 8 package com.fishedee.knights;/*** Created by fishedee on 24/11/2016.*/ public interface Quest {public void embark(); }

建立Quest接口

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.fishedee.knights;import java.io.PrintStream; import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ public class SlayDragonQuest implements Quest{public PrintStream stream;public SlayDragonQuest(PrintStream stream){this.stream = stream;}public void embark(){stream.println("Embarking on quest to slay the dragon!");} }

建立SlayDragonQuest

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.fishedee.knights;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by fishedee on 24/11/2016.*/ public class KnightMain {public static void main(String[] args) throws Exception{ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("knights.xml");Knight knight = context.getBean(Knight.class);knight.embarkOnQuest();context.close();} }

建立KnightMain

1 2 3 4 5 6 7 8 9 10 11 <?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="quest" class="com.fishedee.knights.SlayDragonQuest"><constructor-arg value="#{T(System).out}"/></bean><bean id="knight" class="com.fishedee.knights.BraveKnight"><constructor-arg ref="quest"/></bean> </beans>

建立kngihts.xml的配置文件

運行程序后就能看到Embarking onquest的輸出了

在這個程序中可以看到Spring關于ioc的重要特點

  • 依賴的對象不直接引用,而是只引用接口
  • 對象的創建與注入由Spring來決定,Spring可以根據xml配置來創建對象

這樣的ioc就有很特別的能力了

  • 依賴解耦,依賴對象只要滿足接口就能自由替換,不影響使用方的代碼
  • 依賴封裝,Spring可以在注入對象時,對對象執行變形,例如封裝遠程調用,mock打樁,進行日志輸出等操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.fishedee.knights;import org.junit.Test; import static org.mockito.Mockito.*; import static org.junit.Assert.*;/*** Created by fishedee on 24/11/2016.*/ public class BraveKnightTest {@Testpublic void testEmbarkOnQuest() throws Exception {Quest mockQuest = mock(Quest.class);BraveKnight knight = new BraveKnight(mockQuest);knight.embarkOnQuest();verify(mockQuest,times(1)).embark();} }

例如,BraveKnight依賴的Quest對象,由于BraveKnight依賴的是接口,不是具體實現,我們就能對Quest進行很容易的mock,從而簡單地單元測試

2.3 AOP

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.fishedee.knights;import java.io.PrintStream;/*** Created by fishedee on 26/11/2016.*/ public class Minstrel {private PrintStream stream;public Minstrel(PrintStream stream){this.stream = stream;}public void singBeforeQuest(){stream.println("Fa la la");}public void singAfterQuest(){stream.println("Tea bee bee");} }

建立Minstrel類

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><bean id="quest" class="com.fishedee.knights.SlayDragonQuest"><constructor-arg value="#{T(System).out}"/></bean><bean id="knight" class="com.fishedee.knights.BraveKnight"><constructor-arg ref="quest"/></bean><bean id="minstrel" class="com.fishedee.knights.Minstrel"><constructor-arg value="#{T(System).out}"/></bean><aop:config><aop:aspect ref="minstrel"><aop:pointcut id="embark"expression="execution(* *.embarkOnQuest(..))"/><aop:before pointcut-ref="embark"method="singBeforeQuest"/><aop:after pointcut-ref="embark"method="singAfterQuest"/></aop:aspect></aop:config> </beans>

啟動后看到Embark前后執行對對應的輸出

修改配置文件,將Minstrel聲明為切面,當調用embarkOnQuest方法時會自動回調Minstrel的方法

就這樣,Spring在不修改Knight與Quest的代碼下,就能在其方法執行前后插入自己想要的代碼,這讓我們能達成簡單的cache,日志,事務等切面式的實現了

3 基礎ioc

Spring中提供三種裝配bean的方法

  • 在xml中進行顯式配置
  • 在Java中進行顯式配置
  • 隱式的bean發現機制和自動裝配

3.1 在xml中裝配

第2章已經寫過,就不多說了

3.2 在java中裝配

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.fishedee.knights;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;/*** Created by fishedee on 26/11/2016.*/ @Configuration public class Config {@Beanpublic Quest quest(){return new SlayDragonQuest();}@Beanpublic Knight knight(Quest quest){return new BraveKnight(quest);} }

代替xml,使用Java文件來做配置,要注意用Configuration聲明配置文件,生成bean的方法都用Bean注解

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.fishedee.knights;import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by fishedee on 24/11/2016.*/ public class KnightMain {public static void main(String[] args) throws Exception{AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);Knight knight = context.getBean(Knight.class);knight.embarkOnQuest();context.close();} }

啟動ApplicationContext改用AnnotationConfigApplicationContext即可

使用Java裝配的好處是,強類型,支持豐富的java語法特性

3.3 自動裝配

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.fishedee.knights;import org.springframework.stereotype.Component;import java.io.PrintStream; import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ @Component public class SlayDragonQuest implements Quest{public PrintStream stream;public SlayDragonQuest(){this.stream = System.out;}public void embark(){stream.println("Embarking on quest to slay the dragon!");} }

將SlayDragonQuest聲明為bean,加入@Component即可

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.fishedee.knights;import org.springframework.stereotype.Component;import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ @Component public class BraveKnight implements Knight{private Quest quest;public BraveKnight(Quest quest){this.quest = quest;}public void embarkOnQuest(){quest.embark();} }

將BraveKnight聲明為bean,同時Quest出現在構造參數上,這個Quest類型會被自動裝配

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.fishedee.knights;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ @Component public class BraveKnight implements Knight{@Autowiredprivate Quest quest;public BraveKnight(){}public void embarkOnQuest(){quest.embark();} }

或者可以將Quest寫上Autowired注解,這個私有變量也會被自動裝配

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.fishedee.knights;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.mockito.Mockito.*; import static org.junit.Assert.*;/*** Created by fishedee on 24/11/2016.*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class BraveKnightTest {@AutowiredBraveKnight knight;@Testpublic void testEmbarkOnQuest() throws Exception {knight.embarkOnQuest();} }

自動裝配也支持單元測試,注意測試文件中指定Config

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><context:component-scan base-package="com.fishedee.knights"/><aop:config><aop:aspect ref="minstrel"><aop:pointcut id="embark"expression="execution(* *.embarkOnQuest(..))"/><aop:before pointcut-ref="embark"method="singBeforeQuest"/><aop:after pointcut-ref="embark"method="singAfterQuest"/></aop:aspect></aop:config> </beans>

在xml配置文件中加入component-scan,并指定包名即可

1 2 3 4 5 6 7 8 9 10 11 12 13 package com.fishedee.knights;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;/*** Created by fishedee on 26/11/2016.*/ @Configuration @ComponentScan public class Config { }

或在Java配置中,加入ComponentScan注解即可,非常簡單

自動裝配能大幅度減少需要配置的bean,所以使用中一般是自動裝配為主,Java裝配為輔的方式

3.4 混合配置

1 2 3 4 5 6 7 8 9 10 11 12 13 package com.fishedee.knights;import org.springframework.context.annotation.*;/*** Created by fishedee on 26/11/2016.*/ @Configuration @ComponentScan @Import(Config2.class) @ImportResource("knights.xml") public class Config { }

Java配置中引入其他Java配置,或者引入其他xml配置的方法

1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><import resource="knights2.xml"/> </beans>

xml配置中引入其他xml配置的方式,注意,不能用xml引入Java配置

4 高級ioc

4.1 環境裝配

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.fishedee.knights;import org.springframework.context.annotation.*;import java.io.PrintStream;/*** Created by fishedee on 26/11/2016.*/ @Configuration @ComponentScan public class Config {@Bean@Profile("dev")public PrintStream printStream(){return System.out;}@Bean@Profile("prod")public PrintStream printStream2()throws Exception{return new PrintStream("fish.out");} }

可以在Config上加入Profile注解,用來表明這個bean配置是在哪個環境上使用的,當然也可以將Profile注解放到Config上,表明這個Config都是Profile指定的

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.fishedee.knights;import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.env.ConfigurableEnvironment;/*** Created by fishedee on 24/11/2016.*/ public class KnightMain {public static void main(String[] args) throws Exception{AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.getEnvironment().setActiveProfiles("prod");context.register(Config.class);context.refresh();//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("knights.xml");Knight knight = context.getBean(Knight.class);knight.embarkOnQuest();context.close();} }

啟動時可以根據context來指定profile

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.fishedee.knights;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import static org.mockito.Mockito.*; import static org.junit.Assert.*;/*** Created by fishedee on 24/11/2016.*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @ActiveProfiles("dev") public class BraveKnightTest {@AutowiredBraveKnight knight;@Testpublic void testEmbarkOnQuest() throws Exception {knight.embarkOnQuest();} }

單元測試中可以根據ActiveProfiles注解來指定環境

4.2 條件裝配

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.fishedee.knights;import org.springframework.context.annotation.*;import java.io.PrintStream;/*** Created by fishedee on 26/11/2016.*/ @Configuration @ComponentScan public class Config {@Bean@Conditional(ConfigCondition.class)public PrintStream printStream(){return System.out;}@Bean@Profile("prod")public PrintStream printStream2()throws Exception{return new PrintStream("fish.out");} }

指定printStream生成condition為ConfigCondition.class

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.fishedee.knights;import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata;/*** Created by fishedee on 27/11/2016.*/ public class ConfigCondition implements Condition{public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata){String[] profiles = context.getEnvironment().getActiveProfiles();for( String single : profiles ){if( single.equals("fish")){return true;}}return false;} }

而ConfigCondition則是檢查profile是否為fish

所以,條件裝配是比環境裝配更為強大而動態的方式而已。

4.3 指定裝配

1 No qualifying bean of type 'com.fishedee.knights.Quest' available: expected single matching bean but found 2: slayDragonQuest,slayHumanQuest

如果我有兩個Quest都滿足Quest接口時,Spring就會彈出錯誤,說有歧義,slayDragonQuest和slayHumanQuest

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.fishedee.knights;import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component;import java.io.PrintStream; import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ @Component @Primary public class SlayDragonQuest implements Quest{public PrintStream stream;public SlayDragonQuest(PrintStream stream){this.stream = stream;}public void embark(){stream.println("Embarking on quest to slay the dragon!");} }

解決辦法一,給SlayDragonQuest給予Primary優先級,默認選擇它

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.fishedee.knights;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component;import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ @Component public class BraveKnight implements Knight{@Autowired@Qualifier("slayDragonQuest")private Quest quest;public BraveKnight(){}public void embarkOnQuest(){quest.embark();} }

解決辦法二,讓Knight指定哪個Quest,用Qualifier注解

4.4 作用域

Spring創建的bean,有以下幾種作用域

  • 單例(Singleton),整個應用只有一個bean實例
  • 原型(Prototype),每次都創建一個bean實例
  • 會話(Session),在Web中,每個會話創建一個bean實例
  • 請求(Request),在Web中,每個請求創建一個bean實例

默認情況下,所有的bean都是單例會話域

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package com.fishedee.knights;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.stereotype.Component;import java.util.Queue;/*** Created by fishedee on 24/11/2016.*/ @Component @Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE,proxyMode = ScopedProxyMode.INTERFACES ) public class BraveKnight implements Knight{@Autowired@Qualifier("slayDragonQuest")private Quest quest;public BraveKnight(){}public void embarkOnQuest(){quest.embark();} }

在bean中使用Scope注解就可以了,注意多例插入到單例對象中,需要用INTERFACES的proxy

5 aop

5.1 基礎切面

在Spring中,切面有以下的幾種

  • After,在目標方法返回或拋出異常后調用
  • AfterReturning,在目標方法返回后調用
  • AfterThrowing,在目標方法拋出異常后調用
  • Before,在目標方法調用之前調用
  • Around,將目標方法封裝起來
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.fishedee.knights;import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;import java.io.PrintStream;/*** Created by fishedee on 26/11/2016.*/ @Component @Aspect public class Minstrel {private PrintStream stream;public Minstrel(){stream = System.out;}@Pointcut("execution(* *.embarkOnQuest(..))")public void quest(){}@Before("quest()")public void singBeforeQuest(){stream.println("Fa la la");}@After("quest()")public void singAfterQuest(){stream.println("Tea bee bee");} }

將Minstrel方法用Aspect注解圈起來,然后在觸發方法上,加入Pointcut,Before,After等觸發類型即可

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.fishedee.knights;import org.springframework.context.annotation.*;import java.io.PrintStream;/*** Created by fishedee on 26/11/2016.*/ @Configuration @EnableAspectJAutoProxy @ComponentScan public class Config {@Beanpublic PrintStream printStream(){return System.out;} }

配置上開啟EnableAspectJAutoProxy注解

5.2 切面參數

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.fishedee.knights;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;import java.io.PrintStream;/*** Created by fishedee on 26/11/2016.*/ @Component @Aspect public class Minstrel {private PrintStream stream;public Minstrel(){stream = System.out;}@Pointcut("execution(* *.embark(int)) && args(embarkPower)")public void quest(int embarkPower){}@Around("quest(embarkPower2)")public void aroundQuest(ProceedingJoinPoint jp,int embarkPower2)throws Throwable{try {stream.println("Fa la la");stream.println("Power "+embarkPower2);jp.proceed();}finally{stream.println("Tea bee bee");}} }

在Pointcut中指定包含參數的函數類型,以及args指定參數名稱,然后在Around上也指定接收參數即可。注意,在Around上要調用原函數。

6 基礎MVC

6.1 Intellij IDEA

創建一個勾選了MVC選項的Spring項目

建立以上的文件和文件夾

1 2 3 4 5 6 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"> </web-app>

web.xml為空

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <%--Created by IntelliJ IDEA.User: fishedeeDate: 28/11/2016Time: 9:11 PMTo change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html><head><title>Spring MVC</title></head><body>Hello World</body> </html>

home.jsp為簡單的jsp文件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.fishedee;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/*** Created by fishedee on 29/11/2016.*/ public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected String[] getServletMappings(){System.out.println("uu");return new String[]{"/"};}@Overrideprotected Class<?>[] getRootConfigClasses(){return new Class<?>[]{RootConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses(){return new Class<?>[]{WebConfig.class};} }

WebAppInitializer為入口的serlet文件,getRootConfigClasses指向通用bean的配置文件,getServletConfigClasses指向mvc使用的bean的配置文件

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.fishedee;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver;/*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc public class RootConfig {}

RootConfig基本為空

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package com.fishedee;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver;/*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc @ComponentScan public class WebConfig extends WebMvcConfigurerAdapter {@Beanpublic ViewResolver viewResolver(){InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix("/WEB-INF/views/");resolver.setSuffix(".jsp");resolver.setExposeContextBeansAsAttributes(true);return resolver;}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){configurer.enable();} }

WebConfig配置了視圖解析器,還有默認的路由處理

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.fishedee;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;/*** Created by fishedee on 29/11/2016.*/ @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(){return "home";} }

HomeController的代碼

增加tomcat的啟動選項

讓server指向到tomcat的安裝目錄就可以了,注意tomcat必須是7以上的版本

項目依賴中加入tomcat安裝目錄中lib文件的servlet-api.jar文件

打包選項中將Spring依賴都打包進去就可以了

最后,就是啟動服務器了,這時,你應該看到Hello World的輸出了

6.2 控制器

1 2 3 4 5 6 7 8 9 10 11 /*** Created by fishedee on 29/11/2016.*/ @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(){return "home";} }

簡單的controller,返回值是視圖的名稱

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.fishedee;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.ui.Model;/*** Created by fishedee on 29/11/2016.*/ @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(Model model){model.addAttribute("text","Hello Fish");return "home";} }

新增Model參數,將視圖的數據寫入到Model中

1 2 3 4 5 6 7 8 9 10 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html><head><title>Spring MVC</title></head><body><c:out value="${text}"/></body> </html>

home.jsp加入taglib,用c:out標簽輸出text參數

加入jstl與taglibs兩個庫

啟動后就能看到帶參數的視圖了

6.3 輸入

1 2 3 4 5 6 7 8 9 10 11 12 @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(@RequestParam("page") int page,@RequestParam(value="page2",defaultValue = "mmc") String page2,Model model){model.addAttribute("text","Hello Fish "+ page+","+page2);return "home";} }

控制器中處理Query的參數,用RequestParam就可以了

1 2 3 4 5 6 7 8 9 10 11 @Controller public class HomeController {@RequestMapping(value="/{spittleId}",method= RequestMethod.GET)public String home(@PathVariable("spittleId") int spittleId,Model model){model.addAttribute("text","Hello Fish "+ spittleId);return "home";} }

控制器中處理Path的參數,用PathVariable就可以了

6.4 表單

1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html><head><title>Spring MVC</title></head><body><form method="post">FirstName: <input type="text" name="firstName"/><br/>LastName: <input type="text" name="lastName"/><br/><input type="submit" value="提交"/></form></body> </html>

提交的表單

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(){return "home";}@RequestMapping(value="/",method= RequestMethod.POST)public String submit(User user){System.out.println(user.getFirstName()+","+user.getLastName());return "home";} }

添加控制器,將表單參數寫入到來自一個實體對象

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /*** Created by fishedee on 15/12/2016.*/ public class User {private String firstName;public void setFirstName(String firstName) {this.firstName = firstName;}public String getFirstName() {return firstName;}private String lastName;public void setLastName(String lastName) {this.lastName = lastName;}public String getLastName() {return lastName;}}

建立User對象即可

6.5 異常

1 2 3 4 5 6 7 8 9 10 @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(){throw new MyException();//return "home";}}

在Controller上拋出指定的異常

1 2 3 4 5 6 7 8 9 10 11 12 package com.fishedee;import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus;/*** Created by fishedee on 15/12/2016.*/ @ResponseStatus(value= HttpStatus.NOT_FOUND,reason = "找不到呀") public class MyException extends RuntimeException {}

如果異常上有ResponseStatus的標志,那么mvc的返回碼就會按照注解上的顯示

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(){throw new RuntimeException("mm");//return "home";}@ExceptionHandler(Exception.class)public String handleException(Model model){model.addAttribute("text","這里是異常呀");return "home";} }

通過在Controller類方法上增加ExceptionHandler來捕捉通用異常,并用特定的view來渲染錯誤

1 2 3 4 5 6 7 8 9 @ControllerAdvice public class MyHandler {@ExceptionHandler(Exception.class)public String handleException(Model model){model.addAttribute("text","這里是異常呀2");return "home";} }

新增ControllerAdvice捕捉所有Controller的異常

7 MVC的View

7.1 通用視圖解析器

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.fishedee;import org.springframework.web.servlet.View; import org.springframework.web.servlet.ViewResolver;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.util.Locale; import java.util.Map;/*** Created by fishedee on 15/12/2016.*/ public class MyViewResolver implements ViewResolver{@Overridepublic View resolveViewName(String var1, Locale var2) throws Exception{return new MyView();} }class MyView implements View{@Overridepublic String getContentType(){return "text/html;charset=utf-8";}@Overridepublic void render(Map<String,?> model, HttpServletRequest request, HttpServletResponse response)throws Exception{OutputStream outputStream = response.getOutputStream();String data = "<!doctype><html><head></head><body>jj</body></html>";response.addHeader("Content-Type","text/html;charset=utf-8");outputStream.write(data.getBytes("UTF-8"));} }

定義屬于自己的ViewResolver,相當的簡單

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc @ComponentScan public class WebConfig extends WebMvcConfigurerAdapter {@Beanpublic ViewResolver viewResolver(){return new MyViewResolver();}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){configurer.enable();} }

然后在WebConfig中將ViewResolver指向到自己的MyViewResolver即可

7.2 Thymeleaf解析器

加入thymeleaf的依賴

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 package com.fishedee;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ServletContextTemplateResolver; import org.thymeleaf.templateresolver.TemplateResolver;/*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc @ComponentScan public class WebConfig extends WebMvcConfigurerAdapter {@Beanpublic ViewResolver viewResolver(SpringTemplateEngine engine){ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();viewResolver.setTemplateEngine(engine);return viewResolver;}@Beanpublic SpringTemplateEngine templateEngine(TemplateResolver resolver){SpringTemplateEngine templateEngine = new SpringTemplateEngine();templateEngine.setTemplateResolver(resolver);return templateEngine;}@Beanpublic TemplateResolver templateResolver(){TemplateResolver templateResolver = new ServletContextTemplateResolver();templateResolver.setPrefix("/WEB-INF/views/");templateResolver.setSuffix(".html");templateResolver.setTemplateMode("HTML5");return templateResolver;}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){configurer.enable();} }

將ViewResolver指向ThymeleafViewResolver

1 2 3 4 5 6 7 8 9 10 11 12 <htmlxmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"/><title>Title</title> </head> <body><h1>Welcome!</h1><div th:text="${text}"></div> </body> </html>

建立一個Thymeleaf的模板,呃,明顯比用標簽變量的jsp要順眼多了

8 MVC的安全

8.1 基礎

引入security-web與security-config兩個依賴

1 2 3 4 5 6 7 8 9 10 package com.fishedee;import org.springframework.core.annotation.Order; import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;/*** Created by fishedee on 15/12/2016.*/ public class SecurityAppInitializer extends AbstractSecurityWebApplicationInitializer{ }

建立AbstractSecurityWebApplicationInitializer類,其會增加Security的Filter

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.fishedee;import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;/*** Created by fishedee on 15/12/2016.*/@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter{@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().denyAll();http.csrf().disable();} }

建立SecurityConfig,建立安全配置,默認為禁止所有的請求訪問

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /*** Created by fishedee on 29/11/2016.*/ public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected String[] getServletMappings(){System.out.println("uu");return new String[]{"/"};}@Overrideprotected Class<?>[] getRootConfigClasses(){return new Class<?>[]{RootConfig.class,SecurityConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses(){return new Class<?>[]{WebConfig.class};} }

在WebAppInitializer中將SecurityConfig.class加入到RootConfig中

這時候無論打開什么請求都會返回403返回了

8.2 身份認證

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter{@Overrideprotected void configure(AuthenticationManagerBuilder auth)throws Exception{auth.inMemoryAuthentication().withUser("fish").password("123").roles("USER","ADMIN").and().withUser("fish2").password("456").roles("USER");}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin();http.csrf().disable();} }

配置為所有請求都必須登錄后才能訪問

這時候請求所有請求都會跳轉到固定的/login頁面,登錄后自動跳轉到原有的請求頁面,注意,security指定的登出為/logout

8.3 獲取用戶

1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Controller public class HomeController {@RequestMapping(value="/",method= RequestMethod.GET)public String home(Model model){model.addAttribute("text","My Name is Fish");UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();System.out.println(userDetails);return "home";}}

在Controller層通過SecurityContextHolder.getContext獲取當前用戶的信息

9 數據庫

9.1 數據源

引入mysql-connector-java的庫

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc public class RootConfig {@Beanpublic DataSource dataSource(){DriverManagerDataSource ds = new DriverManagerDataSource();ds.setDriverClassName("com.mysql.jdbc.Driver");ds.setUrl("jdbc:mysql://localhost:3306/test");ds.setUsername("root");ds.setPassword("1");return ds;} }

RootConfig 中加入DataSource的配置,這里使用的是Spring的jdbc連接控制器

9.2 jdbc模板

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc public class RootConfig {@Beanpublic DataSource dataSource(){DriverManagerDataSource ds = new DriverManagerDataSource();ds.setDriverClassName("com.mysql.jdbc.Driver");ds.setUrl("jdbc:mysql://localhost:3306/test");ds.setUsername("root");ds.setPassword("1");return ds;}@Beanpublic JdbcOperations jdbcTemplate(DataSource ds){return new JdbcTemplate(ds);} }

RootConfig中加入jdbcTemplate

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Repository public class UserRepositoryImpl implements UserRepository{@Autowiredprivate JdbcOperations jdbcOperations;public List<User> findAll(){return jdbcOperations.query("select * from t_user", new RowMapper<User>() {@Overridepublic User mapRow(ResultSet resultSet, int i) throws SQLException {return new User(resultSet.getInt("userId"),resultSet.getString("name"),resultSet.getString("email"));}});} }

在UserRepositoryImpl中使用jdbcOperations來獲取數據,簡單暴力

1 2 3 4 5 6 7 /*** Created by fishedee on 16/12/2016.*/ public interface UserRepository {List<User> findAll(); }

簡單的UserRepository接口

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package com.fishedee;/*** Created by fishedee on 16/12/2016.*/ public class User {public User(){}public User(int userId,String name,String mail){this.userId = userId;this.name = name;this.mail = mail;}private int userId;public int getUserId(){return this.userId;}public void setUserId(int userId){this.userId = userId;}private String name;public String getName(){return this.name;}public void setName(String name){this.name = name;}private String mail;public String getMail(){return this.mail;}public void setMail(String mail){this.mail = mail;}}

無聊的User類

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Controller public class HomeController {@Autowiredprivate UserRepository userRepository;@RequestMapping(value="/",method= RequestMethod.GET)public String home(){List<User> users = userRepository.findAll();for( User user : users ){System.out.println(user.getName()+","+user.getMail());}return "home";}}

在HomeController中引入UserRepository,然后直接使用就可以了

10 緩存

10.1 緩存源

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @Configuration @EnableWebMvc @EnableCaching public class RootConfig {@Beanpublic CacheManager cacheManager(){return new ConcurrentMapCacheManager();}@Beanpublic DataSource dataSource(){DriverManagerDataSource ds = new DriverManagerDataSource();ds.setDriverClassName("com.mysql.jdbc.Driver");ds.setUrl("jdbc:mysql://localhost:3306/test");ds.setUsername("root");ds.setPassword("1");return ds;}@Beanpublic JdbcOperations jdbcTemplate(DataSource ds){return new JdbcTemplate(ds);} }

配置好CacheManager的bean,并且設置好EnableCaching的注解即可

10.2 方法注解

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 @Repository public class UserRepositoryImpl implements UserRepository{@Autowiredprivate JdbcOperations jdbcOperations;public List<User> findAll(){return jdbcOperations.query("select * from t_user", new RowMapper<User>() {@Overridepublic User mapRow(ResultSet resultSet, int i) throws SQLException {return new User(resultSet.getInt("userId"),resultSet.getString("name"),resultSet.getString("email"));}});}@Cacheable(value="mycache",key="#id")public User get(int id){System.out.println("repository get");return jdbcOperations.queryForObject("select * from t_user where userId = ?", new RowMapper<User>() {@Overridepublic User mapRow(ResultSet resultSet, int i) throws SQLException {return new User(resultSet.getInt("userId"),resultSet.getString("name"),resultSet.getString("email"));}},id);}@CacheEvict(key="#id",value="mycache")public void del(int id){System.out.println("repository del");jdbcOperations.update("delete from t_user where userId = ?",id);}@CachePut(key="#result.userId",value="mycache")public User add(final User user){System.out.println("repository add");KeyHolder keyHolder = new GeneratedKeyHolder();jdbcOperations.update(new PreparedStatementCreator(){public PreparedStatement createPreparedStatement(Connection conn)throws SQLException {PreparedStatement ps = conn.prepareStatement("insert into t_user(name,email)value(?,?)", Statement.RETURN_GENERATED_KEYS) ;ps.setString(1,user.getName());ps.setString(2,user.getMail());return ps ;}},keyHolder);user.setUserId(keyHolder.getKey().intValue());return user;} }

UserRepositoryImpl在方法中加入Cacheable注解(方法調用緩存),CacheEvict注解(方法調用完畢后刪除緩存),CachePut注解(方法調用完畢后增加緩存),注意緩存的key必須為同一個數據類型

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Controller public class HomeController {@Autowiredprivate UserRepository userRepository;@RequestMapping(value="/",method= RequestMethod.GET)public String home(){System.out.println("begin");userRepository.get(1);userRepository.get(1);System.out.println("get finish");User newUser = userRepository.add(new User(0,"mm3","mm3@qq.com"));userRepository.get(newUser.getUserId());System.out.println("add finish");userRepository.del(1);userRepository.get(1);System.out.println("del finish");return "home";}}

在HomeController中測試緩存的使用

1 2 3 4 5 6 7 begin repository get get finish repository add add finish repository del repository get

注意到了第二次get被緩存了,同時add以后也會走緩存了,而del以后也會強制走緩存了

11 消息

11.1 消息源

安裝activemq,注意用bin/macos下的activemq來啟動,能進入到管理員頁面才算成功

引入activemq-spring的包,以及jackjson的三個包

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 package com.fishedee;import org.apache.activemq.ActiveMQConnectionFactory; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.converter.MappingJackson2MessageConverter; import org.springframework.jms.support.converter.MarshallingMessageConverter; import org.springframework.jms.support.converter.MessageConverter; import org.springframework.web.servlet.config.annotation.EnableWebMvc;import javax.jms.ConnectionFactory;/*** Created by fishedee on 29/11/2016.*/ @Configuration @EnableWebMvc @EnableJms @ComponentScan public class RootConfig {@Beanpublic ConnectionFactory connectionFactory(){ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();connectionFactory.setBrokerURL("tcp://localhost:61616");return connectionFactory;}@Beanpublic MessageConverter messageConverter(){MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();converter.setTypeIdPropertyName("_type");return converter;}@Beanpublic JmsTemplate jmsTemplate(ConnectionFactory factory,MessageConverter messageConverter){JmsTemplate template = new JmsTemplate();template.setConnectionFactory(factory);template.setMessageConverter(messageConverter);return template;}@Beanpublic DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory factory2,MessageConverter messageConverter) {DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();factory.setConnectionFactory(factory2);factory.setMessageConverter(messageConverter);factory.setConcurrency("1-1");return factory;} }

在RootConfig中配置ConnectionFactory,JmsTemplate和DefaultJmsListenerContainerFactory,分別代表連接池,發送模板,接收池,最后,注意打開EnableJms注解,還有就是jackjson要配置TypeIdPropertyName

11.2 發布消息

1 2 3 4 5 6 7 8 9 10 11 12 13 @Controller public class HomeController {@AutowiredJmsTemplate jmsTemplate;@RequestMapping(value="/",method= RequestMethod.GET)public String home(){jmsTemplate.convertAndSend("myqueue",new User(1001,"fish","fish2"));return "home";}}

直接用convertAndSend發送消息,簡單暴力

11.3 接收消息

1 2 3 4 5 6 7 @Component public class MessageReceiver {@JmsListener(destination = "myqueue")public void receiveMessage(final User user) {System.out.println(user);} }

使用JmsListener注解來接收消息,依然也是簡單暴力

12 總結

Spring的IOP與AOP,配合Java中的注解,開發后臺相當的輕松簡單,唯一不爽的地方是

  • 引入外部依賴庫因為墻的緣故很慢
  • 配置太過麻煩,每次都要調好久
  • 與Servlet的耦合太緊密了,不能獨自啟動后臺

總體來說,整個設計還是非常值得參考的


from:?http://fishedee.com/%E5%90%8E%E7%AB%AF/2016/11/22/Spring%E5%AE%9E%E6%88%98%E7%AC%AC%E5%9B%9B%E7%89%88-%E7%9A%84%E8%AF%BB%E4%B9%A6%E7%AC%94%E8%AE%B0.html

總結

以上是生活随笔為你收集整理的《spring实战第四版》的读书笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。