【问题标题】:Not able to access property in spring boot application无法访问 Spring Boot 应用程序中的属性
【发布时间】:2017-09-06 11:13:44
【问题描述】:

我的 Spring Boot 应用程序中目前只有两个类。首先是主要的主要引导应用程序类,另一个是服务类。我在 Application 类中声明了属性源文件,如下所示:

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

它打印 null。 也尝试使用以下注释对 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
}

我是否缺少任何注释或指定属性文件的正确方法?

【问题讨论】:

    标签: java spring-mvc spring-boot properties


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      1. @Component注释FTPService
      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);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2021-12-03
        • 2020-06-30
        • 1970-01-01
        • 1970-01-01
        • 2017-06-26
        • 2020-09-07
        • 1970-01-01
        • 2019-02-21
        • 2019-03-13
        相关资源
        最近更新 更多