@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();
}
}