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

 

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

转载于:https://my.oschina.net/u/4116654/blog/3054756

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值