SpringBoot配置原理快速入门

对于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
    

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;
    }
    }

总结:

所有整合无非是配置bean进行整合,所以只需要把原有的xml形式改成javaBean形式即可。

如果看明白,完全可以自己动手去尝试整合其它技术。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

关注我们