简体   繁体   中英

Spring Boot does not load application.properties

I am trying to load spring boot application.properties into java config class. But when I try to use the values, it is returning as null. I have followed everything as per online tutorials but not sure why it wont work. please guide me on this.

Application component Scan in XML File

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

Application Entry point - Command Line runner

@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());



}

}

Environment Config Class

@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;
}


}

This is where I call Environment values

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 ");
    }
    }

    }

Added Properties File below

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

You are creating the ClassPathXmlApplicationContext yourself, getting the Job and JobLauncher beans, creating job parameters and running the job.. That's not the Spring Boot way in the first place. Spring Boot does all that for you if you correctly configure your application. You need to import your xml configuration files:

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

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

and pass job parameters at startup:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM