2016-08-09 6 views
2

私はapplication.propertiesファイルからプロパティの値を出力する必要がある単純なスプリングブートアプリケーションを1つ持っています。Application.propertiesが取得されない値は静的です

Application.propertiesファイルはここに私は私の問題を簡素化し、私は自分のアプリケーションで使用しているいくつかのサンプルコードを与えているのsrc /メイン/リソース

内部に格納されています。

ここに私は値を取得しようとしているコードです。

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

出力:

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.spring[email protected]339a956c: startup date [Tue Aug 09 12:01:11 IST 2016]; root of context hierarchy 

私はこの理由を見つけることができないのです。

答えて

2

バリューアノテーションの処理はBeanPostProcessorで実行されますが、静的なものはクラスに属し、クラスのインスタンスではありません。 Springコンテナによってインスタンスが作成された後にのみ、その値を取得します。そのロジックを別のクラスに移動し、メインのアプリケーションコンテキストでそのクラスのBeanを作成することができます。

+0

ありがとうございました – Bhavesh

関連する問題