Jeets
Jeets

Reputation: 3377

dynamic property names with spring

I am using spring 4 PropertyPlaceHolder :

<bean id="propertyConfigurer"
    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/database.properties</value>
            <value>/WEB-INF/app.properties</value>    
            <value>/WEB-INF/cdservice.properties</value>            
        </list>
    </property> 

</bean>

From the properties File I want to read properties in my application which I am reading thus:

@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Value("${supporting.documents.location}")
    private String supportingDocumentsLocation;

    @Override
    public String removeFile(String xyz) {
       //Here I want to read property xyz which is dynamic
    }

}

As in above code I am able to read static properties using @Value annotation. But how can I read a property like xyz which is dynamic.Please suggest?

Upvotes: 2

Views: 3337

Answers (1)

Raniz
Raniz

Reputation: 11123

Inject the Spring Environment into your bean and you can read arbitrary properties:

@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    private Environment environment;

    @Value("${supporting.documents.location}")
    private String supportingDocumentsLocation;

    @Override
    public String removeFile(String xyz) {
        String value = environment.getProperty(xyz);
    }

}

Upvotes: 1

Related Questions