绝对不可错过的SpringBoot最详细学习入门教程总结并解释原理

SpringBoot优点

  1. 快速构建项目
  2. 独立运行,从而提高开发效率(调试无需部署tomcat,省去web.xml配置)
  3. 对主流框架无配置集成(大量常规配置为默认配置)
  4. 提供运行时应用监控??
  5. 与云计算天然集成

Spring4.x推荐使用Java配置

  1. @Configuration 作用于类上,相当于一个xml配置文件
  2. @Bean 作用于方法上,相当于xml配置中的(ps:方法名为bean的id,故方法名命名按照对象名规范命名)
 	@Bean
    public LogDao logDao(){
        return new LogDao();
    }
    
    //③定义了logonService的Bean
    @Bean
    public LogonService logonService(){
        LogonService logonService = new LogonService();
                //④将②和③处定义的Bean注入到LogonService Bean中
        logonService.setLogDao(logDao());
        return logonService;
    }

相当于

	<bean id="logDao" class="com.baobaotao.anno.LogDao"/>
	<bean id="logService" class="com.baobaotao.conf.LogonService">
		<property name="logDao" ref="logDao"/>
	</bean>

SpringBoot快速入门

导入jar包

  1. 导入SpringBoot的parent(项目的parent必须为SpringBoot
  2. 导入SpringBoot的web包
  3. 导入SpringBoot的插件(可不加)

编写应用

  1. 应用类上加入@SpringBootApplication,目的是开启自动配置
  2. 通过main方法调用SpringApplication.run()方法启动
  3. 或者通过SpringBoot的插件进行启动
    在这里插入图片描述

SpringBoot核心

SpringBoot默认配置原理

Created with Raphaël 2.2.0 开始 在应用类上加入@SpringBootApplication 注解(@SpringBootApplication为复合注解 ,包含@EnableAutoConfiguration) 在全局application.propreties或 application.yml中进行配置配置 通过SpringApplication.run方法启动 结束
开发人员操作

Created with Raphaël 2.2.0 开始 SpringApplication.run运行前, 会通过SpringApplication#initialize 方法加载boot-autoconfigure的jar包 下的META-INF/spring.factories condition: spring.factories中的 Configuration所依赖的类即所 依赖的jar包被引入到classpath下 引入默认配置和在全局 application.propreties或 application.yml中的配置, 实例化的***Configuration类 结束 yes no
springboot运行操作

关闭SpringBoot自动配置

@SpringBootApplication(exclude = {RedisAutoConfiguration.class})

自定义Banner

  1. 打开网站:http://patorjk.com/software/taag/#p=display&h=3&v=3&f=4Max&t=itcast Spring Boot
  2. 拷贝生成的字符到一个文本文件中,并且将该文件命名为banner.txt
  3. 将banner.txt拷贝到项目的resources目录中:
  4. 重新启动程序,查看效果

注:关闭Banner方式如下

	SpringApplication app = new SpringApplication(SpringApplication.class);
    app.setBannerMode(Banner.Mode.OFF);
    app.run(args);

xml配置文件

通过如下注解引入

	@ImportResource({"classpath:som-context.xml"})	

SpringBoot的web开发

自定义SpringMVC的配置

  1. 通过全局配置配置mvc的属性
  2. 通过重写org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter该类的方法进行配置

加入Filter

  1. 加入实现了Filter接口的类
  2. 在类上加入@WebFilter设置过滤器的属性
  3. 在类上加入@Order设置过滤器的顺序

SpringBoot发布到tomcat运行

  1. pom文件中的package设置为war
  2. pom文件中的spring-boot-starter-tomcat改jar的scope设置为compile
  3. 入口类继承SpringBootServletInitializer,然后重写configure,将Spring Boot的入口类设置进去
  4. 打war包
  5. 部署到tomcat启动

调用其他服务的Http请求

  1. 依赖注入RestTemplate的实体
  2. 使用restTemplate进行方法调用即可

注:RestTemplate底层默认使用的jdk的标准实现,如果我们想让RestTemplate的底层使用okhttp,如下:

  1. 添加okhttp依赖
		<dependency>
			<groupId>com.squareup.okhttp3</groupId>
			<artifactId>okhttp</artifactId>
			<version>3.9.0</version>
		</dependency>

  1. 在RestTemplate的构造方法中引入OkHttp3ClientHttpRequestFactory的工厂类,从而在实际调用请求时,会使用代理进行调用
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
    }

SpringBoot配置文件引用

通过Spring引入

  1. 在application.yml或application.properties配置文件中配置
	sean:
	  item:
	    url: http://127.0.0.1:8081/item/
  1. 通过@Value注解进行引入
    @Value("${sean.item.url}")
    private String seanItemUrl;

通过SpringBoot引入

  1. 在application.yml或application.properties配置文件中配置
	sean:
	  item:
	    url: http://127.0.0.1:8081/item/
  1. 创建对应层级的配置类,并加入@ConfigurationProperties(prefix = “sean”)该注解,prefix为前缀,类中的属性名需要和配置文件中的对应
	@Component
	@ConfigurationProperties(prefix = "sean")
	@Data
	public class OrderProperties {
	    private ItemProperties item;
	}
	
	@Data
	public class ItemProperties {
	    private String url;
	}
  1. 引入配置的实体,然后直接调用
    @Autowired
    private OrderProperties orderProperties;

    /**
     *   方法的功能描述: 获取订单号的方法
     * @param id
     * @return
     */
    public Item queryItemById(Long id) {
        return restTemplate.getForObject(orderProperties.getItem().getUrl() + id, Item.class);
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值