麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 09:01:43
字體:
來源:轉載
供稿:網友

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

1 概述

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

2 Spring之旅

2.1 Intellij IDEA

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

Screen Shot 2016-11-26 at 8.50.20 P

建立一個Spring項目

Screen Shot 2016-11-26 at 8.50.45 P

按圖建立對應的文件

Screen Shot 2016-11-26 at 8.51.14 P

配置啟動參數

Screen Shot 2016-11-26 at 8.51.43 P

建立application的啟動項

Screen Shot 2016-11-26 at 8.52.13 P

將入口Class指向到KnightMain

2.2 IOC

12345678package com.fishedee.knights;/** * Created by fishedee on 24/11/2016. */public interface Knight {    void embarkOnQuest();}

建立Kngiht接口

123456789101112131415161718package 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類

12345678package com.fishedee.knights;/** * Created by fishedee on 24/11/2016. */public interface Quest {    public void embark();}

建立Quest接口

12345678910111213141516171819package 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

123456789101112131415package 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

1234567891011<?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的配置文件

Screen Shot 2016-11-26 at 8.56.30 P

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

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

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

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

依賴解耦,依賴對象只要滿足接口就能自由替換,不影響使用方的代碼依賴封裝,Spring可以在注入對象時,對對象執行變形,例如封裝遠程調用,mock打樁,進行日志輸出等操作
12345678910111213141516171819package 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 {    @Test    public 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

1234567891011121314151617181920212223package 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類

1234567891011121314151617181920212223242526272829<?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/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/aop       http://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>

Screen Shot 2016-11-26 at 9.42.06 P

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

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

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

3 基礎ioc

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

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

3.1 在xml中裝配

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

3.2 在java中裝配

1234567891011121314151617181920package com.fishedee.knights;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * Created by fishedee on 26/11/2016. */@Configurationpublic class Config {    @Bean    public Quest quest(){        return new SlayDragonQuest();    }    @Bean    public Knight knight(Quest quest){        return new BraveKnight(quest);    }}

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

12345678910111213141516package 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 自動裝配

12345678910111213141516171819202122package com.fishedee.knights;import org.springframework.stereotype.Component;import java.io.PrintStream;import java.util.Queue;/** * Created by fishedee on 24/11/2016. */@Componentpublic 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即可

123456789101112131415161718192021package com.fishedee.knights;import org.springframework.stereotype.Component;import java.util.Queue;/** * Created by fishedee on 24/11/2016. */@Componentpublic class BraveKnight implements Knight{    private Quest quest;    public BraveKnight(Quest quest){        this.quest = quest;    }    public void embarkOnQuest(){        quest.embark();    }}

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

1234567891011121314151617181920212223package 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. */@Componentpublic class BraveKnight implements Knight{    @Autowired    private Quest quest;    public BraveKnight(){    }    public void embarkOnQuest(){        quest.embark();    }}

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

12345678910111213141516171819202122232425package 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 {    @Autowired    BraveKnight knight;    @Test    public void testEmbarkOnQuest() throws Exception {        knight.embarkOnQuest();    }}

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

123456789101112131415161718192021222324<?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/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-4.1.xsd       http://www.springframework.org/schema/context       http://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,并指定包名即可

12345678910111213package 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@ComponentScanpublic class Config {}

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

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

3.4 混合配置

12345678910111213package 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配置的方法

12345678910111213<?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/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/aop       http://www.springframework.org/schema/aop/spring-aop-4.1.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context-4.1.xsd">    <import resource="knights2.xml"/></beans>

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

4 高級ioc

4.1 環境裝配

12345678910111213141516171819202122232425package com.fishedee.knights;import org.springframework.context.annotation.*;import java.io.PrintStream;/** * Created by fishedee on 26/11/2016. */@Configuration@ComponentScanpublic 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指定的

123456789101112131415161718192021package 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

123456789101112131415161718192021222324252627package 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 {    @Autowired    BraveKnight knight;    @Test    public void testEmbarkOnQuest() throws Exception {        knight.embarkOnQuest();    }}

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

4.2 條件裝配

12345678910111213141516171819202122232425package com.fishedee.knights;import org.springframework.context.annotation.*;import java.io.PrintStream;/** * Created by fishedee on 26/11/2016. */@Configuration@ComponentScanpublic 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

1234567891011121314151617181920package 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 指定裝配

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

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

123456789101112131415161718192021222324package 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@Primarypublic 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優先級,默認選擇它

1234567891011121314151617181920212223242526package 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. */@Componentpublic 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都是單例會話域

123456789101112131415161718192021222324252627282930313233package 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,將目標方法封裝起來
123456789101112131415161718192021222324252627282930313233343536package 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@Aspectpublic 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等觸發類型即可

123456789101112131415161718package com.fishedee.knights;import org.springframework.context.annotation.*;import java.io.PrintStream;/** * Created by fishedee on 26/11/2016. */@Configuration@EnableAspectJAutoProxy@ComponentScanpublic class Config {    @Bean    public PrintStream printStream(){        return System.out;    }}

配置上開啟EnableAspectJAutoProxy注解

5.2 切面參數

1234567891011121314151617181920212223242526272829303132333435package 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@Aspectpublic 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

Screen Shot 2016-12-14 at 10.54.04 P

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

Screen Shot 2016-12-14 at 10.54.37 P

建立以上的文件和文件夾

123456<?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為空

12345678910111213141516<%--  Created by IntelliJ IDEA.  User: fishedee  Date: 28/11/2016  Time: 9:11 PM  To 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文件

123456789101112131415161718192021222324package com.fishedee;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;/** * Created by fishedee on 29/11/2016. */public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    @Override    protected String[] getServletMappings(){        System.out.println("uu");        return new String[]{"/"};    }    @Override    protected Class<?>[] getRootConfigClasses(){        return new Class<?>[]{RootConfig.class};    }    @Override    protected Class<?>[] getServletConfigClasses(){        return new Class<?>[]{WebConfig.class};    }}

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

