spring-environment

how to use Spring-EL in @Value when using constants to resolve a property

对着背影说爱祢 提交于 2020-05-14 18:39:49
问题 I'm trying to use a constant to define a property and then resolving it with the @Value annotation. I defined the constant in an interface: public interface InternalConstant{ public static final String JOB_NAME_PROPERTY = "javabatch.jobName"; } I'm using springboot and I'm adding the property as a default property to the context: SpringApplication springApp = new SpringApplication(configs.toArray()); Properties props = new Properties(); props.setProperty(InternalConstants.JOB_NAME_PROPERTY,

Expose <property-placeholder> properties to the Spring Environment

牧云@^-^@ 提交于 2019-12-03 13:51:16
问题 I have a properties file that I'm registering with Spring through XML, using the property-placeholder element: <context:property-placeholder location="classpath:foo.properties" /> I can access properties using @Value annotations, e.g. @Value("${prefs.key}") private String prefValue; but I also need to access properties via the Spring Environment, e.g. @Autowired private Environment env; public String getValue(String key) { return env.getProperty(key); } getValue() here always returns null ,

Define Spring @PropertySource in xml and use it in Environment

早过忘川 提交于 2019-12-03 08:48:14
问题 In spring JavaConfig, I can define property source and inject into Environment @PropertySource("classpath:application.properties") @Inject private Environment environment; How do I do that if in xml? I am using context:property-placeholder, and on the JavaConfig class @ImportResource to import the xml. But I cannot retrieve property defined in the properties file using environment.getProperty("xx") <context:property-placeholder location="classpath:application.properties" /> 回答1: AFAIK, there

Define Spring @PropertySource in xml and use it in Environment

…衆ロ難τιáo~ 提交于 2019-12-02 22:46:39
In spring JavaConfig, I can define property source and inject into Environment @PropertySource("classpath:application.properties") @Inject private Environment environment; How do I do that if in xml? I am using context:property-placeholder, and on the JavaConfig class @ImportResource to import the xml. But I cannot retrieve property defined in the properties file using environment.getProperty("xx") <context:property-placeholder location="classpath:application.properties" /> AFAIK, there is no way of doing this by pure XML. Anyways, here is a little code I did this morning: First, the test:

Inject spring bean dynamically

别等时光非礼了梦想. 提交于 2019-11-28 16:53:25
In a java-spring web-app I would like to be able to dynamically inject beans. For example I have an interface with 2 different implementations: In my app I'm using some properties file to configure injections: #Determines the interface type the app uses. Possible values: implA, implB myinterface.type=implA My injections actually loaded conditionally relaying on the properties values in the properties file. For example in this case myinterface.type=implA wherever I inject MyInterface the implementation that will be injected will be ImplA (I accomplished that by extending the Conditional

Inject spring bean dynamically

谁说胖子不能爱 提交于 2019-11-27 20:00:35
问题 In a java-spring web-app I would like to be able to dynamically inject beans. For example I have an interface with 2 different implementations: In my app I'm using some properties file to configure injections: #Determines the interface type the app uses. Possible values: implA, implB myinterface.type=implA My injections actually loaded conditionally relaying on the properties values in the properties file. For example in this case myinterface.type=implA wherever I inject MyInterface the