@Value注解始终为NULL。

huangapple go评论88阅读模式
英文:

@Value annotation is always NULL

问题

这是我 application.yml 文件中的内容:

aws:
  routeconfig: 
    rolearn: myrolearn
    bucketname: mybucket
    region: ap-southeast-1

以下是带有 @Value 注解的变量,它们总是保持为 NULL

@Value("${aws.routeconfig.rolearn}")
String routeConfigRoleArn;

@Value("${aws.routeconfig.bucketname}")
String bucketName;

@Value("${aws.routeconfig.region}")
String awsRegion;
英文:

I am trying to get some values from my application.yml file using the @Value annotation in a ServiceImpl class, but variables with @Value annotation always stay NULL for some reason. Can someone please help me in fetching the values from the application.yml file using @Value annotation?

This is what is present in my application.yml file

aws:
  routeconfig: 
    rolearn: myrolearn
    bucketname: mybucket
    region: ap-southeast-1

These are the variables with @Value annotation which are always NULL

@Value("${aws.routeconfig.rolearn}")
String routeConfigRoleArn;

@Value("${aws.routeconfig.bucketname}")
String bucketName;

@Value("${aws.routeconfig.region}")
String awsRegion;

答案1

得分: 1

我认为你的 yml 文件是不正确的。

aws:
    routeconfig: rolearn
        bucketname: mybucket
        region: ap-southeast-1

你正在将 bucketnameregion 引用放在 routeconfig 属性的父对象下。

如果你想引用 aws 下的所有值,应该像这样:

aws:
    routeconfig: rolearn
    bucketname: mybucket
    region: ap-southeast-1

然后你的 @Value 注解应该可以工作。

你也可以像这样配置一个属性类并注入它:

@Component
@ConfigurationProperties(prefix = "aws")
public class AwsConfig {

    private String routeConfig;
    private String bucketName;
    private String region;

    // getters and setters
}

然后,你可以注入 AwsConfig 类并检索相应的属性,而不是使用 @Value

@Autowired
AwsConfig awsConfig;

awsConfig.getRouteConfig();
awsConfig.getBucketName();
awsConfig.getRegion();

在需要的地方使用。

英文:

I think your yml is incorrect.

aws:
    routeconfig: rolearn
        bucketname: mybucket
        region: ap-southeast-1

You are referencing bucketname and region under a parent object of routeconfig property.

If you want to reference all the values under aws, it should be like so:

aws:
    routeconfig: rolearn
        bucketname: mybucket
        region: ap-southeast-1

Then your @Value annotation should work.

You can also configure a property class like so and inject it:

@Component
@ConfigurationProperties(prefix = "aws")
public class AwsConfig {

    private String routeConfig;
    private String bucketName;
    private String region;

    // getters and setter
}

Then instead of using @Value you can inject the AwsConfig class and retrieve the corresponding properties:

AwsConfig awsConfig;
awsConfig.getRouteConfig();
awsConfig.getBucketName();
awsConfig.getRegion()

wherever needed.

答案2

得分: 0

你能尝试按照以下方式格式化你的 YAML 吗:

aws:
  routeconfig:
    rolearn: myrole
    bucketname: mybucket
    region: ap-southeast-1

同时,请确保缩进只使用两个空格,不要使用制表符。

英文:

Can you try formatting your yaml like this:

aws:
  routeconfig: 
    rolearn: myrole
    bucketname: mybucket
    region: ap-southeast-1

Also, make sure you have only two spaces for indention and no tabs.

答案3

得分: 0

你检查是否做错了什么。

application.yml 中没有问题。

从 application.yml 文件中加载属性。对我有效:

@Controller
public class Sample {
    
    @Value("${aws.routeconfig.rolearn}")
    String routeConfigRoleArn;

    @Value("${aws.routeconfig.bucketname}")
    String bucketName;

    @Value("${aws.routeconfig.region}")
    String awsRegion;
    
    @GetMapping("/test")
    public void load() {
        System.out.println(routeConfigRoleArn);
        System.out.println(bucketName);
        System.out.println(awsRegion);
        
    }

}

控制台输出:

myrolearn
mybucket
ap-southeast-1

如果你仍然无法解决,可以尝试下面的解决方案:

你可以这样加载属性。

创建一个自定义类来加载自定义属性。例如:

@Component
@ConfigurationProperties(prefix = "aws")
public class AWSProps {

    private Map<String, String> routeconfig;

    public Map<String, String> getRouteconfig() {
        return routeconfig;
    }

    public void setRouteconfig(Map<String, String> routeconfig) {
        this.routeconfig = routeconfig;
    }

    @Override
    public String toString() {
        return "AWSProps [routeconfig=" + routeconfig + "]";
    }

}

pom.xml 中添加这个依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

现在进行测试,只需创建示例控制器:

@Controller
public class Sample {
    
    @Autowired
    private AWSProps props;
    
    @GetMapping("/test")
    public void load() {
        System.out.println(props);
    }

}

输出:AWSProps [routeconfig={rolearn=myrolearn, bucketname=mybucket, region=ap-southeast-1}]

英文:

You check if you are doing something wrong.

There is no issue in application.yml.

aws:
  routeconfig: 
    rolearn: myrolearn
    bucketname: mybucket
    region: ap-southeast-1

Properties are loaded from application.yml file. It's working for me :

@Controller
public class Sample {
	
	@Value(&quot;${aws.routeconfig.rolearn}&quot;)
	String routeConfigRoleArn;

	@Value(&quot;${aws.routeconfig.bucketname}&quot;)
	String bucketName;

	@Value(&quot;${aws.routeconfig.region}&quot;)
	String awsRegion;
	
	@GetMapping(&quot;/test&quot;)
	public void load() {
		System.out.println(routeConfigRoleArn);
		System.out.println(bucketName);
		System.out.println(awsRegion);
		
	}

}

Output on console :

myrolearn
mybucket
ap-southeast-1

If you are still not able to solve, then you can go for this solution below :

You can do in this way to load properties.

Create a custom class so as to load custom properties. For example:

@Component
@ConfigurationProperties(prefix = &quot;aws&quot;)
public class AWSProps {

	private Map&lt;String, String&gt; routeconfig;

	public Map&lt;String, String&gt; getRouteconfig() {
		return routeconfig;
	}

	public void setRouteconfig(Map&lt;String, String&gt; routeconfig) {
		this.routeconfig = routeconfig;
	}

	@Override
	public String toString() {
		return &quot;AWSProps [routeconfig=&quot; + routeconfig + &quot;]&quot;;
	}

}

In the pom.xml, add this dependency :

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-configuration-processor&lt;/artifactId&gt;
	&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;

Now to test, just create sample controller :

@Controller
public class Sample {
	
	@Autowired
	private AWSProps props;
	
	@GetMapping(&quot;/test&quot;)
	public void load() {
		System.out.println(props);
	}

}

Output : AWSProps [routeconfig={rolearn=myrolearn, bucketname=mybucket, region=ap-southeast-1}]

huangapple
  • 本文由 发表于 2020年10月3日 00:19:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64174991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定