In our project we are running some non-grails spri...
# questions
u
In our project we are running some non-grails spring related code in the
/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?
j
The recommendation is to use org.springframework.beans.factory.annotation.Value to access values from application.yml your code in src would look like this:
Copy code
import 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
Copy code
beans = {
    configHolder(ConfigHolder)
}
partygrails 1
j
You can also use component scanning to avoid having to explicitly define the bean
partygrails 1
g
@jdaugherty in Grails 7 or 8 can we think of a way to avoid the use of the resources.groovy file? I always wondered if Grails is a Spring app why do we need to explicitly configure bean that way instead of simply having them discoverable like a “normal” spring app?
j
you can use the built in spring annotations to configure instead if resources
g
Mmmm last time I’ve tried I didn’t work.. I’ll try again
j
@giangio That had been my experience also. Here is the part we were missing https://grok.com/share/bGVnYWN5_c76d64b2-b306-44a0-98df-d64a4c045914
👍 1
g
@James Fredley Interesting, I wonder if there is an efficient solution to set a default. I think that having Spring under the hood makes people think that one can just annotate classes under /src/ to make them available for injection. I would love Grails to work that way, without having to configure anything. Has anybody discussed it before?
j
We could definitely make start.grails.org split this config out, since you specify the Base Package
👍 1
g
Another case we have is with spring libraries and as of today we have manually configured each bean in resources.groovy. Being Spring Beans it would be great to have them automatically scanned cause they are in a library. Even though at the moment I don’t remember how Spring itself works in such cases, if the scanning is automatic or not for libraries.
j
The reason it likely doesn't work is if you're dependent on grails beans that aren't wired in using the same mechanism
Scott made a lot of progress here - and eventually all of the grails stuff will be defined in @Configuration
g
I dream of a Grails version where reading Spring docs will do it for Grails as well :)
I mean it would be great to have Spring conventions work in Grails out of the box with no additional configuration