12345678910111213141516171819package 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@EnableWebMvcpublic class RootConfig {}

RootConfig基本為空

1234567891011121314151617181920212223242526272829303132package 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@ComponentScanpublic class WebConfig extends WebMvcConfigurerAdapter {    @Bean    public ViewResolver viewResolver(){        InternalResourceViewResolver resolver = new InternalResourceViewResolver();        resolver.setPrefix("/WEB-INF/views/");        resolver.setSuffix(".jsp");        resolver.setExposeContextBeansAsAttributes(true);        return resolver;    }    @Override    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){        configurer.enable();    }}

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

1234567891011121314151617package 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. */@Controllerpublic class HomeController {    @RequestMapping(value="/",method= RequestMethod.GET)    public String home(){        return "home";    }}

HomeController的代碼

Screen Shot 2016-12-14 at 10.58.02 P

增加tomcat的啟動選項

Screen Shot 2016-12-14 at 10.58.21 P

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

Screen Shot 2016-12-14 at 10.59.06 P

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

Screen Shot 2016-12-14 at 10.59.50 P

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

Screen Shot 2016-12-14 at 11.00.32 P

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

6.2 控制器

1234567891011/** * Created by fishedee on 29/11/2016. */@Controllerpublic class HomeController {    @RequestMapping(value="/",method= RequestMethod.GET)    public String home(){        return "home";    }}

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

12345678910111213141516171819package 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. */@Controllerpublic class HomeController {    @RequestMapping(value="/",method= RequestMethod.GET)    public String home(Model model){        model.addAttribute("text","Hello Fish");        return "home";    }}

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

12345678910<%@ 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參數

Screen Shot 2016-12-15 at 7.36.53 P

加入jstl與taglibs兩個庫

Screen Shot 2016-12-15 at 7.37.24 P

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

6.3 輸入

123456789101112@Controllerpublic 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就可以了

1234567891011@Controllerpublic 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 表單

1234567891011121314<%@ 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>

提交的表單

123456789101112131415@Controllerpublic 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";    }}

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

