【发布时间】:2025-12-12 14:30:01
【问题描述】:
我正在尝试将 spring boot application.properties 加载到 java config 类中。但是当我尝试使用这些值时,它返回为空。我按照在线教程遵循了所有内容,但不确定为什么它不起作用。请指导我。
在 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;
}
}
这就是我所说的环境值
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
【问题讨论】:
-
你能附上application.properties文件吗
-
嗨克劳斯。我已经添加了属性文件。请指教
-
查看属性文件的位置,应该在资源文件夹中
-
似乎没有问题,你得到的确切错误是什么,有什么例外吗?
-
我没有遇到任何异常。当我调用getter方法时它只是返回null。属性位于 src 主要资源中。它与命令行运行器方法有关吗?请指教
标签: java spring spring-boot spring-batch