Using Guice to inject objects into a custom cache

11 views
Skip to first unread message

Justin Field

unread,
Sep 24, 2019, 4:16:28 PM9/24/19
to mybatis-user

Poitras Christian

unread,
Sep 24, 2019, 4:41:22 PM9/24/19
to mybati...@googlegroups.com
Hi,

mybatis-guice does not inject dependencies in cache classes.

Is this functionality available in mybatis-spring?

Best,
Christian

From: <mybati...@googlegroups.com> on behalf of Justin Field <fie...@gmail.com>
Reply-To: "mybati...@googlegroups.com" <mybati...@googlegroups.com>
Date: Tuesday, September 24, 2019 at 4:16 PM
To: mybatis-user <mybati...@googlegroups.com>
Subject: Using Guice to inject objects into a custom cache

I am trying to make a custom cache for my app, here is the WIP:
https://github.com/Nike-Inc/cerberus-management-service/blob/dac5f9d4c837956ca2d48f10732244815e7ebee2/src/main/java/com/nike/cerberus/cache/DatabaseCache.java

The App and MyBatis is configured with Guice:
https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java<https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java#L35>

The cache is wired up via
https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java#L35
https://github.com/Nike-Inc/cerberus-management-service/blob/dac5f9d4c837956ca2d48f10732244815e7ebee2/src/main/resources/com/nike/cerberus/mapper/SecureDataMapper.xml#L23

What I need to do is bring in the application monitoring service so I can instrument the cache stats and send them to our metric aggregator so we can monitor and alert of the stats.

I was wondering if the Guice module had a hook to inject things into something like a custom cache impl?

Cheers,

Justin
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/c76d47fd-9216-4f27-bc7b-7315b5a9cbf8%40googlegroups.com<https://groups.google.com/d/msgid/mybatis-user/c76d47fd-9216-4f27-bc7b-7315b5a9cbf8%40googlegroups.com?utm_medium=email&utm_source=footer>.

Justin Field

unread,
Sep 24, 2019, 5:10:41 PM9/24/19
to mybati...@googlegroups.com
Christian,

I do not know what mybatis-spring does with out digging through that projects code base.

I am not super familiar with how mybaitis creates the Cache classes and how much effort it would be to integrate an injector into that flow.
I assume if it was straightforward it would have already been done, correct me if that is not correct.

Should I attempt to make a PR to allow mybaitis guice to work with Custom Cache classes, or are there good reasons that it doesn't already and/or shouldn't exist?

Alternatively, I can just make static singleton instance with a static getter like I did for the logging configuration:
https://github.com/Nike-Inc/cerberus-management-service/blob/master/src/main/resources/logback.groovy#L207
and use it in the initialize method that mybatis provides.

The latter probably being the easiest, if not a little hacky.

Cheers,

Justin

To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/2041A6AD-282F-4BC8-AF34-2DE6F93BA4D9%40ircm.qc.ca.


--
Justin Field
LinkedIn | @fieldju

Justin Field

unread,
Sep 24, 2019, 5:30:02 PM9/24/19
to mybati...@googlegroups.com
Hey!

OK, I got a working solution, it might be a little hacky but ...

You can use https://github.com/google/guice/wiki/Injections#static-injections to create a statically accessible instance of the injector itself.
Then in the MyBatis initialize lifecycle hook method just call StaticInjector.getInstace().inject(this);
That will then inject all the @Inject props on the CustomCache instance.

Cheers,

Justin

Poitras Christian

unread,
Sep 24, 2019, 6:46:42 PM9/24/19
to mybati...@googlegroups.com
Justin,

If MyBatis registers all Cache instances at configuration time, then it is possible to inject dependencies in a similar way we do injection for the type handlers.
You can make a PR for this if you want. I will have a look in the next few days since it may be easy to fix.

As you said, you can make a static injection helper. Obviously, it is not recommended if an alternative exists.

Best,
Christian

From: <mybati...@googlegroups.com> on behalf of Justin Field <fie...@gmail.com>
Reply-To: "mybati...@googlegroups.com" <mybati...@googlegroups.com>
Date: Tuesday, September 24, 2019 at 5:10 PM
To: "mybati...@googlegroups.com" <mybati...@googlegroups.com>
Subject: Re: Using Guice to inject objects into a custom cache

Christian,

I do not know what mybatis-spring does with out digging through that projects code base.

I am not super familiar with how mybaitis creates the Cache classes and how much effort it would be to integrate an injector into that flow.
I assume if it was straightforward it would have already been done, correct me if that is not correct.