1234567891011121314151617181920212223242526/** * 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 異常

12345678910@Controllerpublic class HomeController {    @RequestMapping(value="/",method= RequestMethod.GET)    public String home(){        throw new MyException();        //return "home";    }}

在Controller上拋出指定的異常

123456789101112package 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的返回碼就會按照注解上的顯示

123456789101112131415@Controllerpublic 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來渲染錯誤

123456789@ControllerAdvicepublic class MyHandler {    @ExceptionHandler(Exception.class)    public String handleException(Model model){        model.addAttribute("text","這里是異常呀2");        return "home";    }}

新增ControllerAdvice捕捉所有Controller的異常

7 MVC的View

7.1 通用視圖解析器

1234567891011121314151617181920212223242526272829303132333435package 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{    @Override    public View resolveViewName(String var1, Locale var2) throws Exception{        return new MyView();    }}class MyView implements View{    @Override    public String getContentType(){        return "text/html;charset=utf-8";    }    @Override    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,相當的簡單

1234567891011121314151617/** * Created by fishedee on 29/11/2016. */@Configuration@EnableWebMvc@ComponentScanpublic class WebConfig extends WebMvcConfigurerAdapter {    @Bean    public ViewResolver viewResolver(){        return new MyViewResolver();    }    @Override    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){        configurer.enable();    }}

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

7.2 Thymeleaf解析器

Screen Shot 2016-12-15 at 9.34.19 P

加入thymeleaf的依賴

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849package 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@ComponentScanpublic class WebConfig extends WebMvcConfigurerAdapter {    @Bean    public ViewResolver viewResolver(SpringTemplateEngine engine){        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();        viewResolver.setTemplateEngine(engine);        return viewResolver;    }    @Bean    public SpringTemplateEngine templateEngine(TemplateResolver resolver){        SpringTemplateEngine templateEngine = new SpringTemplateEngine();        templateEngine.setTemplateResolver(resolver);        return templateEngine;    }    @Bean    public TemplateResolver templateResolver(){        TemplateResolver templateResolver = new ServletContextTemplateResolver();        templateResolver.setPrefix("/WEB-INF/views/");        templateResolver.setSuffix(".html");        templateResolver.setTemplateMode("HTML5");        return templateResolver;    }    @Override    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){        configurer.enable();    }}

將ViewResolver指向ThymeleafViewResolver

123456789101112<html    xmlns="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 基礎

Screen Shot 2016-12-16 at 8.06.54 P

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

12345678910package 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

12345678910111213141516171819202122package 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@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter{    @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests().anyRequest().denyAll();        http.csrf().disable();    }}

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

1234567891011121314151617181920/** * Created by fishedee on 29/11/2016. */public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {    @Override    protected String[] getServletMappings(){        System.out.println("uu");        return new String[]{"/"};    }    @Override    protected Class<?>[] getRootConfigClasses(){        return new Class<?>[]{RootConfig.class,SecurityConfig.class};    }    @Override    protected Class<?>[] getServletConfigClasses(){        return new Class<?>[]{WebConfig.class};    }}

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

Screen Shot 2016-12-16 at 8.09.38 P

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

8.2 身份認證

123456789101112131415161718@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter{    @Override    protected void configure(AuthenticationManagerBuilder auth)throws Exception{        auth.inMemoryAuthentication()                .withUser("fish").passWord("123").roles("USER","ADMIN").and()                .withUser("fish2").password("456").roles("USER");    }    @Override    protected void configure(HttpSecurity http) throws Exception {        http.authorizeRequests().                anyRequest().authenticated().and().formLogin();        http.csrf().disable();    }}

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

Screen Shot 2016-12-16 at 8.13.11 P

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

8.3 獲取用戶

1234567891011121314@Controllerpublic 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 數據源

Screen Shot 2016-12-16 at 9.43.05 P

引入MySQL-connector-java的庫

12345678910111213141516/** * Created by fishedee on 29/11/2016. */@Configuration@EnableWebMvcpublic class RootConfig {    @Bean    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模板

123456789101112131415161718192021/** * Created by fishedee on 29/11/2016. */@Configuration@EnableWebMvcpublic class RootConfig {    @Bean    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;    }    @Bean    public JdbcOperations jdbcTemplate(DataSource ds){        return new JdbcTemplate(ds);    }}

RootConfig中加入jdbcTemplate

