Tags

links:
http://stackoverflow.com/questions/7253694/spring-how-to-inject-a-value-to-static-field
http://planproof-fool.blogspot.com/2010/03/spring-setting-static-fields.html
http://www.connorgarvey.com/blog/?p=105
http://stackoverflow.com/questions/2306078/spring-constructor-injection-of-primitive-values-properties-with-annotation-b
http://stackoverflow.com/questions/11991194/can-i-set-null-as-the-default-value-for-a-value-in-spring

summary:
– can’t inject to static fields, workaround using @Autowired(required=true) on a method
– can’t inject into fields referenced in constructor, unless also do @Value on your constructor-arg, or instead initialize values in init-method or @PostConstruct

@Component 
public class MyBean { 
    @Autowired
    public MyBean(@Value("${prop1}") String arg1, @Value("${prop2}") String arg2) { 
        // ... 
    } 
}

– # for SpEL supporting ?: elvis
– $ for properties which using : as default, no elvis
– @Value(“#{ systemProperties[‘username’] }”), this is SpEL
– @Value(“${prop1}”), for properties
– @Value can’t set null value, unless define thru PropertyPlaceholder

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- config the location(s) of the properties file(s) here -->
    <property name="nullValue" value="@null" />
</bean>

then, @Value(“${stuff.value:@null}”), for setting null when stuff.value not exists

Advertisements