gpt4 book ai didi

java - Spring Boot不加载application.properties

转载 作者:行者123 更新时间:2023-12-01 16:37:41 25 4
gpt4 key购买 nike

我正在尝试将 spring boot application.properties 加载到 java 配置类中。但是当我尝试使用这些值时,它返回为空。我已按照在线教程进行了所有操作,但不确定为什么它不起作用。请在这方面指导我。

应用程序组件扫描 XML 文件

<context:component-scan base-package="com.saimuga.abp"></context:component-scan>

应用程序入口点 - 命令行运行程序

@SpringBootApplication
public class FileUploadApplication implements CommandLineRunner{

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

//access command line arguments




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

System.out.println("args");
System.out.println(args[0]);


ApplicationContext ctx = new ClassPathXmlApplicationContext(
"ABPBatchInfrastructure.xml",
"FileUploadApp.xml"
);

JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
Job job = ctx.getBean(Job.class);

/*
* jobLauncher.run(job, new JobParametersBuilder().addString("inputResource",
* "file:./products.zip") .addString("targetDirectory",
* "./importproductsbatch/").addString("targetFile", "products.txt")
* .addString("date", "2020-06-02").toJobParameters());
*/

jobLauncher.run(job,
new JobParametersBuilder().addString("inputResource", "file:./products.zip")
.addString("targetDirectory", "./importproductsbatch/").addString("targetFile", "products.txt")
.addString("date", "2034-09-30").toJobParameters());



}

}

环境配置类

@ConfigurationProperties(prefix = "notification")
@Configuration("envProperties")
public class NotifyYaml {

private String test;

public NotifyYaml() {

}


/**
* @return the test
*/
public String getTest() {
return test;
}

/**
* @param test the test to set
*/
public void setTest(String test) {
this.test = test;
}


}

这就是我所说的环境值(value)观

public class ProductJdbcItemWriter implements ItemWriter<Product> {

private static final String INSERT_PRODUCT = "insert into product (id,name,description,price)
values(?,?,?,?)";

private static final String UPDATE_PRODUCT = "update product set name=?, description=?, price=? where
id = ?";

private JdbcTemplate jdbcTemplate;

@Autowired
private NotifyYaml envProperties;

public ProductJdbcItemWriter(DataSource dataSource) {
System.out.println("cxf");
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

/* (non-Javadoc)
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
*/
public void write(List<? extends Product> items) throws Exception {
System.out.println("cxf ProductJdbcItemWriter starts ");
System.out.println(envProperties.getTest());
for(Product item : items) {
int updated = jdbcTemplate.update(UPDATE_PRODUCT,
item.getName(),item.getDescription(),item.getPrice(),item.getId()
);
if(updated == 0) {
jdbcTemplate.update(
INSERT_PRODUCT,
item.getId(),item.getName(),item.getDescription(),item.getPrice()
);
}
System.out.println("cxf ProductJdbcItemWriter ends ");
}
}

}

在下面添加了属性文件

notification.test=test
spring.main.allow-bean-definition-overriding=true
spring.batch.job.enabled=false
spring.datasource.username=postgres
spring.jmx.enabled=false
spring.jmx.default-domain=abpservice
endpoints.jmx.domain=abpservice
endpoints.jmx.domain.unique-names=true
spring.application.admin.enabled=true

spring.application.admin.jmx-name=org.springframework.boot:type=Admin,name=SpringApplication

最佳答案

您自己创建 ClassPathXmlApplicationContext,获取 Job 和 JobLauncher beans,创建作业参数并运行作业。这首先不是 Spring Boot 的方式。如果您正确配置应用程序,Spring Boot 会为您完成所有这些工作。您需要导入 xml 配置文件:

@SpringBootApplication
@ImportResource({"classpath:ABPBatchInfrastructure.xml", "classpath:FileUploadApp.xml"})
public class FileUploadApplication {

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

并在启动时传递作业参数:

java -jar myjob.jar inputResource=products.zip // add other parameters

关于java - Spring Boot不加载application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61926841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com