0

Below is my code and configuration, but I can't get properties' value, always null.


application.properties

app.myProperty=1234

class AppProperties:

@Component
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppProperties {

    private String myProperty;

    public String getMyProperty() {
        return myProperty;
    }

    public void setMyProperty(String myProperty) {
        this.myProperty = myProperty;
    }
}

Controller:

@RestController
@Slf4j
public class TestController {

    @Autowired
    AppProperties appProperties;

    @PostMapping(RoutePath.TEST)
    public ResultVO test() {

        try {

            log.info("property value:" + appProperties.getMyProperty());

            return ResultVOUtil.success(null);

       } catch (Exception ex) {

            return ResultVOUtil.error(CommonUtil.logExceptionError("发生错误。", null, ex));
        }
    }

}

log output:

property value:null

5
  • Remove @Component on AppProperties you don't need it Commented Apr 19, 2019 at 2:05
  • Possible duplicate of How to access a value defined in the application.properties file in Spring Boot Commented Apr 19, 2019 at 2:07
  • 1
    and also can show the package structure Commented Apr 19, 2019 at 2:11
  • @Value("${app.myProperty}") int myProperty user on @Configuration Commented Apr 19, 2019 at 2:29
  • @EnableConfigurationProperties({AppProperties.class}) Commented Apr 19, 2019 at 2:44

1 Answer 1

2

Use @Value annotation to read the values from application.properties

@Value("${myProperty}")
private String myProperty;
Sign up to request clarification or add additional context in comments.

1 Comment

I think that it should be with {...} characters: @Value("${myProperty}")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.