Skip to content

Instantly share code, notes, and snippets.

@shui
Created July 11, 2018 05:23
Show Gist options
  • Save shui/4b104388b0422731026d93d7df869468 to your computer and use it in GitHub Desktop.
Save shui/4b104388b0422731026d93d7df869468 to your computer and use it in GitHub Desktop.
利用Spring Framework读取Properties

UrlProperties.java:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 利用Spring Framework读取Properties
 */
@Configuration
@PropertySource("classpath:url.properties")
@ConfigurationProperties(prefix = "url", ignoreUnknownFields = false)
@Data
public class UrlProperties {
    private String url1;
}

DevTest.java

@RestController
@EnableConfigurationProperties(UrlProperties.class)
public class DevTest {

    @Autowired
    UrlProperties1 urlProperties1;


    @GetMapping("/test")
    public String test() {
        return urlProperties1.getUrl1();
    }
}

url.properties:

url.url1=url1url1
@shui
Copy link
Author

shui commented Jul 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment