Spring Boot 注解详情一
程序员文章站
2022-03-07 11:53:30
@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
上一篇: 小程序文章列表超出1024kb的解决方案兼容ios和安卓
下一篇: java学习
推荐阅读
-
Spring Boot整合mybatis(一)实例代码
-
Spring boot 入门(一):快速搭建Spring boot项目
-
只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常
-
Spring Boot中使用LDAP来统一管理用户信息的示例
-
Spring Boot JPA如何把ORM统一起来
-
基于Spring注解的上下文初始化过程源码解析(一)
-
开发一个Spring Boot Starter!
-
[Spring Boot]使用自定义注解统一请求返回值
-
利用递归,反射,注解等,手写Spring Ioc和Di 底层(分分钟喷倒面试官)了解一下
-
spring Boot环境下dubbo+zookeeper的一个基础讲解与示例