基於SpringBoot的後臺管理系統(啓動類解析,開源的世界真好)(一)

Guns基於SpringBoot,致力於作更簡潔的後臺管理系統,完美整合springmvc + shiro + 分頁插件PageHelper + 通用Mapper + beetl!Guns項目代碼簡潔,註釋豐富,上手容易,同時Guns包含許多基礎模塊(用戶管理,角色管理,部門管理,字典管理等10個模塊),能夠直接做爲一個後臺管理系統的腳手架.java

說明

若是您有幸能看到,請認閱讀如下內容;git

  • 一、本項目臨摹自abel533的Guns,他的項目 fork 自 stylefengGuns!開源的世界真好,能夠學到不少知識。
  • 二、版權歸原做者全部,本身只是學習使用。跟着大佬的思路,但願本身也能變成大佬。gogogo》。。
  • 三、目前只是一個後臺模塊,但願本身技能加強到必定時,能夠把stylefeng 的 [Guns]融合進來。
  • 四、不少總結的文檔都來自abel533的GiHub的README.md.爲了方便本身複習就拿來主義了。
  • 五、note裏面是本身的學習過程,菜鳥寫的,不是大佬寫的。內容都是大佬的。

目錄

  • 一、SpringBoot第一站,分析了啓動類。還有各類自動配置的源碼點這裏
  • 二、

修改說明

本項目對 Guns 的改動爲:github

  • 一、將 mybatis-plus 改爲了通用 Mapper.
  • 二、增長分頁插件 PageHelper.
  • 三、去掉com.stylefeng.guns.modular.system.dao包中的全部DAO,將方法放到對應的Mapper接口中.
  • 四、將 Mapper.xml 移動到 resources 中

關於二者的對比,能夠經過 commit 信息查看。web

功能簡介

  • 一、用戶管理
  • 二、角色管理
  • 三、部門管理
  • 四、菜單管理
  • 五、字典管理
  • 六、業務日誌
  • 七、登陸日誌
  • 八、監控管理
  • 九、通知管理
  • 十、代碼生成

一、首先咱們來看包結構,老樣子,咱們先從core包開始,而後common,在config。等等,仍是先從啓動類開始吧。spring

項目包結構說明

├─main
│  │
│  ├─java
│  │   │
│  │   ├─com.guo.guns----------------項目主代碼(原來的包:com.stylefeng.guns)
│  │   │          │
│  │   │          ├─common----------------項目公用的部分(業務中常常調用的類,例如常量,異常,實體,註解,分頁類,節點類)
│  │   │          │
│  │   │          ├─config----------------項目配置代碼(例如mybtais-plus配置,ehcache配置等)
│  │   │          │
│  │   │          ├─core----------------項目運行的核心依靠(例如aop日誌記錄,攔截器,監聽器,guns模板引擎,shiro權限檢查等)
│  │   │          │
│  │   │          ├─modular----------------項目業務代碼
│  │   │          │
│  │   │          ├─GunsApplication類----------------以main方法啓動springboot的類
│  │   │          │
│  │   │          └─GunsServletInitializer類----------------用servlet容器啓動springboot的核心類
│  │   │
│  │   └─generator----------------mybatis-plus Entity生成器
│  │
│  ├─resources----------------項目資源文件
│  │     │
│  │     ├─gunsTemplate----------------guns代碼生成模板
│  │     │
│  │     ├─application.yml----------------springboot項目配置
│  │     │
│  │     └─ehcache.xml----------------ehcache緩存配置
│  │
│  └─webapp----------------web頁面和靜態資源存放的目錄
│

注:SpringBoot項目默認不支持將靜態資源和模板(web頁面)放到webapp目錄,可是我的感受resources目錄只放項目的配置更加簡潔,因此就將web頁面繼續放到webapp目錄了.瀏覽器

一、先來看啓動類:緩存

/**
 * SpringBoot方式啓動類
 */
@SpringBootApplication
public class GunsApplication extends WebMvcConfigurerAdapter {

    protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(GunsApplication.class,args);
        logger.info("GunsApplication is success!");
    }
}

須要注意兩個點:@SpringBootApplication註解和WebMvcConfigurerAdaptertomcat

(1)、1.2版本應該是@Configuretion註解,這個註解代表這個類會處理Spring的常規bean。來自《精通Spring MVC》springboot

