springboot-maven基础版及注册web三大组件

此说明针对仅创建了maven项目但需要整合springboot又不想要创建新项目使用的
我们大致需要的目录结构为以下结构,这里只是例举例子,可以根据要求进行改变:
在这里插入图片描述

  • 1.首先我们需要在pom里边引入jar包如下:
    (1)引入父工程,并且需要2020以及以上的idea版本
    在这里插入图片描述
    (2)引入web启动依赖
    在这里插入图片描述

  • 2.创建启动类并加入注解
    在这里插入图片描述
    注意: controller service mapper必须在主启动类所在的包下或者子包下,入,如果超出这个限制的话,将会无法生效

  • 3.Spring Boot工程中常见的配置文件类型(这里例举一些例子,具体以自己需要为主进行变化),下边以两种不同方式,实现相同效果

在这里插入图片描述
application.properties代码如下:

#配置端口号
server.port=8081
#配置tomcat延迟时间为2秒
server.tomcat.connection-timeout=2000
#配置基本属性
student.name=zs
student.age=14
student.sex=man
student.hobbys=one,two
student.address=beijing
#日期格式必须为"/"
student.birthday=2020/09/10
#map形式,必须有key和value值
student.map.k1=lisi
student.map.k2=wangwu
#set形式
student.sets=3,2
#实体形式
student.c.cid=1
student.c.cname=qy132

application.yml代码如下:

server:
  port: 8082
  tomcat:
    connection-timeout: 2000
student:
  name: zs
  age: 16
  sex: man
  address: beijing
  birtday: 2020/01/01
  hobby:
    -zz
    -aa
  map:
    k1:ls
    k2:ww
  sets:
    -zzz
    -lll
  c:
    cid:2
    cname:zzzz
  • 4.java读取springboot配置文件中的内
    容步骤。
    (1)通过@ConfigurationProperties(prefix = “student”)
    student代码
    在这里插入图片描述
    代码如下:
package com.zll.entity;

import com.zll.entity.Clazz;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Data//引入lombook依赖,Lombok能以简单的注解形式来简化java代码能够使代码更加简洁
@Component//注入服务,用于标注控制层,是组件,可被自动发现
@ConfigurationProperties(prefix = "student")//可以自定义实体类,映射yml或者properties文件,自动为对象bean属性捆绑数据。
// @ConfigurationProperties注解之前是location属性表明配置文件位置,prefix表示读取的配置信息的前缀,
public class Student {
    private String name;
    public Integer age;
    private String sex;
    private String address;
    private Date birthday;
    private List<String> hobbys;
    private Map<String,String> map;
    private Set<String> sets;
    private Clazz c;//实体类
}

clazz代码:
在这里插入图片描述
代码如下:

package com.zll.entity;

import lombok.Data;

@Data
public class Clazz {
    private  Integer cid;
    private  String cname;
}

5.测试类以及测试结果
在这里插入图片描述
浏览器结果:
在这里插入图片描述
代码如下:

package com.zll.controller;

import com.zll.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test {
    @Autowired//注入
    private Student student;
    @GetMapping("hello")//请求路径
    public String hello(){
        System.out.println("11111");
        System.out.println(student);
        return "helloword";
    }
}

二、springboot注册web三大组件(1. Servlet: 2. Filter:过滤器 3. Listener:监听器)
1.servlet
(1)创建servlet类
在这里插入图片描述
代码:

package com.zll.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {//继承HttpServlet
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("经过了GET方法");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("经过了POST方法");
    }
}

(2)创建配置类
在这里插入图片描述
代码如下:

package com.zll.config;

import com.zll.servlet.MyServlet;

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean();
        servletRegistrationBean.setServlet(new MyServlet());
        servletRegistrationBean.setName("myServlet");
        servletRegistrationBean.addUrlMappings("/my");
        return  servletRegistrationBean;
    }

}

2.filter
(1).创建filter类
在这里插入图片描述
代码如下:

package com.zll.filter;

import javax.servlet.*;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println("过滤器");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

(2)创建配置类
在这里插入图片描述
代码如下:

package com.zll.config;
import com.zll.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
        filterRegistrationBean.setName("myFilter");
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值