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?