Guns基於SpringBoot,致力於作更簡潔的後臺管理系統,完美整合springmvc + shiro + 分頁插件PageHelper + 通用Mapper + beetl!Guns項目代碼簡潔,註釋豐富,上手容易,同時Guns包含許多基礎模塊(用戶管理,角色管理,部門管理,字典管理等10個模塊),能夠直接做爲一個後臺管理系統的腳手架.java
若是您有幸能看到,請認閱讀如下內容;git
一、本項目臨摹自abel533的Guns,他的項目 fork 自 stylefeng 的 Guns!開源的世界真好,能夠學到不少知識。github
二、版權歸原做者全部,本身只是學習使用。跟着大佬的思路,但願本身也能變成大佬。gogogo》。。web
三、目前只是一個後臺模塊,但願本身技能加強到必定時,能夠把stylefeng 的 [Guns]融合進來。spring
四、不少總結的文檔都來自abel533的GiHub的README.md.爲了方便本身複習就拿來主義了。瀏覽器
五、note裏面是本身的學習過程,菜鳥寫的,不是大佬寫的。內容都是大佬的。緩存
本項目對 Guns 的改動爲:tomcat
一、首先咱們來看包結構,老樣子,咱們先從core包開始,而後common,在config。等等,仍是先從啓動類開始吧。springboot
├─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目錄了.bash
一、先來看啓動類:
/** * 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註解和WebMvcConfigurerAdapter
(1)、1.2版本應該是@Configuretion註解,這個註解代表這個類會處理Spring的常規bean。來自《精通Spring MVC》
(2)、@ComponentScana 它會告訴Spring去哪裏查找SPring組件(服務,控制器),大白話就是bean那。通常咱們在控制層的類上會加上@Controller註解,不知道你們有木有配置過XML,難受啊。
(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啓動流程,知其因此然的同時也要知其然。
通常來講,初始化 步驟以下:
然而,然而有了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