【问题标题】:Setting application.properties with command line arguments使用命令行参数设置 application.properties
【发布时间】:2019-09-20 18:44:48
【问题描述】:

我正在尝试设置文件 application.properties 从命令行传递参数。我不是弹簧靴的专家,我已经阅读了一些关于此的帖子和文章,但没有什么有用的。

我正在从 STS 运行配置中传递参数。我将两个参数传递给应用程序: --spring.datasource.url = jdbc:oracle:thin:@SERVERNAME:PORT:DB11G --spring.datasource.username = DBUSERNAME

@SpringBootApplication(scanBasePackages={"io.swagger.client","com.ids.app.controller","com.ids.app.service"})
@ConfigurationProperties("application.properties")

public class IdsFeApplication implements CommandLineRunner{



    private static final String FEC_CODEX = "A";

    @Autowired
    private static ConfigInfoDB infoDb;

    @Autowired
    private Login fec;




    public static void main(String[] args) {


        SpringApplication.run(IdsFeApplication.class, args);        

    }

    @Override
    public void run(String... args) throws Exception {

         infoDb.dbInfo();

    fec.token(FEC_CODEX);


    }

}

@Controller
@PropertySource(value = { "classpath:application.properties" })

public class ConfigInfoDB {

   @Value(value = "${spring.datasource.url}")
    private String dbConn;

    @Value(value = "${spring.datasource.username}")
    private String dbUser;




    public void dbInfo() {
        System.out.println("dbConn " +dbConn);
        System.out.println(" dbUser "+dbUser);
    }
}

这是我的 application.properties

spring.datasource.url = Anonymous
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
spring.datasource.username = Anonymous

我希望使用从命令行传递的 url 和用户名参数设置 application.properties,并使用这些参数连接到数据库。如果我在 application.properties 中使用静态参数,我的应用程序运行良好。

STS Configuration Run

@SpringBootApplication(scanBasePackages={"io.swagger.client","com.ids.app.controller","com.ids.app.service"})

public class IdsFeApplication implements ApplicationRunner{



    private static final String FEC_CODEX = "A";

    @Autowired
    private static ConfigInfoDB infoDb;

    @Autowired
    private Login fec;




    public static void main(String... args) throws Exception {

        SpringApplication.run(IdsFeApplication.class, args);        

    }


    @Override
    public void run(ApplicationArguments args) throws Exception {

        fec.token(FEC_CODEX);
    }

@Controller
public class ConfigInfoDB {

   @Value(value = "${spring.datasource.url}")
    private String dbConn;

    @Value(value = "${spring.datasource.username}")
    private String dbUser;




    public void dbInfo() {
        System.out.println("dbConn " +dbConn);
        System.out.println(" dbUser "+dbUser);
    }
}

【问题讨论】:

  • 我已经阅读了这些文章,但我的问题还没有解决。我使用您在帖子中发表的一篇文章重写了我的代码,但没有。您可以看到 STS 运行配置的图像。我正在传递这些论点。
  • 当我从 STS 运行配置运行应用程序时,我会得到“配置数据源失败:未指定 'url' 属性并且无法配置嵌入式数据源”。在 tha application.properties spring.datasource.url = Anonymous 它没有改变

标签: spring-boot


【解决方案1】:

我测试了code,它对我有用,所以我将发布我的代码 sn-ps。

这是我的属性文件:

person.name=anonymous

现在是主类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

控制器看起来像:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

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

    @GetMapping
    public String hello() {
        return "Hello, " + name + "!";
    }
}

如果您使用 mvn clean install 构建应用程序,您可以使用以下命令执行 jar 归档:

java -jar demo-0.0.1-SNAPSHOT.jar --person.name=flaxel

最后你可以浏览到这个网址:http://localhost:8080/

【讨论】:

  • 我做了同样的例子,我得到了同样的结果,但是当我从命令行传递地址 url 时,应用程序没有连接到数据库。在 application.properties 我写了:``` spring.datasource.url = Anonymous```。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 2016-02-02
  • 2023-03-07
  • 2015-05-03
  • 2021-12-02
相关资源
最近更新 更多