我正在尝试设置文件application.properties从命令行传递参数。我不是Spring Boot的专家,我已经阅读了一些有关此问题的文章和文章,但是没有什么有用的。
我正在从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和username参数设置application.properties,并使用它们来连接数据库。如果我在application.properties中使用静态参数,我的应用程序将运行良好。
@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);
}
}
答案 0 :(得分:0)
我测试了code,它对我有用,因此我将发布代码段。
这是我的属性文件:
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
最后,您可以浏览到以下URL:http://localhost:8080/