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
推荐阅读
-
Spring Boot 注解详情一
-
Spring boot搭建的一些问题
-
基于spring Boot的微信开发————AccessToken的缓存方案(一)
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
详解配置spring-boot-actuator时候遇到的一些小问题
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
-
详解Spring boot/Spring 统一错误处理方案的使用
-
Spring Boot基础入门之基于注解的Mybatis
-
详解Spring boot/Spring 统一错误处理方案的使用
-
spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法