2017-09-06 6 views
1

現在、私のスプリングブートアプリケーションには2つのクラスしかありません。最初はメインのメインブートアプリケーションクラスで、もう1つはサービスクラスです。スプリングブートアプリケーションのプロパティにアクセスできない

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

    public static void main(String[] args) throws JSchException, SftpException, IOException 
    { 
     SpringApplication.run(Application.class, args); 
     FTPService ftpservice = new FTPService(); 
     ftpservice.ftp(); 
    } 

} 

他のクラスに注入以下のように@valueを使用しているプロパティの値にFTPServiceです:

public class FTPService 
{ 
    @Value("${vendor1.server}") 
    private String VENDOR1_SERVER; 

    public boolean ftp() throws JSchException, SftpException, IOException 
    { 
     boolean success = false; 
     System.out.println("vendor1.server : " + VENDOR1_SERVER); 
     return success; 
    } 
} 

それはヌル印刷し、私は以下のように、アプリケーションクラスのプロパティのソースファイルを宣言しました。以下の注釈でFTPServiceにアノテーションを付けようとしましたが、うまくいきませんでした。 プロパティをapplication.propertiesにコピーしようとしましたが、機能しませんでした。

@Configuration 
@PropertySource("classpath:ftp.properties") 

src/main/resourcesの下にある自分のプロパティファイル。その名前はftp.propertiesで、内容は以下の通りです:

vendor1.server = server.com 

鉱山は、コンフィグ以下とGradleのアプリです:

plugins { 
id 'org.springframework.boot' version '1.5.6.RELEASE' 
id 'java' 
} 

group = groupName 

sourceCompatibility = javaVersion 
targetCompatibility = javaVersion 

compileJava.options.encoding = 'UTF-8' 

repositories { 
    mavenCentral() 
} 

configurations.all { 
    exclude group: 'commons-logging', module: 'commons-logging' 
    exclude group: 'log4j', module: 'log4j' 
    exclude group: 'org.slf4j', module: 'slf4j-jdk14' 
    exclude group: 'org.slf4j', module: 'slf4j-log4j12' 
} 

configurations { 
    all*.exclude module : 'spring-boot-starter-logging' 
} 

dependencies { 

    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: bootVersion 
    compile group: 'com.jcraft', name: 'jsch', version: jschVersion 

    compile group: 'javax.servlet', name: 'javax.servlet-api', version: servletVersion 
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: jacksonVersion 
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion 
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion 
    compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: log4jVersion 
    compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion 
    compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: slf4jVersion 
    compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: slf4jVersion 
    compile group: 'org.slf4j', name: 'jul-to-slf4j', version: slf4jVersion 

    testCompile group: 'junit', name: 'junit', version: junitVersion 
} 

私は、プロパティファイルを指定するには、任意の注釈や正しい方法足りませんか?

答えて

1

newキーワードを使用してオブジェクトを手動で作成するため、プロパティの挿入がサンプルで機能しません。プロパティの挿入は、Springコンテナによって管理されるオブジェクトでのみ機能します。

でFTPServiceクラスに注釈を付けてから、ftp()メソッドを実行する場所にそのBeanを挿入します。その場合、mainメソッドは機能しません。 @Component

  • 変更あなたのメインクラスと

  • 0
    1. 注釈FTPServiceがあることを

    @SpringBootApplication 
    @PropertySource("classpath:ftp.properties") 
    public class Application implements CommandLineRunner 
    { 
    
        @Autowired 
        FTPService ftpService; 
    
        @Override 
        public void run(String... strings) throws Exception { 
         ftpService.ftp(); 
        } 
    
        public static void main(String[] args) 
        { 
         SpringApplication.run(Application.class, args); 
        } 
    
    } 
    
    関連する問題