spring和springmvc純註解整合

首先在idea建立一個jar工程,不須要去建立任何配置文件,也包括web.xmlhtml

 

首先寫spring的配置類()java

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的配置類web

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,啓動項目就加載配置文件spring

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的掃描中json

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

 

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

 

其實spring的配置類能夠省略 ,只要springmvc的配置類掃描全部地方就行jsp

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

 

這樣spring的配置類就不須要了ide

 

而後寫個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);
    }

 

頁面能看到數據就表示成功了

相關文章
相關標籤/搜索