使用java代码来模仿SpringBoot达到没有xml文件,包括没有web.xml

 

 

1、首先创建一个maven项目javassm

加入如图所需的依赖

2、创建三个包分别时,config,controller,service

SpringConfig对应Spring的xml

SpringMvc对应SpringMvc的xml

WebInit对应Web.xml

 


SpringConfig.class


/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: springConfig
 * Author:   Administrator
 * Date:     2019/12/5 23:23
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hufei.javassm.config;

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

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2019/12/5
 * @since 1.0.0
 */
@Configuration
@ComponentScan(basePackages = "com.hufei.javassm",useDefaultFilters = true,
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
public class SpringConfig {

}

SpringMvc.class


/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: springMVCConfig
 * Author:   Administrator
 * Date:     2019/12/5 23:23
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hufei.javassm.config;

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

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2019/12/5
 * @since 1.0.0
 */
@Configuration
@ComponentScan(basePackages = "com.hufei.javassm",useDefaultFilters = false,
        includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class),
                @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Configuration.class)})
public class SpringMVCConfig {

}

 

@ComponentScan(basePackages = "com.hufei.javassm",useDefaultFilters = false,
        includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class),
                @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Configuration.class)})

上面的代码的意思是扫描所有包com.hufei.javassm

use-default-filters用来指示是否自动扫描带有@Component、@Repository、@Service和@Controller的类。默认为true
includeFilters:包括
excludeFilters:除去

WebInit.xml


/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: webInit
 * Author:   Administrator
 * Date:     2019/12/5 23:23
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hufei.javassm.config;

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

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

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2019/12/5
 * @since 1.0.0
 */
public class WebInit implements WebApplicationInitializer {

    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.setServletContext(servletContext);
        ctx.register(SpringMVCConfig.class);
        ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(ctx));
        springmvc.addMapping("/");
        springmvc.setLoadOnStartup(1);

    }
}

WebInit的代码其实就是把xml文件的配置转换成了代码

 

3、写一个controller和service类来测试

 


HelloService.class


/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: HelloService
 * Author:   Administrator
 * Date:     2019/12/7 4:11
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hufei.javassm.service;

import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2019/12/7
 * @since 1.0.0
 */
@Service
public class HelloService {

    public String sayhello() {
        return "NMSL,孙笑川!!!";
    }
}

HelloController


/**
 * Copyright (C), 2019-2019, XXX有限公司
 * FileName: HelloController
 * Author:   Administrator
 * Date:     2019/12/7 4:04
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hufei.javassm.controller;

import com.hufei.javassm.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Administrator
 * @create 2019/12/7
 * @since 1.0.0
 */
@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping(value = "/hello",produces = "text/html;charset=utf-8")

    public String hellow() {
        return helloService.sayhello();
    }
}

4、完成

效果图

 


版权声明:本文为qq_35953966原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。