欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Boot 注解详情一

程序员文章站 2023-12-29 14:47:16
@Value注解@TOC可以将属性值直接注入到 bean 中 @Value("张老师") private String tname; @Value("42") private int age; @Value("10101") private String number;@Component注解@TOC证明此组件是容器中的组件@ConfigurationProperties注解@TOC告知springboot本类与配置文件中相关配置绑定import...

@Value注解@
可以将属性值直接注入到 bean 中

  @Value("张老师")
    private String tname;

    @Value("42")
    private int age;

    @Value("10101")
    private String number;

@Component注解
证明此组件是容器中的组件

@ConfigurationProperties注解
告知springboot本类与配置文件中相关配置绑定

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@ConfigurationProperties(prefix = "student")
public class Student {

    private String name;

    private String[] hobby;

    private Study study;

    public String getName() {
        return name;
    }

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

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public Study getStudy() {
        return study;
    }

    public void setStudy(Study study) {
        this.study = study;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", hobby=" + Arrays.toString(hobby) +
                ", study=" + study +
                '}';
    }
}

@PropertySource注解
加载指定的配置文件,示例:

@PropertySource(value= {“classpath:student.properties”})

@ImportResource注解
读取自定义配置文件,示例:

@Configuration
@ImportResource(locations = "classpath:teacher.xml")
public class Teacher {

    @Value("张老师")
    private String tname;

    @Value("42")
    private int age;

    @Value("10101")
    private String number;




}

@Bean注解
声明此方法是一个需要被管理的bean

@Configuration
public class BeanConfigure {

    @Bean
    public Teacher teacher(){
        return new Teacher();
    }

}

本文地址:https://blog.csdn.net/Hamlet01/article/details/112280712

上一篇:

下一篇: