1. 程式人生 > >spring和springmvc純註解整合

spring和springmvc純註解整合

首先在idea建立一個jar工程,不需要去建立任何配置檔案,也包括web.xml

 

首先寫spring的配置類()

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配置檔案類
*
*configuration   此註解標明配置類的身份
*下面的步驟相當於xml配置檔案中的開啟spring掃描,開啟預設註解,不過排除掉controller註解
*/
@Configuration
@ComponentScan(basePackages = "com.liy",useDefaultFilters = true,
    excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
            classes = Controller.class)})
public class SpringConfig {
}


 

然後是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 配置檔案類
 */
@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 {
        //把根目錄下的靜態資源放開,不過訪問靜態資源時,請求路徑前要加上“/js/”
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/js/**").addResourceLocations("classpath:/");
        }
 
        //給訪問jsp的請求加上字首(“/”) 和字尾 (".jsp")
        @Override
        protected void configureViewResolvers(ViewResolverRegistry registry) {
                registry.jsp("/",".jsp");
        }

        //這裡表示,訪問/hello3路徑後,進入名為hello的檢視去
        @Override
        protected void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/hello3").setViewName("hello");
        }


        //加了fastjson的依賴後,這裡配置引用fastjson,以及設定編碼
        @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);
        }
}

 

再寫個初始化的類,相當於web.xml,啟動專案就載入配置檔案

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;

/**
 * 相當於web.xml
 *
 */
public class WebInit implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //掃描springmvc
        AnnotationConfigWebApplicationContext afw =
                new AnnotationConfigWebApplicationContext();
        afw.register(SpringMVCConfig.class);
        //新增dispatchservlet
        ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(afw));

        //新增對映檔案路徑
        springmvc.addMapping("/");

        //給springmvc新增啟動時機
        springmvc.setLoadOnStartup(1);
    }
}

 

注意這個相當於web.xml的類這能掃描springmvc的配置類,所以要把spring的配置類的註解型別加到springmvc的掃描中

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

 

這樣spring和springmvc的純java註解方式就整合完成了

 

其實spring的配置類可以省略 ,只要springmvc的配置類掃描所有地方就行

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

 

這樣spring的配置類就不需要了

 

然後寫個controller類測試下

@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);
    }

 

頁面能看到資