从application.properties读取的属性,返回null

时间:2019-12-02 10:32:33

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

我是springboot的新手,我正尝试从location(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>

1 个答案:

答案 0 :(得分:0)

尝试一下:

public static void main(String[] args) {

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

    }