(2)、@ComponentScana 它會告訴Spring去哪裏查找SPring組件(服務,控制器),大白話就是bean那。通常咱們在控制層的類上會加上@Controller註解,不知道你們有木有配置過XML,難受啊。mybatis

(3)、@EnableAutoConfiguration : 看名字,AutoConfiguration啊,這就是Spring魔力所在,省去不少XXML了,在這裏是基於JavaConfig配置的。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    Class<?>[] scanBasePackageClasses() default {};
}
-------------------------------------------------------
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

二、接下來,咱們再看下爲毛要繼承WebMvcConfigrerApapter類。

看見Config沒,這個也是配置類,它聲明瞭視圖解析器、地域解析器、以及靜態資源的位置,(想起來沒,就是前置,後置)

先看一段源碼 ————源碼是個好東西

----------------------InternalResourceViewResolver熟悉嗎?-----------------------
@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix(this.mvcProperties.getView().getPrefix());
    resolver.setSuffix(this.mvcProperties.getView().getSuffix());
    return resolver;
}
---------------------------也是視圖解析器,只是返回的是bean-------------------------
@Bean
@ConditionalOnBean({View.class})
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver() {
    BeanNameViewResolver resolver = new BeanNameViewResolver();
    resolver.setOrder(2147483637);
    return resolver;
}
-------------------------------地域解析器--------------------------------------------
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(
    prefix = "spring.mvc",
    name = {"locale"}
)
public LocaleResolver localeResolver() {
    if(this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.WebMvcProperties.LocaleResolver.FIXED) {
        return new FixedLocaleResolver(this.mvcProperties.getLocale());
    } else {
        AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
        localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
        return localeResolver;
    }
}

說了這麼多,咱們先來看看SpringMVC啓動流程,知其因此然的同時也要知其然。

通常來講,初始化 步驟以下:

  • 一、初始化SpringMVC的DispatcherServlet
  • 二、配置轉碼過濾器(UTF-8,亂碼鍋就在設置,還有一個就是在發送信息前,setCharacterEncoding()。),保證能正確轉碼,爲啥啊,由於瀏覽器發送的是ISO-8859?。
  • 三、配置視圖解析器,就上面說的那個,返回視圖的時候方便定位。
  • 四、配置靜態資源的位置,
  • 五、還有就是配置multipart解析器,主要是爲了能上傳文件,part單詞什麼意思?多個-部分
  • 六、還須要寫錯誤頁面,統一異常處理。

然而,然而有了SpringBoot,通通能夠省略,激動嗎?興奮嗎? 我是蠻激動的,尤爲第一次運行SpringBoot項目。

上面已經幫咱們位置了視圖解析器,接下來咱們看下DispatcherServlet和multipart

@AutoConfigureOrder(-2147483648)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({DispatcherServlet.class})     //只有對應的類在classpath中有存在時纔會注入
@AutoConfigureAfter({EmbeddedServletContainerAutoConfiguration.class})
public class DispatcherServletAutoConfiguration {
    public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";    //熟悉嗎?DeFAULT,默認的那。
    public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";

    public DispatcherServletAutoConfiguration() {
    }
-------------------------------MultipartResolver-------------------------------
    @Bean
    @ConditionalOnBean({MultipartResolver.class})
    @ConditionalOnMissingBean(
        name = {"multipartResolver"}
    )
    public MultipartResolver multipartResolver(MultipartResolver resolver) {
        return resolver;
    }
}

還有還有,錯誤配置、轉碼配置、tomcat配置Jetty等等。具體的在這個配置類中EmbeddedServletContainerAutoConfiguration,只看ContainerAutofig。咱們仍是正式進入項目吧。

/**
 * Guns Web程序啓動類
 */
public class GunsServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(GunsApplication.class);
    }
}

咱們點擊源碼看看SpringBootServletInitializer。其實看名字就可看出是Servlet初始化,熟悉設這個ApplicationContext單詞嗎?應用上下文。很重要的,還有一個叫作BeanFactory,主要有個getBean方法,通常用前者。不懂的能夠去看看我臨摹別人的簡單版Spring框架點這裏

public abstract class SpringBootServletInitializer implements WebApplicationInitializer {

    protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {}


    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder;
    }
}

今晚就先到這裏吧,明早gogogo

相關文章
相關標籤/搜索