简体   繁体   中英

Spring - Set property value using annotations without using properties file

I have a bean class like, for example

class Sample {
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

and I want to set this property's value.

In Xml Configuration, I could do

<bean id = "sample" class = "Sample"
    <property name = "message" value = "Hello there!"/>
</bean>

How do I achieve the same thing ie set property's value using Java Annotation? Now I have read that we can use @Value annotation using some properties file but cannot it be done without using properties file, doing the way I did it through xml file? Or using properties file is necessary ?

I was able to do it by including @Value("Hello there!") above the setter method. But I could feel that is not a good idea. How to set the property values for different instances using Java Annotations?

Thanks.

The value inserted into the @Value can come from places other than a properties file, for example it can also use system properties.

Using the guide here as a starting point should help you understand a little better.

As a basic and mostly useless usage example we can only inject “string value” from the annotation to the field:

 @Value("string value") private String stringValue;

Using the @PropertySource annotation allows us to work with values from properties files with the @Value annotation. In the following example we get “Value got from the file” assigned to the field:

 @Value("${value.from.file}") private String valueFromFile;

We can also set the value from system properties with the same syntax. Let's assume that we have defined a system property named systemValue and look at the following sample:

 @Value("${systemValue}") private String systemValue;

Default values can be provided for properties that might not be defined. In this example the value “some default” will be injected:

 @Value("${unknown.param:some default}") private String someDefault;

You have a few options, depending on your requirements. In both of these examples you can set the annotation on a setter instead of the field.

Custom PropertySource

This lets you continue using @Value with greater control for how the properties are supplied. There are a large number of PropertySource implementations, but you can always create your own.

References:

Example:

@Configuration
class MyConfiguration {
  @Bean
  PropertySource myPropertySource(ConfigurableEnvironment env) {
    MapPropertySource source = new MapPropertySource("myPropertySource", singletonMap("myPropertyValue", "example"));
    env.getPropertySources().addFirst(source);
    return source;
  }
}

class Sample {
  @Value("${myPropertyValue}")
  private String message;

  public String getMessage() {
    return message;
  }
}

String Bean

Define a bean as a String and auto-wire it using its qualifier.

Example:

@Configuration
class MyConfiguration {
  @Bean
  String myPropertyValue() {
    String value;
    // do something to get the value
    return value;
  }
}

class Sample {
  @Autowired
  @Qualifier("myPropertyValue")
  private String message;

  public String getMessage() {
    return message;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM