简体   繁体   English

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

[英]How do I use a configuration class to configure properties for my application in Spring Boot?

I am trying my hand at using a Spring Configuration class to set up configuration for my project.我正在尝试使用 Spring Configuration 类为我的项目设置配置。 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:演示应用程序.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:配置.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:应用程序.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?我做错了什么?

Here lies the problem这就是问题所在

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

Spring can't autowire static fields. Spring 不能自动装配static字段。 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.然后您将无法在main方法中使用此字段,因为main被声明为静态。 The solution here is the following.这里的解决方案如下。 You need to retrieve the bean after the application context is loaded programmatically without the use of @Autowired您需要在以编程方式加载应用程序上下文后检索 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);   
  }

}

Config对象是静态的,所以一开始会被创建,Spring容器接管对象的创建已经来不及了。

The reason that you get null pointer exception, you can't initialize spring like that.你得到空指针异常的原因,你不能像那样初始化spring。 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);
   } 

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性? - How can I use properties from a configuration (properties/yml) file in my Spring Boot application? "如何在 application.properties 文件中的 Spring Boot 应用程序中配置 HikariCP?" - How do I configure HikariCP in my Spring Boot app in my application.properties files? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何将 Spring Data JPA 正确配置到 Spring Boot 2.X 应用程序的 application.properties 配置文件中? - How to correctly configure Spring Data JPA into the application.properties configuration file of a Spring Boot 2.X application? 如何使用Maven在现有Spring Boot应用程序中使用属性文件? - How do I use Maven to Utilize Properties File with Existing Spring Boot Application? Spring Boot:使用 database 和 application.properties 进行配置 - Spring Boot: Use database and application.properties for configuration 如何为嵌入在我的 spring-boot 应用程序中的 Web 服务器(Tomcat)配置低级套接字属性? - How to configure low-level socket properties for a web server (Tomcat) embedded in my spring-boot application? 如何在Spring配置文件中使用可选属性文件? - How do I use an optional properties file in a Spring configuration file? Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 如何使用 Spring Boot 配置 Sniffy? - How do I configure Sniffy with Spring Boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM