Home > Enterprise >  How do I use a configuration class to configure properties for my application in Spring Boot?
How do I use a configuration class to configure properties for my application in Spring Boot?

Time:05-26

I am trying my hand at using a Spring Configuration class to set up configuration for my project. I created a new trial project for I did not want to edit my original project (Sorry for all this crap, but it seems the system does not forgive an almost all code post if the user is new)

DemoApplication.java:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    @Autowired
    public static Config configuration;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        String fullname = configuration.name   " "   configuration.surname   " "   configuration.age;
        System.out.println(fullname);   
    }

}

Config.java:

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:application.yml")
public class Config {
    @Value("${person.name}")
    public String name;

    @Value("${person.surname}")
    public String surname;

    @Value("${person.age}")
    public Integer age;

}

application.yml:

person:
  name: name1
  surname: surname1
  age: 25

Exception:

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
        at com.example.demo.DemoApplication.main(DemoApplication.java:15)
        ... 5 more

What have I done wrong?

CodePudding user response:

Here lies the problem

@Autowired
public ---> static <---- Config configuration;

Spring can't autowire static fields. You need to remove static from the autowired field

Then you would not be able to use this field inside the main method because main is declared as static. The solution here is the following. You need to retrieve the bean after the application context is loaded programmatically without the use of @Autowired

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {

    ConfigurableApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);

    Config configuration = ctx.getBean(Config.class);   

    String fullname = configuration.name   " "   configuration.surname   " "   configuration.age;
    System.out.println(fullname);   
  }

}

CodePudding user response:

The Config object is static,so it would be created at first,which is too late for spring container to take over the creation of object.

CodePudding user response:

The reason that you get null pointer exception, you can't initialize spring like that. Your static variable isn't initialized.

My suggestion:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements ApplicationRunner{

    @Autowired
    private Config configuration;

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

    @Override
    public void run(ApplicationArguments args) {
        String fullname = configuration.name   " "   configuration.surname   " "   configuration.age;
        System.out.println(fullname);
   } 

}
  • Related