Spring and spring MVC pure annotation integration

Keywords: Mobile Spring xml JSP Java

First of all, create a jar project in idea without creating any configuration files, including web.xml

 

First write the spring configuration class ()

package com.liy.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

/**
*spring Profile class
*
*configuration   This annotation indicates the identity of the configuration class
*The following steps are equivalent to turning on spring scanning in the xml configuration file, turning on the default annotation, but excluding the controller annotation
*/
@Configuration
@ComponentScan(basePackages = "com.liy",useDefaultFilters = true,
    excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
            classes = Controller.class)})
public class SpringConfig {
}


 

Then the configuration class of springmvc

package com.liy.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.charset.Charset;
import java.util.List;

/**
 *springmvc Profile class
 */
@Configuration
@ComponentScan(basePackages = "com.liy",
 useDefaultFilters = false,
 includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
 classes = Controller.class),
 @ComponentScan.Filter(type = FilterType.ANNOTATION,
 classes = Configuration.class)})
public class SpringMVCConfig extends WebMvcConfigurationSupport {
        //Release the static resources in the root directory, but when accessing the static resources, add "/ js /" before the request path
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/js/**").addResourceLocations("classpath:/");
        }
 
        //Prefix ("/") and suffix (". jsp") the request to access the jsp
        @Override
        protected void configureViewResolvers(ViewResolverRegistry registry) {
                registry.jsp("/",".jsp");
        }

        //This means that after accessing the / hello3 path, enter the view named hello
        @Override
        protected void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/hello3").setViewName("hello");
        }


        //After the fastjson dependency is added, you can configure the reference to fastjson and set the encoding
        @Override
        protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

                FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
                converter.setDefaultCharset(Charset.forName("UTF-8"));

                FastJsonConfig config = new FastJsonConfig();
                config.setCharset(Charset.forName("UTF-8"));

                converter.setFastJsonConfig(config);

                converters.add(converter);
        }
}

 

Write another initialization class, which is equivalent to web.xml. Start the project and load the configuration file

package com.liy.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Equivalent to web.xml
 *
 */
public class WebInit implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //Scan springmvc
        AnnotationConfigWebApplicationContext afw =
                new AnnotationConfigWebApplicationContext();
        afw.register(SpringMVCConfig.class);
        //Add dispatchservlet
        ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(afw));

        //Add map file path
        springmvc.addMapping("/");

        //Add start time to spring MVC
        springmvc.setLoadOnStartup(1);
    }
}

 

Note that this class is equivalent to web.xml, which can scan the configuration class of spring MVC, so you need to add the annotation type of the configuration class of spring to the scan of spring MVC

 @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)

 

In this way, the pure java annotation mode of spring and spring MVC is integrated

 

In fact, spring configuration classes can be omitted, as long as spring MVC configuration classes scan all places

@Configuration
@ComponentScan(basePackages = "com.liy")
public class SpringMVCConfig extends WebMvcConfigurationSupport {

 

In this way, the spring configuration class is not needed

 

Then write a controller class test

@Controller
public class HelloController {
    @Autowired
    HelloService hs;
    @GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String Hello(String name) {

        return hs.hello(name);
    }

 

If the page can see the data, it means success

Posted by Comtemplative on Sun, 03 Nov 2019 10:27:19 -0800