Springboot(其一)

1.定义:

springboot也是spring公司开发的一款框架。为了简化spring项目的初始化搭建的。spring项目搭建的缺点: [1]配置麻烦 [2]依赖 [3] tomcat启动慢

2.特点:

1) 自动配置

Spring Boot的自动配置是一个运行时(更准确地说,是应用程序启动时)的过程,考虑了众多因素,才决定Spring配置应该用哪个,不该用哪个。该过程是SpringBoot自动完成的。

2) 起步依赖

起步依赖本质上是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能。

3) 辅助功能

提供了一些大型项目中常见的非功能性特性,如嵌入式服务器tomcat、安全、指标,健康检测、外部配置等。

3.搭建springboot项目

4.springboot常用的配置文件种类

(1)application.yml
server:
  port: 8088
  servlet:
    context-path: /bbb
(2)applicatio.properties
#key=value
server.port=8080
server.servlet.context-path=/aaa

5.springboot中的java如何读取配置文件中的内容

(1)@congfigurationproperties(prefix=“前缀”)
(2)@value(只能读取基本类型和字符串类型)

6.profile多环境配置

(1)不同环境的的配置文件

application-dev.properties [开发环境的配置文件] application-test.properties [测试环境的配置文件] application-pro.properties [生产环境的配置文件] application.properties[存放相同配置]

(2)激活配置文件

激活对应的环境的配置文件:spring.profiles.active=pro

命令行参数:java –jar xxx.jar --spring.profiles.active=dev

7.springboot注册web组件

(1)创建一个servlet
//创建一个Servlet类
public class MyServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
}
​
​
//配置类
@Configuration //表示该类为配置类,等价于之前的xml配置文件
public class MyConfig {
​
    @Bean //等价于<bean标签.
    public ServletRegistrationBean  myServlet(){
        ServletRegistrationBean bean=new ServletRegistrationBean();
        bean.setServlet(new MyServlet());
        bean.setName("my");
        bean.setLoadOnStartup(1);
        bean.addUrlMappings("/my");
        return bean;
    }
}
(2)注册过滤器
//创建一个过滤器类
public class MyFilter implements Filter {
​
​
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter");
    }
}
​
//配置类
@Configuration
public class MyConfig {
    @Bean
    public FilterRegistrationBean filterRegistrationBean()  {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
​
    }
}

8.Spring Boot 自动配置原理是什么?

里面有一个 main 方法运行了一个 run()方法,在 run 方法中必须要传入一个带有@SpringBootApplication 注解的类,该注解是一个复合注解,它其中包含的@EnableAutoConfiguration开启了自动装配功能,同时该注解也是一个复合注解,里面包含@Import({AutoConfigurationImportSelector.class})的注解,该注解需要导入AutoConfigurationImportSelector自动装配选择器类,该类会自动加载很多自动装配。

9.Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?

启动类上面的注解是@SpringBootApplication,它也是Spring Boot 的核心注解,主要组合包含了以下 3 个注解: @SpringBootConfiquration: 组合了 @Configuration 注解,实现配置文件的功能。 @EnableAutoConfiquration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能:@SpringBootApplication(exclude =fDataSourceAutoConfiguration.class }). @ComponentScan:Spring组件扫描

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值