@Value和@Configurationproperties和@Resource/@Autowired获取配置值

springboot @value和@configurationproperties

Spring注解@Resource和@Autowired区别对比

application.properties

neo.title=啊啊啊啊啊啊
neo.description=地对地导弹

other.properties

other.title=keep smile
other.blog=www

NeoProperties.java

package com.neo.comm;

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

@Component
@ConfigurationProperties(prefix="neo")
public class NeoProperties {
    private String title;
    private String description;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
View Code

OtherProperties.java

package com.neo.comm;

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

@Component
@ConfigurationProperties(prefix="other")
@PropertySource("classpath:other.properties")
public class OtherProperties {
    private String title;
    private String blog;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBlog() {
        return blog;
    }

    public void setBlog(String blog) {
        this.blog = blog;
    }
}
View Code

获取:

import org.springframework.beans.factory.annotation.Value;
 
import javax.annotation.Resource;
    
@Value("${neo.title}") private String title; @Resource private NeoProperties properties; @Resource private OtherProperties otherProperties; System.out.println("title:"+properties.getTitle());

 

posted @ 2020-04-15 03:10  假程序猿  阅读(619)  评论(0)    收藏  举报