SpringBoot学习笔记——容器功能

目录

1、组件添加 

1.1、第一种方法:@Configuration 

1.2、第二种方法:@Component + @ConfigurationProperties

2、开发小技巧

2.1、Lombok


1、组件添加 

1.1、第一种方法:@Configuration

新建一个bean文件,在bean文件下新建Pet类和User类,利用快捷键Alt+Ins,添加有参无参构造器、Getter and Setter方法、toString()方法

Pet类:

public class Pet {

    private String name;

    public Pet() {
    }

    public Pet(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                '}';
    }
}

User类:

public class User {

    private String name;
    private Integer age;

    public User() {
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

新建一个config文件,在config文件下新建MyConfig类,给容器添加组件

@Configuration
public class MyConfig {

    @Bean  //给容器中添加组件,以方法名作为id
    public User user01(){
        return new User("zhangsan",18);
    }

    @Bean
    public Pet tom(){
        return new Pet("tomcat");
    }
}

在main函数中,从容器中获取组件

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //从容器中获取组件
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println(tom01 == tom02);  //判断获取的组件是否相等

    }
}

运行main函数,判断获取的组件是否相等

SpringBoot学习笔记——容器功能_第1张图片

1.2、第二种方法:@Component + @ConfigurationProperties

bean文件下新建Car类,利用快捷键Alt+Ins,添加有参无参构造器、Getter and Setter方法、toString()方法

Car类:

@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {

    private String brand;
    private Integer price;

    public Car() {
    }

    public Car(String brand, Integer price) {
        this.brand = brand;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public Integer getPrice() {
        return price;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

controller文件下的HelloController类中添加以下代码

@RestController
public class HelloController {

    @Autowired
    Car car;

    @RequestMapping("/car")
    public Car car(){
        return car;
    }
}

在application.properties文件中添加

mycar.brand=BYD
mycar.price=100000

运行main函数,查看输出结果

SpringBoot学习笔记——容器功能_第2张图片

2、开发小技巧

2.1、Lombok

在pom.xml文件中添加Lombok依赖


    org.projectlombok
    lombok

idea中搜索安装lombok插件:File --> Settings --> Plugins --> 搜索lombok --> 安装 --> 重启idea

SpringBoot学习笔记——容器功能_第3张图片

简化JavaBean开发

@NoArgsConstructor            //无参构造
@AllArgsConstructor            //有参构造
@Data                      //Getter and Setter方法
@ToString                  //toString()方法
@EqualsAndHashCode
public class User {

    private String name;
    private Integer age;

    private Pet pet;
}

SpringBoot学习笔记 

你可能感兴趣的:(Spring,Boot,spring,boot)