我正在使用spring boot 1.5.7.RELEASE并尝试将application.yml文件中的值注入类中,但值始终为null。该值确实会在我的课程中加载。
我的基础包中有一个如下所示的应用程序类
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();
}
}
我的班级,
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!");
}
}
我的application.yml,
s3:
accessKey: XXXXXXXXXXXXXXXXXXXX
secretKey: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
我收到以下错误,
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
为什么它总是返回NULL ??
请提供您的意见。
答案 0 :(得分:2)
您必须通过Spring
VideoTranscoder
注入@Value
注释才能生效。
目前,您正在通过
创建VideoTranscoder
的新实例
VideoTranscoder vt = new VideoTranscoder();
您已在@Component
VideoTranscoder
注释
所以请回到@Component
中的VideoTranscoder
:
@Component
public class VideoTranscoder {
<强>更新强>
从Spring上下文中获取转码器:
@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();
}
}