Spring Boot该怎么来学 --- 来自菜鸟的经验

1.我们为什么来学Spring Boot?Spring Boot 有什么用?

   1.1这里就对比Spring来说(Spring Boot 是Spring 的一层壳),相比而言:

                1.1.1 Spring Boot 规约大于配置,大多数组件,不需要自行配置,而是自动组装!

                1.1.2  简化开发,大多数情况,使用默认即可!

                1.1.3 独立Spring 应用,不需要外部依赖,依赖容器(Tomcat)

                1.1.4 外部应用:Servlet 应用、Spring Web MVC、Spring Web Flux、WebSocket、WebService

                1.1.5 SQL:JDBC、JPA、ORM

                1.1.6  NoSQL(Not Only SQL):Redis、ElasticSearch、Hbase

2.spring Boot 里面大概包含了那些东西?

 

3.实战中我们必须了解Spring Boot 中那些基础知识?

   3.1 SpringApplication--Spring Boot 中驱动Spring Context(上下文)的重要的类

   3.1.1启动项目的两种写法,一种是SpringApplication.run(Demo1Application.class, args);

 

@SpringBootApplication
public class Demo1Application {

	public static void main(String[] args)
	{
		SpringApplication.run(Demo1Application.class, args);
	}
}

 3.2 SpringBoot中的事件 的两种方式,

       3.2.1.默认的事件

public class MyEventListern

{
	public static void main(String[] args) {

		GenericApplicationContext context = new GenericApplicationContext();
		context.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
		@Override
		public void onApplicationEvent(ApplicationEvent event)
		{
			System.out.println("为spring boot 添加也给监听事件" + event);
		}

		});
		//启动上下文
		context.refresh();
		//发布事件
		context.publishEvent("HelloWorld"); // 发布一个 HelloWorld 内容的事件
		//关闭事件
		context.close();

	}
}

 

@EnableAutoConfiguration
public class SpringBootEventDemo {

    public static void main(String[] args) {
        new SpringApplicationBuilder(SpringBootEventDemo.class)
                .listeners(event -> { // 增加监听器
                    System.err.println("监听到事件 : " + event.getClass().getSimpleName());
                })
                .run(args)
                .close();
        ; // 运行
    }
}

3.2.2自定义事件

public static void main(String[] args) {
		GenericApplicationContext context = new GenericApplicationContext();
		// 添加自义定监听器
		context.addApplicationListener(new ClosedListener());
		context.addApplicationListener(new RefreshedListener());
		// 启动 Spring 应用上下文
		context.refresh();
		// 一个是 ContextRefreshedEvent
		// 一个是 PayloadApplicationEvent
		// Spring 应用上下文发布事件
		// context.publishEvent("HelloWorld"); // 发布一个 HelloWorld 内容的事件
		// 一个是 MyEvent

		// 一个是 ContextClosedEvent
		// 关闭应用上下文
		context.close();
	}

	private static class RefreshedListener implements ApplicationListener<ContextRefreshedEvent> {

		@Override
		public void onApplicationEvent(ContextRefreshedEvent event) {
			System.out.println("上下文启动:" + event);
		}
	}

	private static class ClosedListener implements ApplicationListener<ContextClosedEvent> {

		@Override
		public void onApplicationEvent(ContextClosedEvent event) {
			System.out.println("关闭上下文:" + event);
		}
	}

3.2.3  监听的作用:Spring Boot 很多组件依赖于 Spring Boot 事件监听器实现,本质是 Spring Framework 事件/监听机制

SpringApplication 利用

  • Spring 应用上下文(ApplicationContext)生命周期控制 注解驱动 Bean 
  • Spring 事件/监听(ApplicationEventMulticaster)机制加载或者初始化组件

3.2.4.Spring 注解驱动运行过程

     驱动的两种方式:一种是Annotation(注解)驱动,第二种是xml配置文件驱动

  @ComponentScan -> @Confiugration Class -> basePackages -> @Component Beans ->

  BeanDefinition -> BeanDefinitionRegistry -> Beans -> BeanFactory -> getBean or @Autowired

 public static void main(String[] args) {



        //   XML 配置文件驱动       ClassPathXmlApplicationContext

        // Annotation 驱动

        // 找 BeanDefinition

        // @Bean @Configuration

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 注册一个 Configuration Class = SpringAnnotationDemo

        context.register(SpringAnnotationDemo.class);

        // 上下文启动

        context.refresh();



        System.out.println(context.getBean(SpringAnnotationDemo.class));



    }

4使用Spring Boot Demo?

          4.1.快速搭建一个Spring Boot 项目

                4.1.1 环境的搭建 (笔者用的是Eclipse(我不得不吐槽下笔者自己没有跟上潮流,现在主流并且强大的工具是idea))

                     第一步:将Eclipse集成好Maven、

                     第二步:下载好Eclipse的Spring Tool Suite:https://spring.io/tools/sts/all(插件)、

                     第三步:去spring 官网的上面有一个脚手架一样的(就是下载一个空的Demo),

                                    点击Quick Strat(位置在官网页面的下方),点击便可创建一个项目

 

              第四步:选择对应类型的项目,进行下载导入

    

   

第五步:导入Eclipse,我们直接运行Main方法启动项目(Spring Boot 不像Spring,他内嵌了Tomcat,jetty),笔者的项目是运行不起来的,也没有报错,对比了自己之前写的Spring Boot 项目,发现jar 包不一样。由是解决。

               官网下载项目pom.xml文件(这个在我的环境有问题)

              

我的pom.xml文件(可以正常启动)

差别就在jar包问题。

  启动之后,可以直接访问项目:说明访问成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值