Should I attempt to make a PR to allow mybaitis guice to work with Custom Cache classes, or are there good reasons that it doesn't already and/or shouldn't exist?

Alternatively, I can just make static singleton instance with a static getter like I did for the logging configuration:
https://github.com/Nike-Inc/cerberus-management-service/blob/master/src/main/resources/logback.groovy#L207
and use it in the initialize method that mybatis provides.

The latter probably being the easiest, if not a little hacky.

Cheers,

Justin

On Tue, Sep 24, 2019 at 1:41 PM Poitras Christian <Christia...@ircm.qc.ca<mailto:Christia...@ircm.qc.ca>> wrote:
Hi,

mybatis-guice does not inject dependencies in cache classes.

Is this functionality available in mybatis-spring?

Best,
Christian

From: <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>> on behalf of Justin Field <fie...@gmail.com<mailto:fie...@gmail.com>>
Reply-To: "mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>" <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>
Date: Tuesday, September 24, 2019 at 4:16 PM
To: mybatis-user <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>
Subject: Using Guice to inject objects into a custom cache

I am trying to make a custom cache for my app, here is the WIP:
https://github.com/Nike-Inc/cerberus-management-service/blob/dac5f9d4c837956ca2d48f10732244815e7ebee2/src/main/java/com/nike/cerberus/cache/DatabaseCache.java

The App and MyBatis is configured with Guice:
https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java<https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java#L35>

The cache is wired up via
https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java#L35
https://github.com/Nike-Inc/cerberus-management-service/blob/dac5f9d4c837956ca2d48f10732244815e7ebee2/src/main/resources/com/nike/cerberus/mapper/SecureDataMapper.xml#L23

What I need to do is bring in the application monitoring service so I can instrument the cache stats and send them to our metric aggregator so we can monitor and alert of the stats.

I was wondering if the Guice module had a hook to inject things into something like a custom cache impl?

Cheers,

Justin
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com><mailto:mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com>>.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com>.
fie...@gmail.com<mailto:fie...@gmail.com>
LinkedIn<https://www.linkedin.com/in/fieldju> | @fieldju

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CANn6F%2BtZFTzCS%3DK_4FsxFfSwME8ywQR-miJO%2Bt4xN3qDbT%2BCXg%40mail.gmail.com<https://groups.google.com/d/msgid/mybatis-user/CANn6F%2BtZFTzCS%3DK_4FsxFfSwME8ywQR-miJO%2Bt4xN3qDbT%2BCXg%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Justin Field

unread,
Sep 24, 2019, 11:27:52 PM9/24/19
to mybati...@googlegroups.com
I will have a look in the next few days since it may be easy to fix. 

That sounds nice, I am pretty slammed and don't have to bandwidth to ramp up on a new project / framework right now and can use that work around for now.

Thanks for checking this out.

Cheers,

Justin 

To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/98901847-2545-4883-A94B-4A2F278C0B49%40ircm.qc.ca.

Poitras Christian

unread,
Sep 26, 2019, 5:05:11 PM9/26/19
to mybati...@googlegroups.com
Hi Justin,

Instead of relying on mybatis-guice to inject your custom cache, you can implement a custom ConfigurationSetting.

In the configuration setting, just inject the Injector and call:
configuration.getCaches().forEach(cache->injector.injectMembers(cache));

Best,
Christian

From: <mybati...@googlegroups.com> on behalf of Justin Field <fie...@gmail.com>
Reply-To: "mybati...@googlegroups.com" <mybati...@googlegroups.com>
Date: Tuesday, September 24, 2019 at 11:27 PM
To: "mybati...@googlegroups.com" <mybati...@googlegroups.com>
Subject: Re: Using Guice to inject objects into a custom cache

I will have a look in the next few days since it may be easy to fix.

That sounds nice, I am pretty slammed and don't have to bandwidth to ramp up on a new project / framework right now and can use that work around for now.

Thanks for checking this out.

Cheers,

Justin

On Tue, Sep 24, 2019 at 3:46 PM Poitras Christian <Christia...@ircm.qc.ca<mailto:Christia...@ircm.qc.ca>> wrote:
Justin,

If MyBatis registers all Cache instances at configuration time, then it is possible to inject dependencies in a similar way we do injection for the type handlers.
You can make a PR for this if you want. I will have a look in the next few days since it may be easy to fix.

As you said, you can make a static injection helper. Obviously, it is not recommended if an alternative exists.

