abdoe
abdoe

Reputation: 459

Spring Boot read values from application properties

I'm not sure if I understand it correctly, but from what I got, is that I can use @Value annotations to read values from my application.properties.

As I figured out this works only for Beans.

I defined such a bean like this

@Service
public class DBConfigBean {


    @Value("${spring.datasource.username}")
    private String userName;

    @Bean
    public String getName() {
        return this.userName;
    }
}

When the application starts I'm able to retrieve the username, however - how can I access this value at runtime?

Whenever I do

DBConfigBean conf = new DBConfigBean() conf.getName();

* EDIT *

Due to the comments I'm able to use this config DBConfigBean - but my initial problem still remains, when I want to use it in another class

@Configurable
public SomeOtherClass {

   @Autowired
   private DBConfigBean dbConfig; // IS NULL

   public void DoStuff() {
       // read the config value from dbConfig
   }
} 

How can I read the DBConfig in a some helper class which I can define as a bean

Thanks

Upvotes: 3

Views: 12125

Answers (4)

Eirini Graonidou
Eirini Graonidou

Reputation: 1566

You shouldn't instantiate your service with the new operator. You should inject it, for example

@Autowired
private DBConfigBean dbConfig;

and then dbConfig.getName();

Also you don't need any @Bean decorator in your getName() method

You just need to tell spring where to search for your annotated beans. So in your configuration you could add the following:

@ComponentScan(basePackages = {"a.package.containing.the.service",
                             "another.package.containing.the.service"})

EDIT

The @Value, @Autowired etc annotations can only work with beans, that spring is aware of.

Declare your SomeOtherClass as a bean and add the package config in your @Configuration class

@Bean
private SomeOtherClass someOtherClass;

and then

 @Configuration
 @ComponentScan(basePackages = {"a.package.containing.the.service"              
  "some.other.class.package"})
 public class AppConfiguration {

  //By the way you can also define beans like:
   @Bean 
   public AwesomeService service() {
       return new AwesomeService();
   }

 }

Upvotes: 2

Gaurav Shakya
Gaurav Shakya

Reputation: 121

Just add below annotation to your DBConfigBean class:

@PropertySource(value = {"classpath:application.properties"})

Upvotes: 0

iabughosh
iabughosh

Reputation: 551

Wrap your DBConfig with @Component annotation and inject it using @Autowired :

@Autowired
private DBConfig dbConfig;

Upvotes: 1

Simon Martinelli
Simon Martinelli

Reputation: 36223

As Eirini already mentioned you must inject your beans.

The @Value annotation only works on Spring beans.

There is another way of accessing configuration with @ConfigurationProperties. There you define a class that holds the configuration. The main advantage is, that this is typesafe and the configuration is in one place.

Read more about this: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-vs-value

Upvotes: 3

Related Questions