Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Application.properties未拾取是静态值_Java_Spring_Maven - Fatal编程技术网

Java Application.properties未拾取是静态值

Java Application.properties未拾取是静态值,java,spring,maven,Java,Spring,Maven,我有一个简单的spring启动应用程序,需要从application.properties文件打印属性值 Application.properties文件存储在src/main/resources中 在这里,我将简化我的问题,并给出我在应用程序中使用的一些示例代码 下面是我试图获取值的代码 @SpringBootApplication public class Application { @Value("${secret.property}") static String secret;

我有一个简单的spring启动应用程序,需要从application.properties文件打印属性值

Application.properties文件存储在src/main/resources中

在这里,我将简化我的问题,并给出我在应用程序中使用的一些示例代码

下面是我试图获取值的代码

@SpringBootApplication    
public class Application {
@Value("${secret.property}")
static String secret;

public static void main(String[] args) throws Exception {
    System.out.println("Hello World!");
    SpringApplication.run(Application.class, args);
}


public static void run(String... arg0) throws Exception {
    System.out.println("Secret key: " + secret);

}
}
pom.xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ezetap</groupId>
<artifactId>jasypt-test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jasypt-test2</name>
<url>http://maven.apache.org</url>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>
<properties>
    <main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jasypt</groupId>
        <artifactId>jasypt</artifactId>
        <version>1.9.2</version>
    </dependency>
</dependencies>

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

我无法找到原因。

请注意,值注释的处理是由
BeanPostProcessor
执行的,而静态内容属于类而不是类的实例。只有在SpringContainer创建了实例之后,才能获得该值。您可以将该逻辑移动到单独的类中,并使用主应用程序上下文创建该类的bean。

谢谢。。我错过了一件小事
command: java -jar target/jasypt-test2-0.0.1-SNAPSHOT.jar 

2016-08-09 12:01:12.299  INFO 7350 --- [           main]         com.ezetap.test.Application              : Started Application in 0.825 seconds (JVM running for 1.15)
Secret key: null
2016-08-09 12:01:12.300  INFO 7350 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@339a956c: startup date [Tue Aug 09 12:01:11 IST 2016]; root of context hierarchy