user
07/15/2025, 5:56 PM/src/
directory under the project and we have dependency to retrieve the configuration from application.yml
. To achieve that we are using GrailsApplication grailsApplication = Holders.grailsApplication
but found it here https://github.com/apache/grails-core/issues/12090#issuecomment-934592118 that using holders to load properties is not a good approach. So may I know whats the best and right approach?
If holders is the right way to go, I am unable to stub it for unit-testing, can you please educate me how may I stub the Holders+application data?James Fredley
07/15/2025, 6:12 PMimport org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
@Component
class ConfigHolder {
private static String HIBERNATE_JDBC_DATE_FORMAT
private static String HIBERNATE_JDBC_TIME_ZONE
@Value('${hibernate.jdbc.date_format}')
void setHibernateDateFormat(String input) {
ConfigHolder.HIBERNATE_JDBC_DATE_FORMAT = input
}
static String getHibernateDateFormat() {
HIBERNATE_JDBC_DATE_FORMAT
}
@Value('${hibernate.jdbc.time_zone}')
void setHibernateTimeZone(String input) {
ConfigHolder.HIBERNATE_JDBC_TIME_ZONE = input
}
static TimeZone getHibernateTimeZone() {
TimeZone.getTimeZone(ConfigHolder.HIBERNATE_JDBC_TIME_ZONE)
}
}
You also need to add this to grails-app/conf/spring/resources.groovy so the values are injected by Spring
beans = {
configHolder(ConfigHolder)
}
jdaugherty
07/15/2025, 7:38 PMgiangio
07/16/2025, 6:49 AMjdaugherty
07/16/2025, 11:42 AMgiangio
07/16/2025, 11:44 AMJames Fredley
07/16/2025, 1:27 PMgiangio
07/16/2025, 1:35 PMJames Fredley
07/16/2025, 1:44 PMgiangio
07/16/2025, 1:51 PMjdaugherty
07/16/2025, 2:29 PMjdaugherty
07/16/2025, 2:29 PMgiangio
07/16/2025, 3:19 PMgiangio
07/16/2025, 3:20 PM