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

 

页面能看到数据就表示成功了

相关文章
相关标签/搜索