之前有错误的认识,@Value只能注入基本数据类型,但是其实是错误的,总结一下@Value特点:
1)@Value所有数据类型它都可以注入。
2)@Value注入不依赖set方法,@Autowired也不需要
一、Spring工程
建立Spring工程,不要使用Springboot
1.1、application.properties文件
username=mysql-root
clusterArray=shanghai,beijing,shenzhen
clusterList=hangzhou,guangzhou
clusterList2=tianjin_wuhan_fuzhou
cluster.weight={"shanghai":"90","beijing":"87","shenzhen":"85","guangzhou":"100"}
last_access = 2021-10-08T14:11:30
created_date = 20211008
1.2、创建配置类
创建一个配置类,用于加载配置文件
@Configuration
@PropertySource(value = "classpath:application.properties")
public class AppConfig {
@Bean
public Data getData() {
return new Data();
}
@Bean("accountService")
public AccountService getAccountService() {
return new AccountService();
}
}
public class AccountService {
}
public class Data {
@Value("${username}")
private String uname;
@Value("${clusterArray}") //默认逗号分隔
private String[] clusters;
@Value("${clusterList}") //默认逗号分隔
private List<String> clusterList;
@Value("#{'${clusterList2}'.split('_')}") //按照_分隔, 注意|是特殊字符
private List<String> clusterList2;
@Value("#{${cluster.weight}}")
private Map<String, String> weights;
// 通过SpEL表达式注入
@Value("#{T(java.time.LocalDateTime).parse('${last_access}')}")
private LocalDateTime lastAccess;
@Value("#{new java.text.SimpleDateFormat('yyyyMMdd').parse('${created_date}')}")
private Date createdDate;
// 通过SpEl表达式 注入其他bean对象
@Value("#{accountService}")
private AccountService service;
public Data() {
}
@Override
public String toString() {
return "Data{" +
"uname='" + uname + ''' +
", nclusters=" + Arrays.toString(clusters) +
", nclusterList=" + clusterList +
", nclusterList2=" + clusterList2 +
", nweights=" + weights +
", nlastAccess=" + lastAccess +
", ncreatedDate=" + createdDate +
", nservice=" + service +
'}';
}
}
//创建单元测试
public class MyTest {
@Test
public void test1() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
Data data = applicationContext.getBean("getData", Data.class);
System.out.println(data);
}
}
/*
输出结果:
Data{uname='mysql-root',
clusters=[shanghai, beijing, shenzhen],
clusterList=[hangzhou,guangzhou],
clusterList2=[tianjin, wuhan, fuzhou],
weights={shanghai=90, beijing=87, shenzhen=85, guangzhou=100},
lastAccess=2021-10-08T14:11:30,
createdDate=Fri Oct 08 00:00:00 CST 2021,
service=com.example.service.AccountService@1e66f1f5}
*/
由此可见对于比较复杂类型,我们需要借助SpEl表达式才能实现注入,到了Springboot就比较简单了,下面是SpringBoot做法。
二、SpringBoot工程
SpringBoot默认支持两种配置文件,properties(优先级高)和yml文件,使用哪种都可以
2.1、配置文件文件
application.properties配置文件,注意时间格式,不需要加双引号
app.username=mysql-root
app.clusterArray=shanghai,beijing,shenzhen
app.clusterList=hangzhou,guangzhou
app.cluster.weight={"shanghai":"90","beijing":"87","shenzhen":"85","guangzhou":"100"}
app.last_access = 2021-10-08 14:11:30
app.created_date = 20211008
server.port=9001
application.yml,时间戳需要加双引号
app:
username: mysql-root
clusterArray:
- shanghai
- beijing
- shenzhen
clusterList:
- hangzhou
- guangzhou
cluster:
weight:
shanghai: 90
beijing: 87
shenzhen: 85
guangzhou: 100
last_access: "2021-10-08 14:11:30"
created_date: "20211008"
server:
port: 9001
@Component
@ConfigurationProperties(prefix = "app")
public class Data {
private String userName;
private String[] clusterArray;
private List<String> clusterList;
private Cluster cluster;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime lastAccess;
@DateTimeFormat(pattern = "yyyyMMdd")
private Date createdDate;
// 通过SpEl表达式 注入其他bean对象, 这里用@Autowired也可以
@Value("#{accountService}")
private AccountService service;
public Data() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String[] getClusterArray() {
return clusterArray;
}
public void setClusterArray(String[] clusterArray) {
this.clusterArray = clusterArray;
}
public List<String> getClusterList() {
return clusterList;
}
public void setClusterList(List<String> clusterList) {
this.clusterList = clusterList;
}
public Cluster getCluster() {
return cluster;
}
public void setCluster(Cluster cluster) {
this.cluster = cluster;
}
public LocalDateTime getLastAccess() {
return lastAccess;
}
public void setLastAccess(LocalDateTime lastAccess) {
this.lastAccess = lastAccess;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Override
public String toString() {
return "Data{" +
"userName='" + userName + ''' +
", clusterArray=" + Arrays.toString(clusterArray) +
", clusterList=" + clusterList +
", cluster=" + cluster +
", lastAccess=" + lastAccess +
", createdDate=" + createdDate +
", service=" + service +
'}';
}
}
通过springboot工程,只需要使用@ConfigurationProperties注解,就可以注入大部分数据。有两点需要说明:
1)@ConfigurationProperties注解的类,需要有set方法,才能注入数据
2)@ConfigurationProperties中prefix不是必须的,如果不写idea会显示波浪线(可以忽略)
三、总结
通过对比可以发现SpringBoot的确很方便,对于复杂类型的注入也很轻松搞定
最后
以上就是独特月饼为你收集整理的Spring常用注解-@Value @ConfigurationProperties一、Spring工程二、SpringBoot工程2.1、配置文件文件三、总结的全部内容,希望文章能够帮你解决Spring常用注解-@Value @ConfigurationProperties一、Spring工程二、SpringBoot工程2.1、配置文件文件三、总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复