Spring Boot入门

一:SpringBoot入门

1:导入SpringBoot的Maven依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
  </parent>
  <groupId>hhxy</groupId>
  <artifactId>01-spring-boot-hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <!-- 修改jdk版本 -->
  <properties>
  	<java.version>1.8</java.version>
  </properties>
  
  <dependencies>
  <!-- springBoot的启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
</project>

2:编写Controller测试类

package com.lwk.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * SpringBoot HelloWorld
 * @author 李伟康
 *
 */
@Controller
public class HelloWorld {

	@RequestMapping("/hello")
	@ResponseBody
	public Map<String, Object> showHelloWorld(){
		Map<String, Object> map = new HashMap<>();
		map.put("msg", "HelloWorld");
		return map;
	}
}

2:编写SpringBoot启动类

/**
* SpringBoot 启动类
* @author 李伟康
*
*/
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

注:启动器存放的位置。启动器可以和 Controller 位于同一个包下,或者位于 controller 的上一级
包中,但是不能放到 Controller 的平级包以及子包下。

二:SpringBoot整合JavaEE

 

1:整合Servlet

1.1:通过注解扫描完成 Servlet 

1.1.1:编写Servlet

/**
*SpringBoot 整合 Servlet 方式一
*
*/
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("FirstServlet..........");
    }
}

1.1.2:编写启动类

/**
* SpringBoot 整合 Servlet 方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan //在 springBoot 启动时会扫描@WebServlet,并将该类实例化
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

1.2:通过方法完成Servlet的注册

1.2.1:编写Servlet

/**
*SpringBoot 整合 Servlet 方式二
*
*/
public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SecondServlet..........");
    }
}

1.2.2:编写启动类

/**
* SpringBoot 整合 Servlet 方式二
*
*
*/
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class, args);
    }
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
}

2:整合Filter

2.1:通过注解扫描完成 Filter 

2.1.1:编写Filter

@WebFilter(filterName="FirstFilter",urlPatterns="/first")
public class FirstFilter implements Filter {
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2)throws IOException, ServletException {
        System.out.println("进入 Filter");
        arg2.doFilter(arg0, arg1);
        System.out.println("离开 Filter");
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }
}

2.1.2:编写启动类

/**
*SpringBoot 整合 Filter 方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

2.2:通过方法完成 Filter 

2.2.1:编写 Filter

/**
*
*SpringBoot 整合 Filter 方式二
*
*/
public class SecondFilter implements Filter {
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2)throws IOException, ServletException {
        System.out.println("进入 SecondFilter");
        arg2.doFilter(arg0, arg1);
        System.out.println("离开 SecondFilter");
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }
}

2.2.2:编写启动类

/**
* SpringBoot 整合 Filter 方式二
*
*
*/
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class, args);
    }
    /**
    * 注册 Servlet
    * @return
    */
    @Bean
    public ServletRegistrationBean getServletRegistrationBean(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
    /**
    * 注册 Filter
    */
    @Bean
    public FilterRegistrationBean getFilterRegistrationBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
        bean.addUrlPatterns("/second");
        return bean;
    }
}

3:整合Listener

3.1:通过注解扫描完成 Listener

3.1.1:编写 Listener

@WebListener
public class FirstListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("Listener...init......");
    }
}

3.1.2:编写启动类

/**
* springBoot 整合 Listener 方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

3.2:通过方法完成 Listener 

3.2.1:  编写 Listener

/**
* springBoot 整合 Listener 方式二。
*
*
*/
public class SecondListener implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("SecondListener..init.....");
    }
}

3.2.2:编写启动类

/**
* SpringBoot 整合 Listener 方式二
*
*
*/
@SpringBootApplication
public class App2 {
    public static void main(String[] args) {
        SpringApplication.run(App2.class, args);
    }
    /**
    * 注册 listener
    */
    @Bean
    public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<SecondListener> bean= new
        ServletListenerRegistrationBean<SecondListener>(new SecondListener());
        return bean;
    }
}

4:访问静态资源

4.1:classpath/static目录下

4.2: ServletContext (webapp) 根目录下

5:文件上传

5.1:编写html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
	<form action="fileUploadController" method="post" enctype="multipart/form-data">
		上传文件:<input type="file" name="filename"/><br/>
		<input type="submit"/>
	</form>
</body>
</html>

5.2:编写Controller

/**
* SpringBoot 文件上传
*
*
*/
//@Controller
@RestController //表示该类下的方法的返回值会自动做 json 格式的转换
public class FileUploadController {
    /*
    * 处理文件上传
    */
    @RequestMapping("/fileUploadController")
    public Map<String, Object> fileUpload(MultipartFile filename)throws Exception{
        System.out.println(filename.getOriginalFilename());
        filename.transferTo(new File("e:/"+filename.getOriginalFilename()));
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "ok");
        return map;
    }
}

5.3:编写启动类

/**
* SpringBoot 文件上传
*
*
*/
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

5.4:resources下添加配置文件application.properties

#设置单个上传文件的大小
spring.http.multipart.maxFileSize=200MB
#设置一次请求上传文件的总容量
spring.http.multipart.maxRequestSize=200MB

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值