123456789101112131415161718@Repositorypublic class UserRepositoryImpl implements UserRepository{    @Autowired    private JdbcOperations jdbcOperations;    public List<User> findAll(){        return jdbcOperations.query("select * from t_user", new RowMapper<User>() {            @Override            public User mapRow(ResultSet resultSet, int i) throws SQLException {                return new User(                        resultSet.getInt("userId"),                        resultSet.getString("name"),                        resultSet.getString("email")                );            }        });    }}

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

1234567/** * Created by fishedee on 16/12/2016. */public interface UserRepository {    List<User> findAll();}

簡單的UserRepository接口

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748package 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類

12345678910111213141516@Controllerpublic class HomeController {    @Autowired    private 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 緩存源

123456789101112131415161718192021222324@Configuration@EnableWebMvc@EnableCachingpublic class RootConfig {    @Bean    public CacheManager cacheManager(){        return new ConcurrentMapCacheManager();    }    @Bean    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;    }    @Bean    public JdbcOperations jdbcTemplate(DataSource ds){        return new JdbcTemplate(ds);    }}

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

10.2 方法注解

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657@Repositorypublic class UserRepositoryImpl implements UserRepository{    @Autowired    private JdbcOperations jdbcOperations;    public List<User> findAll(){        return jdbcOperations.query("select * from t_user", new RowMapper<User>() {            @Override            public 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>() {            @Override            public 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必須為同一個數據類型

12345678910111213141516171819202122@Controllerpublic class HomeController {    @Autowired    private 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中測試緩存的使用

1234567beginrepository getget finishrepository addadd finishrepository delrepository get

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

11 消息

11.1 消息源

Screen Shot 2016-12-17 at 5.28.55 P

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

Screen Shot 2016-12-17 at 5.30.22 P

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657package 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@ComponentScanpublic class RootConfig {    @Bean    public ConnectionFactory connectionFactory(){        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();        connectionFactory.setBrokerURL("tcp://localhost:61616");        return connectionFactory;    }    @Bean    public MessageConverter messageConverter(){        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();        converter.setTypeIdPropertyName("_type");        return converter;    }    @Bean    public JmsTemplate jmsTemplate(ConnectionFactory factory,MessageConverter messageConverter){        JmsTemplate template = new JmsTemplate();        template.setConnectionFactory(factory);        template.setMessageConverter(messageConverter);        return template;    }    @Bean    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,分別代表連接池,發送模板,接收池,最后,注意打開EnableJms注解,還有就是jackjson要配置TypeIdPropertyName

11.2 發布消息

12345678910111213@Controllerpublic class HomeController {    @Autowired    JmsTemplate jmsTemplate;    @RequestMapping(value="/",method= RequestMethod.GET)    public String home(){        jmsTemplate.convertAndSend("myqueue",new User(1001,"fish","fish2"));        return "home";    }}

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

11.3 接收消息

1234567@Componentpublic 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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品久久久久久久久久了 | 日本黄色a视频 | 欧美性生话视频 | 久久精品一区二区三区四区五区 | 中文字幕网址 | 免费看真人a一级毛片 | 亚洲国产超高清a毛毛片 | 国产亚洲精品久久午夜玫瑰园 | 激情小说激情电影 | 免费一级欧美在线观看视频 | 国产精品免费一区二区三区四区 | 欧美一级淫片免费播放口 | 国产毛片在线 | 伊久在线| 久久久久久久一区二区三区 | 精品一区二区三区免费 | 成人在线观看小视频 | 色婷婷av一区二区三区久久 | 久久久久久久.comav | 美女一级视频 | 欧美一级在线免费 | 九九色在线观看 | 欧美中文字幕一区二区三区亚洲 | 在线成人毛片 | 色婷婷一区二区三区 | 国内精品久久久久久2021浪潮 | 久久精品国产清自在天天线 | 久久艹逼| xxxxxx性| 看片一区二区三区 | 亚洲第一视频 | 日韩一级片一区二区三区 | 欧美a在线 | 成人青青草 | 99在线免费观看视频 | 国产女厕一区二区三区在线视 | 成人影片在线免费观看 | 欧美视频在线一区二区三区 | omofun 动漫在线观看 | 久久亚洲精品国产一区 | 成人福利在线观看 |