0

I am using spring boot 1.5.7.RELEASE and trying to inject the value from an application.yml file into a class but the value is always coming as null. The value does get loaded in my class though.

I have a application class as below in my base package

package com.mypackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        VideoTranscoder vt = new VideoTranscoder();
        vt.createJob();
    }

}

My class,

package com.mypackage;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.elastictranscoder.AmazonElasticTranscoder;
import com.amazonaws.services.elastictranscoder.AmazonElasticTranscoderClientBuilder;
import com.amazonaws.services.elastictranscoder.model.CreateJobOutput;
import com.amazonaws.services.elastictranscoder.model.CreateJobRequest;
import com.amazonaws.services.elastictranscoder.model.JobInput;

//@Component
public class VideoTranscoder {

    private static final String PIPELINE_ID = "xxxxxxxxx-xxxx";

    private static final String INPUT_KEY = "video.avi";

    private static final String OUTPUT_KEY = "transcoded_video.mp4";

    private static final String PRESET_ID = "1351620000001-000061";

//    @Value("${s3.accessKey}")
//    private String accessKey;

    @Value("Hello")
    private String accessKey;

    @Value("${s3.secretKey}")
    private String secretKey;

    public void createJob() {
        System.out.println("SECRETKEY: " + secretKey);
        System.out.println("ACCESSKEY: " + accessKey);
        BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);

        AmazonElasticTranscoder amazonElasticTranscoder = AmazonElasticTranscoderClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(creds)).withRegion(Regions.US_EAST_1).build();

        JobInput input = new JobInput().withKey(INPUT_KEY);
        CreateJobOutput output = new CreateJobOutput().withKey(OUTPUT_KEY).withPresetId(PRESET_ID);
        CreateJobRequest createJobRequest = new CreateJobRequest().withPipelineId(PIPELINE_ID).withInput(input)
                .withOutputs(output);
        amazonElasticTranscoder.createJob(createJobRequest);

        System.out.println("DONE!");

    }
}

My application.yml,

s3:
  accessKey: XXXXXXXXXXXXXXXXXXXX
  secretKey: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I am getting the following error,

SECRETKEY: null
ACCESSKEY: null
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.IllegalArgumentException: Access key cannot be null.
    at com.amazonaws.auth.BasicAWSCredentials.<init>(BasicAWSCredentials.java:37)
    at com.mypackage.VideoTranscoder.createJob(VideoTranscoder.java:38)
    at com.mypackage.Application.main(Application.java:12)
    ... 8 more

Why it always returns NULL??

Kindly provide your inputs.

2
  • 2
    You commented out the @Component annotation. Commented Sep 15, 2017 at 7:43
  • Still getting the same after commented out the @Component Commented Sep 15, 2017 at 7:51

1 Answer 1

2

You have to inject via Spring your VideoTranscoder in order for the @Value annotation to work.

Currently you are creating a new instance of the VideoTranscoder by

VideoTranscoder vt = new VideoTranscoder();

And you have commented out the @Component annotation in your VideoTranscoder

So bring back the @Component in VideoTranscoder:

@Component
public class VideoTranscoder {

UPDATE

and get your transcoder from Spring context:

@SpringBootApplication
public class Application {


    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
        VideoTranscoder vt = (VideoTranscoder) ctx.getBean("videoTranscoder");
        vt.createJob();
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

No If I define outside of main, it will ask for "static" and it returns "vt" is null
Yeap you are right, sorry, I have updated my answer. No need to @Autowired, just get the bean from the application context

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.