无法访问属性在春季启动应用程序

问题描述:

我只有两个类目前在我的春季启动应用程序。首先是主要的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 
} 

我失去了任何注释或指定属性文件的路吗?

属性注入不适用于您的示例,因为您使用new关键字手动创建对象。属性注入只适用于由Spring容器管理的对象。

@Service注释FTPService类,然后将该bean注入某个您想要执行ftp()方法的位置。在这种情况下,main方法将不起作用。

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

}