nanosoft
nanosoft

Reputation: 3091

Not able to access property in spring boot application

I have just two classes currently in my spring boot application. First is main main Boot Application class another is a service class. I have declared property source file in Application class as below:

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

}

Other class is FTPService in which property value in injected using @Value as below:

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

It prints null. Tried annotating FTPService with below annotations as well but didn't worked. Tried copying properties to application.properties but didn't worked.

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

My properties file in under src/main/resources. Its name is ftp.properties and content is below:

vendor1.server = server.com

Mine is gradle app with below config:

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
}

Am I missing any annotation or right way to specify property file?

Upvotes: 1

Views: 2420

Answers (2)

Strelok
Strelok

Reputation: 51441

  1. Annotate FTPService with @Component
  2. Change your main class to be

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

}

Upvotes: 0

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

The property injection doesn't work in your sample because you create the object manually with the new keyword. Property injection works only in objects managed by the Spring container.

Annotate the FTPService class with @Service and then inject that bean to some place where you would like to execute the ftp() method. The main method won't work in that case.

Upvotes: 2

Related Questions