最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Spring boot application.properties not working - Stack Overflow

programmeradmin4浏览0评论

I'm having trouble getting spring boot (3.4.1) to read some parameters from a properties file. I was trying to use the default mechanism, naming my file application.properties and putting it in the resources folder. But since I couldn't get it to work, I decided to go the way of externalizing the properties file. So my code looks like this:

@PropertySource("file:/home/user/myapp/myapp.properties")
@SpringBootApplication
public class MyApplication {
    public static void main(String... args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

This seems to be correct because, if I delete (or rename) the myapp.properties file, when I try to initialize the application, it generates an error: APPLICATION FAILED TO START Config data resource 'file [/home/user/myapp/myapp.properties]' via location 'file:///home/user/myapp/myapp.properties' does not exist

So, assuming that spring is able to find the file, I configured my component to read a parameter like this:

@RestController
public class MyAppController {

    @Value("${staticRepo}")
    private String staticRepo;

    @GetMapping(value = "/api/myapp")
    List<Foo> getFoo() {
        if (staticRepo== null || staticRepo.trim().isEmpty()) {
            throw new InternalServerErrorException("MyApp is not configured properlly. Missing 'staticRepo' parameter in 'myapp.properties'");
        }
        return FooUtils.load(staticRepo);
    }

I double checked the myapp.properties and it does have a staticRepo parameter (the file contains only 1 line: staticRepo=/home/user/myapp/repo/)

I found several posts with the same problem, but none solved my issue. From one of those posts, I tried to specify the properties file into the command line as follows, but without luck...

java -jar /home/user/myapp/myapp.jar --spring.config.location=file:///home/user/myapp/myapp.properties

So my question is: what am i doing wrong?

EDIT

As suggested, I renamed the variable to repo and it still didn't work and I also verified the the @Value annotation. I'm using .springframework.beans.factory.annotation.Value.

And no, it isn't a dumbed down version. Yes, I changed the path and renamed the app, but the application is just what I posted. To be sure, using the VS Code (v1.96.2) wizard (Spring Initializr Java Support v0.11.2) I generated a new application from scratch. The wizard itself created an application.properties file and placed it into /src/main/resources with the property spring.application.name=demo. And yes, it prints null to the System.out... So probably my setup is messed up... By the way, it is an Ubuntu 24.04.2 LTS with openjdk 21.0.6.

@SpringBootApplication
public class DemoApplication {

    @Value("${spring.application.name}")
    private String name;

    public static void main(String[] args) {
        DemoApplication demo = new DemoApplication();
        System.out.println(demo.getName());

        SpringApplication.run(DemoApplication.class, args);
    }

    public String getName() {
        return name;
    }
}

I'm having trouble getting spring boot (3.4.1) to read some parameters from a properties file. I was trying to use the default mechanism, naming my file application.properties and putting it in the resources folder. But since I couldn't get it to work, I decided to go the way of externalizing the properties file. So my code looks like this:

@PropertySource("file:/home/user/myapp/myapp.properties")
@SpringBootApplication
public class MyApplication {
    public static void main(String... args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

This seems to be correct because, if I delete (or rename) the myapp.properties file, when I try to initialize the application, it generates an error: APPLICATION FAILED TO START Config data resource 'file [/home/user/myapp/myapp.properties]' via location 'file:///home/user/myapp/myapp.properties' does not exist

So, assuming that spring is able to find the file, I configured my component to read a parameter like this:

@RestController
public class MyAppController {

    @Value("${staticRepo}")
    private String staticRepo;

    @GetMapping(value = "/api/myapp")
    List<Foo> getFoo() {
        if (staticRepo== null || staticRepo.trim().isEmpty()) {
            throw new InternalServerErrorException("MyApp is not configured properlly. Missing 'staticRepo' parameter in 'myapp.properties'");
        }
        return FooUtils.load(staticRepo);
    }

I double checked the myapp.properties and it does have a staticRepo parameter (the file contains only 1 line: staticRepo=/home/user/myapp/repo/)

I found several posts with the same problem, but none solved my issue. From one of those posts, I tried to specify the properties file into the command line as follows, but without luck...

java -jar /home/user/myapp/myapp.jar --spring.config.location=file:///home/user/myapp/myapp.properties

So my question is: what am i doing wrong?

EDIT

As suggested, I renamed the variable to repo and it still didn't work and I also verified the the @Value annotation. I'm using .springframework.beans.factory.annotation.Value.

And no, it isn't a dumbed down version. Yes, I changed the path and renamed the app, but the application is just what I posted. To be sure, using the VS Code (v1.96.2) wizard (Spring Initializr Java Support v0.11.2) I generated a new application from scratch. The wizard itself created an application.properties file and placed it into /src/main/resources with the property spring.application.name=demo. And yes, it prints null to the System.out... So probably my setup is messed up... By the way, it is an Ubuntu 24.04.2 LTS with openjdk 21.0.6.

@SpringBootApplication
public class DemoApplication {

    @Value("${spring.application.name}")
    private String name;

    public static void main(String[] args) {
        DemoApplication demo = new DemoApplication();
        System.out.println(demo.getName());

        SpringApplication.run(DemoApplication.class, args);
    }

    public String getName() {
        return name;
    }
}
Share Improve this question edited Feb 17 at 21:13 Bob Rivers asked Feb 16 at 0:28 Bob RiversBob Rivers 5,4636 gold badges51 silver badges59 bronze badges 6
  • 1 Can you update my app.properties to rename staticRepo to static-repo and see if it’s working? – Imran Commented Feb 16 at 0:45
  • 3 Could not reproduce the same issue, everything works fine on my local. Since it does not work even for application.properties on your side, did you check any if custom spring config is set? Or are you using wrong Value annotation? – samabcde Commented Feb 16 at 2:54
  • This may be a few things. In the properties file the property name must be kebab not camel case, ie staticRepo should be static-repo. I would revert to the conventional /resources/application.properties file. Your build should put it in the 'BOOT-INF\classes\config\application.properties' location within the jar file, which is on the class-path. – John Williams Commented Feb 16 at 13:32
  • Putting the application.properties in your src/main/resources is all you need. If that doesn't work you are doing things wrong. Then moving the file to a different directory and try to work around Spring Boot is obviously not the solution. Judging from what you posted here it should have worked, so either what you have here isn't what you actually used but a dumbed down version that doesn't show the problem, or there is something weird in your project. In both cases there is too little information here to help you answer this. – M. Deinum Commented Feb 17 at 7:48
  • It worked perfectly fine for me in local. may be you can try using <context:property-placeholder> tag and see if it works. – DDK Commented Feb 17 at 13:41
 |  Show 1 more comment

2 Answers 2

Reset to default 2

This:

DemoApplication demo = new DemoApplication();
System.out.println(demo.getName());

does NOT start a Spring application, nor does it autowire any dependencies or inject configuration values into the object instance.

The call that initializes the application context is:

SpringApplication.run(DemoApplication.class, args);

And if you do that and let Spring manage your beans, you will see that the config properties are properly picked up.

Here's a minimal, fully working example:

@SpringBootApplication
public class DemoApplication {

    private final String name;

    public DemoApplication(@Value("${spring.application.name}") final String name) {
      this.name = name;

      System.out.println("name=" + name);
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public String getName() {
        return name;
    }
}

(It's generally a good idea to favor constructor injection of dependencies and values, unless there's a very good reason not to do so)

Can you try passing application config server using environment variable spring.config.import=optional:configserver:http://localhost:8888

Change your ports accordingly

发布评论

评论列表(0)

  1. 暂无评论