《spring实战第四版》的读书笔记
《spring實(shí)戰(zhàn)第四版》的讀書筆記
1 概述
《Spring實(shí)戰(zhàn)第四版》描述了Spring4架構(gòu)的設(shè)計(jì),看完了以后,最大感覺是Spring的IOC與aop理念實(shí)在是太強(qiáng)大了,而且用注解來簡化系統(tǒng)配置的想法也非常棒,整個(gè)架構(gòu)簡直就是MVC的典范
2 Spring之旅
2.1 Intellij IDEA
下載Intellij的15版本,然后將授權(quán)地址填寫為http://idea.iteblog.com/key.php
建立一個(gè)Spring項(xiàng)目
按圖建立對(duì)應(yīng)的文件
配置啟動(dòng)參數(shù)
建立Application的啟動(dòng)項(xiàng)
將入口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的配置文件
運(yùn)行程序后就能看到Embarking onquest的輸出了
在這個(gè)程序中可以看到Spring關(guān)于ioc的重要特點(diǎn)
- 依賴的對(duì)象不直接引用,而是只引用接口
- 對(duì)象的創(chuàng)建與注入由Spring來決定,Spring可以根據(jù)xml配置來創(chuàng)建對(duì)象
這樣的ioc就有很特別的能力了
- 依賴解耦,依賴對(duì)象只要滿足接口就能自由替換,不影響使用方的代碼
- 依賴封裝,Spring可以在注入對(duì)象時(shí),對(duì)對(duì)象執(zhí)行變形,例如封裝遠(yuǎn)程調(diào)用,mock打樁,進(jìn)行日志輸出等操作
例如,BraveKnight依賴的Quest對(duì)象,由于BraveKnight依賴的是接口,不是具體實(shí)現(xiàn),我們就能對(duì)Quest進(jìn)行很容易的mock,從而簡單地單元測(cè)試
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>
啟動(dòng)后看到Embark前后執(zhí)行對(duì)對(duì)應(yīng)的輸出
修改配置文件,將Minstrel聲明為切面,當(dāng)調(diào)用embarkOnQuest方法時(shí)會(huì)自動(dòng)回調(diào)Minstrel的方法
就這樣,Spring在不修改Knight與Quest的代碼下,就能在其方法執(zhí)行前后插入自己想要的代碼,這讓我們能達(dá)成簡單的cache,日志,事務(wù)等切面式的實(shí)現(xiàn)了
3 基礎(chǔ)ioc
Spring中提供三種裝配bean的方法
- 在xml中進(jìn)行顯式配置
- 在Java中進(jìn)行顯式配置
- 隱式的bean發(fā)現(xiàn)機(jī)制和自動(dòng)裝配
3.1 在xml中裝配
第2章已經(jīng)寫過,就不多說了
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.*/ public class Config {public Quest quest(){return new SlayDragonQuest();}public 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();} }啟動(dòng)ApplicationContext改用AnnotationConfigApplicationContext即可
使用Java裝配的好處是,強(qiáng)類型,支持豐富的java語法特性
3.3 自動(dòng)裝配
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.*/ 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.*/ public class BraveKnight implements Knight{private Quest quest;public BraveKnight(Quest quest){this.quest = quest;}public void embarkOnQuest(){quest.embark();} }將BraveKnight聲明為bean,同時(shí)Quest出現(xiàn)在構(gòu)造參數(shù)上,這個(gè)Quest類型會(huì)被自動(dòng)裝配
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.*/ public class BraveKnight implements Knight{private Quest quest;public BraveKnight(){}public void embarkOnQuest(){quest.embark();} }或者可以將Quest寫上Autowired注解,這個(gè)私有變量也會(huì)被自動(dòng)裝配
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();} }自動(dòng)裝配也支持單元測(cè)試,注意測(cè)試文件中指定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注解即可,非常簡單
自動(dòng)裝配能大幅度減少需要配置的bean,所以使用中一般是自動(dòng)裝配為主,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.*/ (Config2.class) ("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 高級(jí)ioc
4.1 環(huán)境裝配
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.*/ public class Config {("dev")public PrintStream printStream(){return System.out;}("prod")public PrintStream printStream2()throws Exception{return new PrintStream("fish.out");} }可以在Config上加入Profile注解,用來表明這個(gè)bean配置是在哪個(gè)環(huán)境上使用的,當(dāng)然也可以將Profile注解放到Config上,表明這個(gè)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();} }啟動(dòng)時(shí)可以根據(jù)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();} }單元測(cè)試中可以根據(jù)ActiveProfiles注解來指定環(huán)境
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.*/ public class Config {(ConfigCondition.class)public PrintStream printStream(){return System.out;}("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
所以,條件裝配是比環(huán)境裝配更為強(qiáng)大而動(dòng)態(tài)的方式而已。
4.3 指定裝配
1 No qualifying bean of type 'com.fishedee.knights.Quest' available: expected single matching bean but found 2: slayDragonQuest,slayHumanQuest如果我有兩個(gè)Quest都滿足Quest接口時(shí),Spring就會(huì)彈出錯(cuò)誤,說有歧義,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.*/ 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優(yōu)先級(jí),默認(rèn)選擇它
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.*/ public class BraveKnight implements Knight{("slayDragonQuest")private Quest quest;public BraveKnight(){}public void embarkOnQuest(){quest.embark();} }解決辦法二,讓Knight指定哪個(gè)Quest,用Qualifier注解
4.4 作用域
Spring創(chuàng)建的bean,有以下幾種作用域
- 單例(Singleton),整個(gè)應(yīng)用只有一個(gè)bean實(shí)例
- 原型(Prototype),每次都創(chuàng)建一個(gè)bean實(shí)例
- 會(huì)話(Session),在Web中,每個(gè)會(huì)話創(chuàng)建一個(gè)bean實(shí)例
- 請(qǐng)求(Request),在Web中,每個(gè)請(qǐng)求創(chuàng)建一個(gè)bean實(shí)例
默認(rèn)情況下,所有的bean都是單例會(huì)話域
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.*/ (value=ConfigurableBeanFactory.SCOPE_PROTOTYPE,proxyMode = ScopedProxyMode.INTERFACES ) public class BraveKnight implements Knight{("slayDragonQuest")private Quest quest;public BraveKnight(){}public void embarkOnQuest(){quest.embark();} }在bean中使用Scope注解就可以了,注意多例插入到單例對(duì)象中,需要用INTERFACES的proxy
5 aop
5.1 基礎(chǔ)切面
在Spring中,切面有以下的幾種
- After,在目標(biāo)方法返回或拋出異常后調(diào)用
- AfterReturning,在目標(biāo)方法返回后調(diào)用
- AfterThrowing,在目標(biāo)方法拋出異常后調(diào)用
- Before,在目標(biāo)方法調(diào)用之前調(diào)用
- Around,將目標(biāo)方法封裝起來
將Minstrel方法用Aspect注解圈起來,然后在觸發(fā)方法上,加入Pointcut,Before,After等觸發(fā)類型即可
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.*/ public class Config {public PrintStream printStream(){return System.out;} }配置上開啟EnableAspectJAutoProxy注解
5.2 切面參數(shù)
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.*/ public class Minstrel {private PrintStream stream;public Minstrel(){stream = System.out;}("execution(* *.embark(int)) && args(embarkPower)")public void quest(int embarkPower){}("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中指定包含參數(shù)的函數(shù)類型,以及args指定參數(shù)名稱,然后在Around上也指定接收參數(shù)即可。注意,在Around上要調(diào)用原函數(shù)。
6 基礎(chǔ)MVC
6.1 Intellij IDEA
創(chuàng)建一個(gè)勾選了MVC選項(xiàng)的Spring項(xiàng)目
建立以上的文件和文件夾
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 {protected String[] getServletMappings(){System.out.println("uu");return new String[]{"/"};}protected Class<?>[] getRootConfigClasses(){return new Class<?>[]{RootConfig.class};}protected 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.*/ public class WebConfig extends WebMvcConfigurerAdapter {public ViewResolver viewResolver(){InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix("/WEB-INF/views/");resolver.setSuffix(".jsp");resolver.setExposeContextBeansAsAttributes(true);return resolver;}public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){configurer.enable();} }WebConfig配置了視圖解析器,還有默認(rèn)的路由處理
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的啟動(dòng)選項(xiàng)
讓server指向到tomcat的安裝目錄就可以了,注意tomcat必須是7以上的版本
項(xiàng)目依賴中加入tomcat安裝目錄中l(wèi)ib文件的servlet-api.jar文件
打包選項(xiàng)中將Spring依賴都打包進(jìn)去就可以了
最后,就是啟動(dòng)服務(wù)器了,這時(shí),你應(yīng)該看到Hello World的輸出了
6.2 控制器
1 2 3 4 5 6 7 8 9 10 11 /*** Created by fishedee on 29/11/2016.*/ public class HomeController {(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參數(shù),將視圖的數(shù)據(jù)寫入到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標(biāo)簽輸出text參數(shù)
加入jstl與taglibs兩個(gè)庫
啟動(dòng)后就能看到帶參數(shù)的視圖了
6.3 輸入
1 2 3 4 5 6 7 8 9 10 11 12 public class HomeController {(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的參數(shù),用RequestParam就可以了
1 2 3 4 5 6 7 8 9 10 11 public class HomeController {(value="/{spittleId}",method= RequestMethod.GET)public String home(@PathVariable("spittleId") int spittleId,Model model){model.addAttribute("text","Hello Fish "+ spittleId);return "home";} }控制器中處理Path的參數(shù),用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 public class HomeController {(value="/",method= RequestMethod.GET)public String home(){return "home";}(value="/",method= RequestMethod.POST)public String submit(User user){System.out.println(user.getFirstName()+","+user.getLastName());return "home";} }添加控制器,將表單參數(shù)寫入到來自一個(gè)實(shí)體對(duì)象
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對(duì)象即可
6.5 異常
1 2 3 4 5 6 7 8 9 10 public class HomeController {(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的標(biāo)志,那么mvc的返回碼就會(huì)按照注解上的顯示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class HomeController {(value="/",method= RequestMethod.GET)public String home(){throw new RuntimeException("mm");//return "home";}(Exception.class)public String handleException(Model model){model.addAttribute("text","這里是異常呀");return "home";} }通過在Controller類方法上增加ExceptionHandler來捕捉通用異常,并用特定的view來渲染錯(cuò)誤
1 2 3 4 5 6 7 8 9 public class MyHandler {(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{public View resolveViewName(String var1, Locale var2) throws Exception{return new MyView();} }class MyView implements View{public String getContentType(){return "text/html;charset=utf-8";}public 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,相當(dāng)?shù)暮唵?/p> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /*** Created by fishedee on 29/11/2016.*/ public class WebConfig extends WebMvcConfigurerAdapter {public ViewResolver viewResolver(){return new MyViewResolver();}public 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.*/ public class WebConfig extends WebMvcConfigurerAdapter {public ViewResolver viewResolver(SpringTemplateEngine engine){ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();viewResolver.setTemplateEngine(engine);return viewResolver;}public SpringTemplateEngine templateEngine(TemplateResolver resolver){SpringTemplateEngine templateEngine = new SpringTemplateEngine();templateEngine.setTemplateResolver(resolver);return templateEngine;}public TemplateResolver templateResolver(){TemplateResolver templateResolver = new ServletContextTemplateResolver();templateResolver.setPrefix("/WEB-INF/views/");templateResolver.setSuffix(".html");templateResolver.setTemplateMode("HTML5");return templateResolver;}public 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>建立一個(gè)Thymeleaf的模板,呃,明顯比用標(biāo)簽變量的jsp要順眼多了
8 MVC的安全
8.1 基礎(chǔ)
引入security-web與security-config兩個(gè)依賴
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類,其會(huì)增加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,建立安全配置,默認(rèn)為禁止所有的請(qǐng)求訪問
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 {protected String[] getServletMappings(){System.out.println("uu");return new String[]{"/"};}protected Class<?>[] getRootConfigClasses(){return new Class<?>[]{RootConfig.class,SecurityConfig.class};}protected Class<?>[] getServletConfigClasses(){return new Class<?>[]{WebConfig.class};} }在WebAppInitializer中將SecurityConfig.class加入到RootConfig中
這時(shí)候無論打開什么請(qǐng)求都會(huì)返回403返回了
8.2 身份認(rèn)證
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class SecurityConfig extends WebSecurityConfigurerAdapter{protected void configure(AuthenticationManagerBuilder auth)throws Exception{auth.inMemoryAuthentication().withUser("fish").password("123").roles("USER","ADMIN").and().withUser("fish2").password("456").roles("USER");}protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().authenticated().and().formLogin();http.csrf().disable();} }配置為所有請(qǐng)求都必須登錄后才能訪問
這時(shí)候請(qǐng)求所有請(qǐng)求都會(huì)跳轉(zhuǎn)到固定的/login頁面,登錄后自動(dòng)跳轉(zhuǎn)到原有的請(qǐng)求頁面,注意,security指定的登出為/logout
8.3 獲取用戶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class HomeController {(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獲取當(dāng)前用戶的信息
9 數(shù)據(jù)庫
9.1 數(shù)據(jù)源
引入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.*/ public class RootConfig {public 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.*/ public class RootConfig {public 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;}public 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 public class UserRepositoryImpl implements UserRepository{private JdbcOperations jdbcOperations;public List<User> findAll(){return jdbcOperations.query("select * from t_user", new RowMapper<User>() {public User mapRow(ResultSet resultSet, int i) throws SQLException {return new User(resultSet.getInt("userId"),resultSet.getString("name"),resultSet.getString("email"));}});} }在UserRepositoryImpl中使用jdbcOperations來獲取數(shù)據(jù),簡單暴力
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 public class HomeController {private UserRepository userRepository;(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中引入U(xiǎn)serRepository,然后直接使用就可以了
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 public class RootConfig {public CacheManager cacheManager(){return new ConcurrentMapCacheManager();}public 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;}public JdbcOperations jdbcTemplate(DataSource ds){return new JdbcTemplate(ds);} }配置好CacheManager的bean,并且設(shè)置好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 public class UserRepositoryImpl implements UserRepository{private JdbcOperations jdbcOperations;public List<User> findAll(){return jdbcOperations.query("select * from t_user", new RowMapper<User>() {public User mapRow(ResultSet resultSet, int i) throws SQLException {return new User(resultSet.getInt("userId"),resultSet.getString("name"),resultSet.getString("email"));}});}(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>() {public User mapRow(ResultSet resultSet, int i) throws SQLException {return new User(resultSet.getInt("userId"),resultSet.getString("name"),resultSet.getString("email"));}},id);}(key="#id",value="mycache")public void del(int id){System.out.println("repository del");jdbcOperations.update("delete from t_user where userId = ?",id);}(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注解(方法調(diào)用緩存),CacheEvict注解(方法調(diào)用完畢后刪除緩存),CachePut注解(方法調(diào)用完畢后增加緩存),注意緩存的key必須為同一個(gè)數(shù)據(jù)類型
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中測(cè)試緩存的使用
1 2 3 4 5 6 7 begin repository get get finish repository add add finish repository del repository get注意到了第二次get被緩存了,同時(shí)add以后也會(huì)走緩存了,而del以后也會(huì)強(qiáng)制走緩存了
11 消息
11.1 消息源
安裝activemq,注意用bin/macos下的activemq來啟動(dòng),能進(jìn)入到管理員頁面才算成功
引入activemq-spring的包,以及jackjson的三個(gè)包
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.*/ public class RootConfig {public ConnectionFactory connectionFactory(){ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();connectionFactory.setBrokerURL("tcp://localhost:61616");return connectionFactory;}public MessageConverter messageConverter(){MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();converter.setTypeIdPropertyName("_type");return converter;}public JmsTemplate jmsTemplate(ConnectionFactory factory,MessageConverter messageConverter){JmsTemplate template = new JmsTemplate();template.setConnectionFactory(factory);template.setMessageConverter(messageConverter);return template;}public 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,分別代表連接池,發(fā)送模板,接收池,最后,注意打開EnableJms注解,還有就是jackjson要配置TypeIdPropertyName
11.2 發(fā)布消息
1 2 3 4 5 6 7 8 9 10 11 12 13 public class HomeController {JmsTemplate jmsTemplate;(value="/",method= RequestMethod.GET)public String home(){jmsTemplate.convertAndSend("myqueue",new User(1001,"fish","fish2"));return "home";}}直接用convertAndSend發(fā)送消息,簡單暴力
11.3 接收消息
1 2 3 4 5 6 7 public class MessageReceiver {(destination = "myqueue")public void receiveMessage(final User user) {System.out.println(user);} }使用JmsListener注解來接收消息,依然也是簡單暴力
12 總結(jié)
Spring的IOP與AOP,配合Java中的注解,開發(fā)后臺(tái)相當(dāng)?shù)妮p松簡單,唯一不爽的地方是
- 引入外部依賴庫因?yàn)閴Φ木壒屎苈?/li>
- 配置太過麻煩,每次都要調(diào)好久
- 與Servlet的耦合太緊密了,不能獨(dú)自啟動(dòng)后臺(tái)
總體來說,整個(gè)設(shè)計(jì)還是非常值得參考的
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
總結(jié)
以上是生活随笔為你收集整理的《spring实战第四版》的读书笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JDK7与JDK8中HashMap的实现
- 下一篇: Spring in Action 4 读