java 从 Spring @Configuration 类自动装配字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25411102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:59:48  来源:igfitidea点击:

Autowire a string from Spring @Configuration class?

javaspring

提问by css

I'm looking to centralize access to all of my property values so that I can do things like make sure everything uses the same name for properties, the same default values, etc. I've created a class to centralize all of this, but I'm not sure how the classes that need access to these values should get them, since you can't autowire Strings.

我希望集中访问我所有的属性值,以便我可以做一些事情,例如确保所有属性都使用相同的名称、相同的默认值等。我创建了一个类来集中所有这些,但是我不确定需要访问这些值的类应该如何获取它们,因为您不能自动装配字符串。

My class is something like this:

我的课是这样的:

@Configuration
public class SpringConfig {
    @Autowired
    @Value("${identifier:asdf1234}")
    public String identifier;
}

Where I might use it in multiple classes

我可以在多个课程中使用它的地方

public class Foo {
    @Autowired 
    private String theIdentifier;
}

public class Bar {
    @Autowired 
    private String anIdentifier;
}

采纳答案by Christophe L

Another solution would be to create a Spring bean with all your configuration and autowire it:

另一种解决方案是使用您的所有配置创建一个 Spring bean 并自动装配它:

@Component
public class MyConfig {
    @Value("${identifier:asdf1234}")
    public String identifier;

    public String getIdentifier() {
        return identifier;
    }
}

And in your other classes:

在您的其他课程中:

public class Foo {
    @Autowired
    private MyConfig myConfig;

    public void someMethod() {
        doSomething(myConfig.getIdentifier());
    }
}

回答by Ricardo Veguilla

Initialized a String bean based on the property:

根据属性初始化一个String bean:

@Configuration
public class SpringConfig {

    @Bean
    public String identifier(@Value("${identifier:asdf1234}") identifier) {
       return identifier;
    }
}

Then inject it by bean name using Spring's SpELbeanName expression "#{beanName}in a @Valueannotation

然后在注解中使用Spring 的 SpELbeanName 表达式通过 bean 名称注入它"#{beanName}@Value

@Component
public class Foo {
    @Value("#{identifier}")
    private String oneIdentifier;
}

public class Bar {
   @Value("#{identifier}")
   private String sameIdentifier;
}

回答by Sotirios Delimanolis

There's no clean solution to this, in my opinion. The simplest I can think of is to use a staticconstant Stringfield that holds the property placeholder.

在我看来,没有干净的解决方案。我能想到的最简单的方法是使用一个static常量String字段来保存属性占位符。

public final class Constants {
    private Constants() {}
    public static final String PROPERTY_NAME= "${identifier.12345}";
}

and have all @Valueannotations use it

并让所有@Value注释都使用它

public class Foo {
    @Value(Constants.PROPERTY_NAME)
    private String theIdentifier;
}

public class Bar {
    @Value(Constants.PROPERTY_NAME)
    private String anIdentifier;
}

Now, if you ever need to change the property name, you only need to change it in Constants. But make sure to recompile everything.

现在,如果您需要更改属性名称,只需在Constants. 但请确保重新编译所有内容。