如何使用配置类在Spring Boot中为我的应用程序配置属性?

发布于 2025-02-02 02:06:07 字数 2231 浏览 3 评论 0原文

我正在尝试使用春季配置类为项目设置配置。我创建了一个新的试用项目,因为我不想编辑我的原始项目(对不起,所有这些废话,但是如果用户是新的,该系统几乎没有宽恕所有代码帖子)

demoapplication.java.java:config.java:config.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);   
    }

}

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 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

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

肩上的翅膀 2025-02-09 02:06:07

这是问题所在的问题

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

。您需要从自动化字段中删除静态

,然后您将无法在main方法中使用此字段,因为main将其声明为静态。这里的解决方案是以下内容。在应用程序上下文通过编程加载后,您需要检索bean,而无需使用@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);   
  }

}

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

}
困倦 2025-02-09 02:06:07

配置对象是静态的,因此它首先会创建,这对于弹簧容器接管对象的创建为时已晚。

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.

喵星人汪星人 2025-02-09 02:06:07

您获得Null指针异常的原因,您不能这样初始化Spring。您的静态变量未初始化。

我的建议:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文