Rahul Khimasia
Rahul Khimasia

Reputation: 503

Defining string constants in configuration metadata

I often have a need to define string constants in the XML configuration metadata file. These constants are things like Company Name, Fiscal Year, etc; that I need to lookup from various classes of my application. I end up coding them as bean definitions like <bean id="CompanyName" class="java.lang.String" c:_0="Google" />. Is there a better way to define this information?

Upvotes: 0

Views: 3273

Answers (1)

reos
reos

Reputation: 8334

You can have your properties inside a property file for example.

config.properties

property1=value
property2=value

Then in your class you can use the propety file

@Configuration
@PropertySource("classpath:config.properties")
public class MyClass {

    @Value("${property1}")
    private String myProperty1;

    @Value("${property2}")
    private String myProperty2;
}

You can see these two tutorials

http://www.mkyong.com/spring/spring-propertysources-example/

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Upvotes: 1

Related Questions