Best,
Christian

From: <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>> on behalf of Justin Field <fie...@gmail.com<mailto:fie...@gmail.com>>
Reply-To: "mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>" <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>
Date: Tuesday, September 24, 2019 at 5:10 PM
To: "mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>" <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>
Subject: Re: Using Guice to inject objects into a custom cache

Christian,

I do not know what mybatis-spring does with out digging through that projects code base.

I am not super familiar with how mybaitis creates the Cache classes and how much effort it would be to integrate an injector into that flow.
I assume if it was straightforward it would have already been done, correct me if that is not correct.

Should I attempt to make a PR to allow mybaitis guice to work with Custom Cache classes, or are there good reasons that it doesn't already and/or shouldn't exist?

Alternatively, I can just make static singleton instance with a static getter like I did for the logging configuration:
https://github.com/Nike-Inc/cerberus-management-service/blob/master/src/main/resources/logback.groovy#L207
and use it in the initialize method that mybatis provides.

The latter probably being the easiest, if not a little hacky.

Cheers,

Justin

On Tue, Sep 24, 2019 at 1:41 PM Poitras Christian <Christia...@ircm.qc.ca<mailto:Christia...@ircm.qc.ca><mailto:Christia...@ircm.qc.ca<mailto:Christia...@ircm.qc.ca>>> wrote:
Hi,

mybatis-guice does not inject dependencies in cache classes.

Is this functionality available in mybatis-spring?

Best,
Christian

From: <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com><mailto:mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>> on behalf of Justin Field <fie...@gmail.com<mailto:fie...@gmail.com><mailto:fie...@gmail.com<mailto:fie...@gmail.com>>>
Reply-To: "mybati...@googlegroups.com<mailto:mybati...@googlegroups.com><mailto:mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>" <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com><mailto:mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>>
Date: Tuesday, September 24, 2019 at 4:16 PM
To: mybatis-user <mybati...@googlegroups.com<mailto:mybati...@googlegroups.com><mailto:mybati...@googlegroups.com<mailto:mybati...@googlegroups.com>>>
Subject: Using Guice to inject objects into a custom cache

I am trying to make a custom cache for my app, here is the WIP:
https://github.com/Nike-Inc/cerberus-management-service/blob/dac5f9d4c837956ca2d48f10732244815e7ebee2/src/main/java/com/nike/cerberus/cache/DatabaseCache.java

The App and MyBatis is configured with Guice:
https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java<https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java#L35>

The cache is wired up via
https://github.com/Nike-Inc/cerberus-management-service/blob/feature/secure_data_caching/src/main/java/com/nike/cerberus/server/config/guice/CmsMyBatisModule.java#L35
https://github.com/Nike-Inc/cerberus-management-service/blob/dac5f9d4c837956ca2d48f10732244815e7ebee2/src/main/resources/com/nike/cerberus/mapper/SecureDataMapper.xml#L23

What I need to do is bring in the application monitoring service so I can instrument the cache stats and send them to our metric aggregator so we can monitor and alert of the stats.

I was wondering if the Guice module had a hook to inject things into something like a custom cache impl?

Cheers,

Justin
--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com><mailto:mybatis-user%2Bunsu...@googlegroups.com<mailto:mybatis-user%252Buns...@googlegroups.com>><mailto:mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com><mailto:mybatis-user%2Bunsu...@googlegroups.com<mailto:mybatis-user%252Buns...@googlegroups.com>>>.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com><mailto:mybatis-user%2Bunsu...@googlegroups.com<mailto:mybatis-user%252Buns...@googlegroups.com>>.
fie...@gmail.com<mailto:fie...@gmail.com><mailto:fie...@gmail.com<mailto:fie...@gmail.com>>
LinkedIn<https://www.linkedin.com/in/fieldju> | @fieldju

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com><mailto:mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com>>.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user%2Bunsu...@googlegroups.com>.
fie...@gmail.com<mailto:fie...@gmail.com>
LinkedIn<https://www.linkedin.com/in/fieldju> | @fieldju

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com<mailto:mybatis-user...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/CANn6F%2Bsgf6vVMLcMuYrcODvAuL2LpwD0zEdyGMhpJqU3oP1jdQ%40mail.gmail.com<https://groups.google.com/d/msgid/mybatis-user/CANn6F%2Bsgf6vVMLcMuYrcODvAuL2LpwD0zEdyGMhpJqU3oP1jdQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Reply all
Reply to author
Forward
0 new messages