Guns, based on SpringBoot, is committed to creating a more concise background management system, perfectly integrating SpringMVC + Shiro + pagination plugin PageHelper + universal Mapper + Beetl! Guns project has simple code, rich annotation and easy to use. Meanwhile, Guns contains many basic modules (10 modules including user management, role management, department management and dictionary management), which can be directly used as the scaffolding of a background management system.

instructions

If you are lucky enough to see it, please read the following;

  • 1. This project is copied from the Guns of ABel533, and his project forks from the Guns of StyleFeng! Open source is a great place to learn.

  • 2, copyright belongs to the original author, I just learn to use. Follow the big guy’s idea, hope oneself also can become big guy. Gogogo…

  • 3. Currently, it is only a background module. I hope that when my skills are enhanced to a certain extent, I can integrate the [Guns] of StyleFeng into it.

  • 4. Many of the summarized documents come from Abel533’s GiHub readme.md. In order to facilitate their review took doctrine.

  • Note inside is their own learning process, written by the rookie, not the big guy. It’s all about the big guy.

directory

  • 1. The first station of SpringBoot is analyzed. There are also various auto-configurable source points here
  • 2,

Modify the description

The changes to Guns in this project are as follows:

  • 1. Changed Mybatis – Plus into general Mapper.
  • 2. Add the pagination plugin PageHelper.
  • 3, remove the com. Stylefeng. Guns. Modular. The system. All the dao in the dao package, put the method in the corresponding Mapper interface.
  • 4. Move mapper. XML to Resources.

Function introduction

  • 1. User management
  • 2. Role management
  • 3. Department management
  • 4. Menu management
  • 5. Dictionary management
  • 6. Service logs
  • 7. Login logs
  • 8. Monitoring and management
  • 9. Notification management
  • Code generation

1. First we’ll look at the package structure. As usual, we’ll start with the Core package, then common, in Config. Wait, let’s start with the startup class.

Description of project package structure

├ ─ the main │ │ │ ├ ─ Java │ │ │ │ │ ├ ─ com. Guo. Guns -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- project main code (the original package: Com. Stylefeng. Guns) │ │ │ │ │ │ │ ├ ─ common -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - part of the project utility (often call in business class, such as constants, exceptions, entity, annotations, paging, node class) │ │ │ │ │ │ │ ├ ─ config -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the project configuration code (for example mybtais - plus configuration, ehcache configuration, etc.) │ │ │ │ │ │ │ ├ ─ the core -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the core of the project running on (such as aop logging, interceptors, listener, guns template engine, shiro permission check, etc.) │ │ │ │ │ │ │ ├ ─ modular -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- project business code │ │ │ │ │ │ │ ├ ─ GunsApplication class -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the main method to start springboot class │ │ │ │ │ │ │ └ ─ GunsServletInitializer class -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - using the servlet container to start springboot core class │ │ │ │ │ └ ─ the generator -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - mybatis - plus The Entity generator │ │ │ ├ ─ resources -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - project resource files │ │ │ │ │ ├ ─ gunsTemplate -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - guns code generation templates │ │ │ │ │ ├ ─ application. Yml -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- springboot project configuration │ │ │ │ │ └ ─ ehcache. XML -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ehcache cache configuration │ │ │ ├ ─webapp---------------- Web page and directory for storing static resourcesCopy the code

Note: The SpringBoot project does not support static resources and templates (Web pages) in the WebApp directory by default, but I feel that the resources directory is simpler to configure only projects, so I will continue to put web pages in the WebApp directory.

1. Start class:

/** * 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!"); }}Copy the code

There are two points to note: the @SpringBootApplication annotation and the WebMvcConfigurerAdapter

(1) Version 1.2 should have the @Configuretion annotation, which indicates that this class handles Spring’s regular beans. From Mastering Spring MVC

(2) @ComponentScana tells Spring where to find Spring components (services, controllers), beans in plain English. Usually we put @Controller annotation on the class of the control layer, I don’t know if you’ve configured XML, it’s hard.

(3) @enableAutoConfiguration: Look at the name, AutoConfiguration, this is the magic of Spring, save a lot of XXML, here based on JavaConfig configuration.

@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 @interfaceSpringBootApplication { Class<? >[] scanBasePackageClasses()default{}; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
Copy the code

2. Next, we will look at the inheritance of webMVCConfigrerapter class.

See Config, this is also a configuration class that declares the location of view resolvers, region resolvers, and static resources.

First look at a piece of source ———— source is a good thing

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- InternalResourceViewResolver familiar? -----------------------@Bean
@ConditionalOnMissingBean
public InternalResourceViewResolver defaultViewResolver(a) {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix(this.mvcProperties.getView().getPrefix());
    resolver.setSuffix(this.mvcProperties.getView().getSuffix());
    returnresolver; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- is also the view parser, just return the bean -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --@Bean
@ConditionalOnBean({View.class})
@ConditionalOnMissingBean
public BeanNameViewResolver beanNameViewResolver(a) {
    BeanNameViewResolver resolver = new BeanNameViewResolver();
    resolver.setOrder(2147483637);
    returnresolver; } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - regional parser -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(
    prefix = "spring.mvc",
    name = {"locale"})public LocaleResolver localeResolver(a) {
    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());
        returnlocaleResolver; }}Copy the code

With that said, let’s take a look at the SpringMVC startup process and understand why as well as why.

In general, the initialization steps are as follows:

  • Initialize SpringMVC’s DispatcherServlet
  • 2. Configure the transcoding filter (UTF-8, setCharacterEncoding() before sending the message. To ensure correct transcoding, why, because the browser is sending ISO-8859? .
  • 3. Configure the view resolver, as described above, to locate the view when it returns.
  • 4. Configure the location of static resources.
  • The multipart parser is configured to upload files. Multi-part
  • 6, also need to write error page, unified exception processing.

However, however, with SpringBoot, all can be omitted. Excited? Excited? I was excited, especially to run the SpringBoot project for the first time.

Having helped us locate the view parser, let’s take a look at DispatcherServlet and multipart

@AutoConfigureOrder(-2147483648)
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({DispatcherServlet.class})     // Inject only if the corresponding class exists in the classpath
@AutoConfigureAfter({EmbeddedServletContainerAutoConfiguration.class})
public class DispatcherServletAutoConfiguration {
    public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";    // Familiar? DeFAULT, the DeFAULT one.
    public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";

    public DispatcherServletAutoConfiguration(a) {
    }
-------------------------------MultipartResolver-------------------------------
    @Bean
    @ConditionalOnBean({MultipartResolver.class})
    @ConditionalOnMissingBean(
        name = {"multipartResolver"})public MultipartResolver multipartResolver(MultipartResolver resolver) {
        returnresolver; }}Copy the code

Misconfiguration, transcoding configuration, Tomcat configuration Jetty, and so on. Specific in this configuration class EmbeddedServletContainerAutoConfiguration, only see ContainerAutofig. Let’s get into the program.

/** * Guns Web application startup class */
public class GunsServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        returnbuilder.sources(GunsApplication.class); }}Copy the code

We click on the source code to see SpringBootServletInitializer. Servlet initialization. Familiar with the “ApplicationContext” word? Application context. Most importantly, there’s another one called BeanFactory, which basically has a getBean method, usually the former. For those of you who don’t know, check out my simple copy of someone else’s Spring framework here

public abstract class SpringBootServletInitializer implements WebApplicationInitializer {

    protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {}


    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        returnbuilder; }}Copy the code

Let’s call it a night. Go go tomorrow morning