2017-09-06 184 views
1

我只有兩個類目前在我的春季啓動應用程序。首先是主要的Boot應用程序類另一個是服務類。我宣佈在應用類屬性的源文件如下:無法訪問屬性在春季啓動應用程序

@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(); 
    } 

} 

其他類FTPService其中屬性值在使用@Value作爲下部注入:

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容器管理的對象。

@Service註釋FTPService類,然後將該bean注入某個您想要執行ftp()方法的位置。在這種情況下,main方法將不起作用。

0
  1. 標註FTPService@Component
  2. 更改主類是

@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); 
    } 

} 
相關問題