SpringBoot配置原理快速入门

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> SpringBoot配置原理快速入门

Author:Alan

公众号:阿风的JAVA

本篇文章需要对spirng有所了解

对于Spring Boot其并不是一个框架。其整合原理是Sping对技术的整合。不过与传统xml不同的是它推荐使用的 java配置文件的整合形式。那么,Sping 的Java配置文件的形式是什么呢?

最初的sprng1.x开始,spring一直使用的是xml配置的形式。而从spring2.x因为jdk5的发布也引入了注解的形式

Java代码类配置形式则是Spring3.x出现的功能。

springboot的配置形式多数使用 是java代码类配置方式,这也是官方推荐的形式。

什么Java代码类的配置呢?在Spring中通过在类上方添加注解@Configuration,就能告知Spring这是一个注解类,而这个类的就相当于一个配置文件。

Java Configuration形式入门:

在此之前我们来接一下Java配置的基本注解:

Spring的Java配置方式是通过 @Configuration和 @Bean 这两个注解实现的:

1、@Configuration 作用于类上,相当于一个xml配置文件(需要被扫描);

2、@Bean 作用于方法上,相当于xml配置中的Bean;

快速入门案例:

  • 目录结构

    │-pom.xml
    src
    ├─main
    │  ├─java
    │  │  └─com
    │  │      └─lifeibai
    │  │          ├─conf
    │  │          │      BeanConfig.java ---Java配置文件类
    │  │          │
    │  │          └─pojo
    │  │                  User.java  ---pojo类
    │  │
    │  └─resources
    └─test
        └─java
            └─com
                └─lifeibai
                    └─test
                            MyTest.java ---简单测试文件
    
  • BeanConfig.java

    package com.lifeibai.conf;
    import com.lifeibai.pojo.User;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    //声明这是一个配置类
    @Configuration
    public class BeanConfig {
    //声明这是一个bean,默认的id是方法名称
    @Bean
    public User user(){
        return new User("小明","男",18);
    }
    }
    
  • User.java

    public class User {
    private String name;
    private String sex;
    private Integer  age;
    public User(String name , String sex , Integer age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    ...
    }
    
  • MyTest.java

    package com.lifeibai.test;
    import com.lifeibai.pojo.User;
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    public class MyTest {
    @Test
    public void test01(){
        //通过扫描包的方式扫描所有的javaconfig配置类
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.lifeibai.conf");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }
    }
    //以上运行结果
    User{name='小明', sex='男', age=18}
    Process finished with exit code 0
    
  • 
    package com.lifeibai.conf;
    import com.lifeibai.pojo.User;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    //声明这是一个配置类
    @Configuration
    public class BeanConfig {
    //声明这是一个bean,默认的id是方法名称
    @Bean
    public User user(){
        return new User("小明","男",18);
    }
    }
    
    
    package com.lifeibai.test;
    import com.lifeibai.pojo.User;
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    public class MyTest {
    @Test
    public void test01(){
        //通过扫描包的方式扫描所有的javaconfig配置类
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.lifeibai.conf");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }
    }
    //以上运行结果
    User{name='小明', sex='男', age=18}
    Process finished with exit code 0
    

    Sping Boot结合Java配置文件的形式

    @Configuration 需要被Spring扫描到

  • 目录结构

    │  pom.xml
    └─src
      ├─main
      │  ├─java
      │  │  └─com
      │  │      └─lifeibai
      │  │          ├─conf
      │  │          │      ConfDemo.java
      │  │          │
      │  │          ├─controller
      │  │          │      HelloWorldController.java
      │  │          │
      │  │          ├─po
      │  │          │      Student.java
      │  │          │
      │  │          └─springboot
      │  │                  SpringbootDemoApplication.java
      │  │
      │  ├─resources
      │  │      application.yml
      │  │
      │  └─webapp
      │          index.html
      │
      └─test
          └─java
    
  • SpringbootDemoApplication.java

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    package com.lifeibai.springboot;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
     
    @SpringBootApplication(scanBasePackages = {"com.lifeibai"})//自定义扫描包1
    //@ComponentScan(basePackages = {"com.lifeibai"})//自定义扫描包2
    public class SpringbootDemoApplication {
     public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
     }
    }

    @SpringBootApplication(scanBasePackages = {“com.lifeibai”})//自定义扫描包1
    //@ComponentScan(basePackages = {“com.lifeibai”})//自定义扫描包2
    public class SpringbootDemoApplication {
     public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
     }
    }

  • ConfDemo.java
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    package com.lifeibai.conf;
    import com.lifeibai.po.Student;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
     
    @Configuration
    public class ConfDemo {
      @Bean
      public Student student(){
          Student s = new Student();
          s.setName("Alan");
          s.setSex("male");
          s.setAge(18);
          return s;
      }
    }

    @Configuration
    public class ConfDemo {
      @Bean
      public Student student(){
          Student s = new Student();
          s.setName(“Alan”);
          s.setSex(“male”);
          s.setAge(18);
          return s;
      }
    }

  • HelloWorldController.java
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    package com.lifeibai.controller;
    import com.lifeibai.po.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
     
    @Controller
    public class HelloWorldController {
      @Autowired
      private Student student;
     
      @GetMapping("/hello")
      @ResponseBody
      public Student hello(){
          return student;
      }
    }

    @Controller
    public class HelloWorldController {
      @Autowired
      private Student student;

      @GetMapping(“/hello”)
      @ResponseBody
      public Student hello(){
          return student;
      }
    }

  • 123456789101112131415161718
    package com.lifeibai.controller;import com.lifeibai.po.Student;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.ResponseBody; @Controllerpublic class HelloWorldController {  @Autowired  private Student student;   @GetMapping("/hello")  @ResponseBody  public Student hello(){      return student;  }}

    总结:

    所有整合无非是配置bean进行整合,所以只需要把原有的xml形式改成javaBean形式即可。
    如果看明白,完全可以自己动手去尝试整合其它技术。

    文章如有错误,请您一定指出,感谢之至!
    如果你有不同的见解,欢迎留言
    图片可能来源于网络,如有侵权请告知。
    文章中的资料有时忘记书写来源,如果需求请告知
    最后:关注一下呗

    SpringBoot配置原理快速入门
    SpringBoot配置原理快速入门

    长按二维码识别关注

     

    本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

    本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

    原文链接:blog.ouyangsihai.cn >> SpringBoot配置原理快速入门


     上一篇
    SpringBoot自动配置原理入门 SpringBoot自动配置原理入门
    author:阿风/Alan 公众号:阿风的JAVA Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入到Sp
    下一篇 
    【加精】Spring全家桶系列–SpringBoot渐入佳境 【加精】Spring全家桶系列–SpringBoot渐入佳境
    //本文作者:cuifuan//本文将收录到菜单栏:《Spring全家桶》专栏中如果要想加入spring交流群,可以点这里: 萌新:小哥,我在实体类写了那么多get/set方法,看着很迷茫