Reputation: 163
I need to create sth like Shared class, which I can use in the following way:
Shared.getProperty(key);
I tried to use Environment object, but it is always null. Where should it be specified and how? I use .xml for my bean configuration. I also have application.properties, where I want to retrieve data from.
Upvotes: 0
Views: 180
Reputation: 331
The way I do it is using defining the values in application.properties
and then creating a Configuration class for example:
Define constants in application.properties
app.email_subject =My app Registration
app.email_from =Some person
An annotated class
@Configuration
@ConfigurationProperties(prefix = "app")
public class GlobalProperties {
@Value("email_subject")
private String emailSubject;
@Value("email_from")
private String emailFrom;
// getters and setters
}
You can use this class anywhere you want like this:
@Service
public class SomeService {
@Autowired
private GlobalProperties globalProperties;
public someMethod() {
System.out.println(globalProperties.getEmailFrom());
}
}
Upvotes: 0
Reputation: 2516
// Shared.java
@Component
@ConfigurationProperties("prefix.for.application.properties")
public class Shared {
private String str;
// getters, setters
}
// application.properties
prefix.for.application.properties.str=STR
// other code
@Autovired
private Shared shared;
shared.getStr();
Upvotes: 1
Reputation: 952
The best way is to have properties defined in application.properties file and then you can access these properties using @Value annotation.
Upvotes: 0