简体   繁体   中英

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

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

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

 }

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

@Autowired
private DBConfig dbConfig;

Just add below annotation to your DBConfigBean class:

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

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