【问题标题】:property read from application.properties returning null从 application.properties 读取的属性返回 null
【发布时间】:2020-03-26 22:56:03
【问题描述】:

我是 springboot 的新手,我正在尝试从位置 (src/main/resources) 中的 application.properties 文件中读取属性值。但它总是返回null。我也需要帮助。附加类和属性文件。 请注意:我尝试了与“https://www.baeldung.com/properties-with-spring”“How to access a value defined in the application.properties file in Spring Boot”不同的方法。 但这无助于从 application.properties 文件中获取值。

    @SpringBootApplication
    @EnableAutoConfiguration
    @PropertySource("classpath:application.properties")
    public class Application {

        public static void main(String[] args) {

            SpringApplication.run(Application.class, args);
            SampleTest ap=new SampleTest();
            System.out.println("+++++++++++++ "+ap.returnvalue());

        }


    @Configuration
    @PropertySource("classpath:application.properties")
    public class SampleTest {

        @Value("${testValue}")
        String value1;

        public String returnvalue()
        {
            return value1;
        }
    }

application.properties 文件

        testValue=Value from application

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.1.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>ReadValue</groupId>
        <artifactId>com.read.vale</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>com.read.vale</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    </project>

【问题讨论】:

  • 您正在自己创建SampleTest 的实例,因此Spring 无法处理它。请改用ApplicationContext 中的那个。

标签: spring-boot spring-mvc spring-boot-maven-plugin


【解决方案1】:

试试这个:

public static void main(String[] args) {

   ApplicationContext ctx = SpringApplication.run(Application.class, args);
   SampleTest ap = ctx.getBean(SampleTest.class);
   System.out.println("+++++++++++++ "+ap.returnvalue());

    }

【讨论】:

    猜你喜欢
    • 2018-10-16
    • 2016-06-03
    • 1970-01-01
    • 2014-09-26
    • 2020-11-17
    • 1970-01-01
    • 2023-02-12
    • 2023-01-30
    相关资源
    最近更新 更多