A_Wen
A_Wen

Reputation: 813

How to get the content of file properties in Spring Boot?

As the title, my custom properties:

#app settings
my.chassisNum=10

java code:

@PropertySource("classpath:appconf.properties")
@ConfigurationProperties(prefix = "my" )
@Component
public class AppConfig {

    private String chassisNum;

    public String getChassisNum() {
        return this.chassisNum;
    }

    public void setChassisNum(String chassisNum) {
        this.chassisNum = chassisNum;
    }
}

when Spring Boot start completed, I got the "chassisNum" value is 10. when I got it in other place when spring-boot not start completed, it get "null"

@Component
public class CreateBaseFolder {

    private final Logger logger = LogManager.getLogger(CreateBaseFolder.class);
    private File f; 
    @Autowired
    AppConfig appconf;

    public CreateBaseFolder() {

        System.out.println(appconf.getChassisNum());


    } 

i try many way to get it value,but is false.such as :implements InitializingBean, @DependsOn....

Upvotes: 10

Views: 35793

Answers (4)

Vy Do
Vy Do

Reputation: 52516

Assume you has application.properties with content:

foo.bar=Jerry

You will use annotation @Value

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class GetPropertiesBean {

    private final String foo;

    @Autowired
    public GetPropertiesBean(@Value("${foo.bar}") String foo) {
        this.foo = foo;
        System.out.println(foo);
    }

}

Of course, we need an entry point

package com.example;

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

@SpringBootApplication
public class Application {

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

}

Then run class Application as Spring Boot application, component is autoload, you will see result at console screen

Jerry

enter image description here

Upvotes: 22

MJL
MJL

Reputation: 390

I did this with a Spring Boot Application, and for me, the problem was that I forgot to add

 implements ApplicationRunner 

to the class and then implement

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

public void run(ApplicationArguments args) throws Exception {
    //the variables are available here
}

Upvotes: -1

Bhushan Uniyal
Bhushan Uniyal

Reputation: 5703

You can achieve it by @Value annotation,This annotation can use at the method parameter as @Do Nhu Vy mentioned in his comment. Also you can use it with constructor parameter as well as a class level filed. e.g:

    @Service
    Class TestService {

   @Value("${my.chassisNum}")
   private String chassisNum;

    public void printChassisNum {
    System.out.println(chassisNum)
    }
    }

As per it's doc

Annotation at the field or method/constructor parameter level that indicates a default value expression for the affected argument.

Typically used for expression-driven dependency injection. Also supported for dynamic resolution of handler method parameters, e.g. in Spring MVC.

A common use case is to assign default field values using "#{systemProperties.myProp}" style expressions.

Note that actual processing of the @Value annotation is performed by a BeanPostProcessor which in turn means that you cannot use @Value within BeanPostProcessor or BeanFactoryPostProcessor types. Please consult the javadoc for the AutowiredAnnotationBeanPostProcessor class (which, by default, checks for the presence of this annotation).

Upvotes: 0

Prashant Kumar
Prashant Kumar

Reputation: 39

@Resource private Environment environment;

environment.getProperty("my.chassisNum");

via this you would be able to access the property of my.chassisNum.

Upvotes: 0

Related Questions