You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Pavel Vodenski <pa...@beckon.com> on 2012/05/01 04:04:13 UTC

Hooking a custom InjectionProvider into the IoC container

Tapestry Users,

I'd like to contribute an
InjectionProvider2<http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/transform/InjectionProvider2.html>
into
the chain that processes @Inject annotations. The cookbook example that
illustrates the chain of command
pattern<http://tapestry.apache.org/ioc-cookbook-patterns.html> cites
how Tapestry IOC's own injection provider is built. The example says, "And,
of course, other contributions could be made in other modules ... if you
wanted to add in your own form of injection." So, I tried copying a similar
method into my AppModule:

    public static void contributeInjectionProvider2(
            OrderedConfiguration<InjectionProvider2> configuration,
            MasterObjectProvider masterObjectProvider,
            ObjectLocator locator,
            SymbolSource symbolSource,
            AssetSource assetSource)
    {
        configuration.add("BeckonLogger", new InjectionProvider2() {
            public boolean provideInjection(PlasticField field,
ObjectLocator locator1, MutableComponentModel componentModel) {
                if
(field.getTypeName().equals("com.beckon.app.util.log.BeckonLogger")) {

field.inject(BeckonLoggerImpl.getLogger(field.getPlasticClass().getClassName()));
                    return true;
                }
                return false;
            }
        });
    }


But that gives me the following error:
java.lang.IllegalArgumentException: Contribution
com.beckon.app.services.AppModule.contributeInjectionProvider2(OrderedConfiguration,
MasterObjectProvider, ObjectLocator, SymbolSource, AssetSource) (at
AppModule.java:92) is for service 'InjectionProvider2', which does not
exist.

Is there a different way that I should contribute my InjectionProvider2?
More generally, what is the best way to add a custom form of injection?
More generally still, my overarching goal is to be able to instiate my own
logger with the name of the owning class and to use @Inject to describe the
dependency--is there a way to customize or otherwise use one of the
existing InjectionProviders to do that?

Thank you in advance,
- Pavel

Hooking a custom InjectionProvider into the IoC container

Posted by Lance Java <la...@googlemail.com>.
Tapestry already provides this functionality. You can @Inject
org.slf4j.Logger

Re: Hooking a custom InjectionProvider into the IoC container

Posted by Pavel Vodenski <pa...@beckon.com>.
After some more experimentation, I've corrected an error in my original
contribution method. It should've read:


    public static void contributeInjectionProvider(
            OrderedConfiguration<InjectionProvider2> configuration,
            MasterObjectProvider masterObjectProvider,
            ObjectLocator locator,
            SymbolSource symbolSource,
            AssetSource assetSource)
    {
        configuration.add("BeckonLogger", new
BeckonLoggerInjectionProvider(), "before:Default");
    }

When I include this method in my AppModule, my InjectionProvider
implementation get a chance to provide implementations for Pages and
Components, which is most of what I'd like to do. However, I'd also like my
InjectionProvider to act on objects built during the Registry Startup
phase. In particular, I configure a database in a submodule method
annotated with @Startup. @Inject-annotated fields of objects built in that
phase are not seen by my InjectionProvider.

I found a thread from last year that discusses a very similar goal:
http://markmail.org/thread/ny4xefhxzuwrus3e. That thread culminated in
tapestry-cdi: https://github.com/magnuskvalheim/tapestry-cdi . I tried to
follow the same approach--contributing my InjectionProvider in a totally
separate Module which tapestry should've seen via autoloading, but I can't
seem to get autoloading right. I added the following block to my pom.xml:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <manifestEntries>

<Tapestry-Module-Classes>com.beckon.util.log.BeckonLoggerInjectionModule</Tapestry-Module-Classes>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

This correctly adds the Tapestry-Module-Classes line to the MANIFEST.MF of
jars built using mvn jar:jar, but I don't believe it affects the archive
built by mvn jetty:run. In any case, my LoggerInjectionModule doesn't
appear in the list of modules added by ioc.RegistryBuilder.

Any suggestions regarding contributing InjectionProviders before the
Startup phase or troubleshooting autoloading when using the jetty:run maven
goal?

Thank you,
- Pavel


On Tue, May 1, 2012 at 2:28 PM, Pavel Vodenski <pa...@beckon.com> wrote:

> > The error message says you're trying to contribute configuration for a
> service that doesn't exist. Are you trying to run this without
> tapestry-core (the web framework)?
>
> I am using tapestry-core. The snippet I posted came out of our tapestry
> project's AppModule class.
>
>
> On Tue, May 1, 2012 at 5:59 AM, Thiago H. de Paula Figueiredo <
> thiagohp@gmail.com> wrote:
>
>> On Mon, 30 Apr 2012 23:04:13 -0300, Pavel Vodenski <pa...@beckon.com>
>> wrote:
>>
>>  But that gives me the following error:
>>> java.lang.**IllegalArgumentException: Contribution
>>> com.beckon.app.services.**AppModule.**contributeInjectionProvider2(**
>>> OrderedConfiguration,
>>> MasterObjectProvider, ObjectLocator, SymbolSource, AssetSource) (at
>>> AppModule.java:92) is for service 'InjectionProvider2', which does not
>>> exist.
>>>
>>
>> The error message says you're trying to contribute configuration for a
>> service that doesn't exist. Are you trying to run this without
>> tapestry-core (the web framework)?
>>
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>> and instructor
>> Owner, Ars Machina Tecnologia da Informação Ltda.
>> http://www.arsmachina.com.br
>>
>
>

Re: Hooking a custom InjectionProvider into the IoC container

Posted by Pavel Vodenski <pa...@beckon.com>.
> The error message says you're trying to contribute configuration for a
service that doesn't exist. Are you trying to run this without
tapestry-core (the web framework)?

I am using tapestry-core. The snippet I posted came out of our tapestry
project's AppModule class.

On Tue, May 1, 2012 at 5:59 AM, Thiago H. de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Mon, 30 Apr 2012 23:04:13 -0300, Pavel Vodenski <pa...@beckon.com>
> wrote:
>
>  But that gives me the following error:
>> java.lang.**IllegalArgumentException: Contribution
>> com.beckon.app.services.**AppModule.**contributeInjectionProvider2(**
>> OrderedConfiguration,
>> MasterObjectProvider, ObjectLocator, SymbolSource, AssetSource) (at
>> AppModule.java:92) is for service 'InjectionProvider2', which does not
>> exist.
>>
>
> The error message says you're trying to contribute configuration for a
> service that doesn't exist. Are you trying to run this without
> tapestry-core (the web framework)?
>
> --
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
> and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
>

Re: Hooking a custom InjectionProvider into the IoC container

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 30 Apr 2012 23:04:13 -0300, Pavel Vodenski <pa...@beckon.com>  
wrote:

> But that gives me the following error:
> java.lang.IllegalArgumentException: Contribution
> com.beckon.app.services.AppModule.contributeInjectionProvider2(OrderedConfiguration,
> MasterObjectProvider, ObjectLocator, SymbolSource, AssetSource) (at
> AppModule.java:92) is for service 'InjectionProvider2', which does not
> exist.

The error message says you're trying to contribute configuration for a  
service that doesn't exist. Are you trying to run this without  
tapestry-core (the web framework)?

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org