You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by nquirynen <na...@pensionarchitects.be> on 2011/09/20 16:51:56 UTC

Console app with Tapestry IoC configuration

Hi,

I'm making an application using Tapestry IoC configuration (contributions).

So in my AppModule I have:

*public void
contributePolicyValueProviderSource(MappedConfiguration&lt;String,
PolicyValueProvider&gt; config) {
...
}*

and in code I do:

*@Inject
private PolicyValueProviderSource policyValueProvider;*


This does all work how it should work, but I want to make a simple console
application out of it (no webinterface).

So now i made a Main class where I have this:

	public static void main(String[] args) {
 	
	DocumentGenerator gen = new DocumentGenerator();

	}


DocumentGenerator:

	@Inject
	private static PolicyValueProviderSource policyValueProvider;
	
	public DocumentGenerator() {
		for(String field : policyValueProvider.getConfiguredFields()) {
			System.out.println(field);
		}
	}


But i geth the error:

/Exception in thread "main" java.lang.IllegalArgumentException: Contribution
be.pensionarchitects.merge.services.AppModule.contributeRequestHandler(OrderedConfiguration,
RequestFilter) (at AppModule.java:128) is for service 'RequestHandler',
which does not exist./


Anyone an idea what the problem could be here?
If you need specific code, please do ask.

Thanks in advance!
Nathan



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4822836.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Console app with Tapestry IoC configuration

Posted by Lenny Primak <lp...@hope.nyc.ny.us>.
You need to instantiate the registry, and then use bind() in your AppModule to do what you want.
You can search the net for further details on that  There have been threads on this before.

On Sep 20, 2011, at 10:51 AM, nquirynen wrote:

> Hi,
> 
> I'm making an application using Tapestry IoC configuration (contributions).
> 
> So in my AppModule I have:
> 
> *public void
> contributePolicyValueProviderSource(MappedConfiguration&lt;String,
> PolicyValueProvider&gt; config) {
> ...
> }*
> 
> and in code I do:
> 
> *@Inject
> private PolicyValueProviderSource policyValueProvider;*
> 
> 
> This does all work how it should work, but I want to make a simple console
> application out of it (no webinterface).
> 
> So now i made a Main class where I have this:
> 
> 	public static void main(String[] args) {
> 	
> 	DocumentGenerator gen = new DocumentGenerator();
> 
> 	}
> 
> 
> DocumentGenerator:
> 
> 	@Inject
> 	private static PolicyValueProviderSource policyValueProvider;
> 	
> 	public DocumentGenerator() {
> 		for(String field : policyValueProvider.getConfiguredFields()) {
> 			System.out.println(field);
> 		}
> 	}
> 
> 
> But i geth the error:
> 
> /Exception in thread "main" java.lang.IllegalArgumentException: Contribution
> be.pensionarchitects.merge.services.AppModule.contributeRequestHandler(OrderedConfiguration,
> RequestFilter) (at AppModule.java:128) is for service 'RequestHandler',
> which does not exist./
> 
> 
> Anyone an idea what the problem could be here?
> If you need specific code, please do ask.
> 
> Thanks in advance!
> Nathan
> 
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4822836.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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


Re: Console app with Tapestry IoC configuration

Posted by Taha Hafeez <ta...@gmail.com>.
Hi

As DocumentGenerator is a class you can use registry.autobuild to
create an instance. This will inject the dependencies for you.

On Wed, Sep 21, 2011 at 5:34 PM, Martin Strand
<do...@gmail.com> wrote:
> On Wed, 21 Sep 2011 13:19:51 +0200, nquirynen <na...@pensionarchitects.be>
> wrote:
>
>> if I do
>>     registry.getService(PolicyValueProviderSource.class)
>> and pass this as a parameter to my DocumentGenerator's constructor it does
>> work.
>> Is this the way to go then? (my knowledge of Tapestry IoC is very small)
>
>
> Yes, that is the correct way to retrieve a service.
> You could also define a DocumentGenerator service in your AppModule, and use
> "registry.getService(DocumentGenerator.class)" instead of "new
> DocumentGenerator(...)"
> Normally, you should not use "new" to instantiate a service; Tapestry should
> instantiate it for you and automatically inject dependencies.
> But in your case this should work just fine.
>
> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>



-- 
Regards

Taha Hafeez Siddiqi (tawus)
http://tawus.wordpress.com

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


Re: Console app with Tapestry IoC configuration

Posted by Martin Strand <do...@gmail.com>.
On Wed, 21 Sep 2011 13:19:51 +0200, nquirynen  
<na...@pensionarchitects.be> wrote:

> if I do
>      registry.getService(PolicyValueProviderSource.class)
> and pass this as a parameter to my DocumentGenerator's constructor it  
> does work.
> Is this the way to go then? (my knowledge of Tapestry IoC is very small)


Yes, that is the correct way to retrieve a service.
You could also define a DocumentGenerator service in your AppModule, and  
use "registry.getService(DocumentGenerator.class)" instead of "new  
DocumentGenerator(...)"
Normally, you should not use "new" to instantiate a service; Tapestry  
should instantiate it for you and automatically inject dependencies.
But in your case this should work just fine.

Martin

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


Re: Console app with Tapestry IoC configuration

Posted by nquirynen <na...@pensionarchitects.be>.
Removed the "static" (shouldnt be there anyway), but still the same problem
:/


if I do 
     registry.getService(PolicyValueProviderSource.class)
and pass this as a parameter to my DocumentGenerator's constructor it does
work.
Is this the way to go then? (my knowledge of Tapestry IoC is very small)

Thanks
Nathan



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4825992.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Console app with Tapestry IoC configuration

Posted by Steve Eynon <st...@alienfactory.co.uk>.
No, @Inject is an IOC annotation and works for services. I've never
tried injecting a static service though...

@Inject
private static PolicyValueProviderSource policyValueProvider;

maybe try deleting the 'static' keyword, oh, and ensure you're using
the Tapestry @Inject and not javax or anyother.

Steve.


On 21 September 2011 16:18, Chris Poulsen <ma...@nesluop.dk> wrote:
> Hi,
>
> I thought the @Inject was a part of the web app goodness that only works in
> those special packages and constructor injection is the way to go when using
> the IoC stand-alone?
>
> (This page shows an example using this:
> http://wiki.apache.org/tapestry/Tapestry5HowToIocOnly )
>
> --
> Chris
>
> On Wed, Sep 21, 2011 at 9:37 AM, nquirynen <na...@pensionarchitects.be>wrote:
>
>> I have made a new project and have now this:
>>
>> *Main.java*
>>
>> public static void main(String[] args) {
>>         RegistryBuilder builder = new RegistryBuilder();
>>        builder.add(AppModule.class);
>>
>>        Registry registry = builder.build();
>>        registry.performRegistryStartup();
>>
>>                DocumentGenerator gen = new DocumentGenerator();
>>
>>        registry.cleanupThread();
>>        registry.shutdown();
>>        }
>>
>> *AppModule.java*
>>
>> public static void bind(ServiceBinder binder)
>>    {
>>        binder.bind(PolicyValueProviderSource.class,
>> PolicyValueProviderSourceImpl.class);
>>     }
>>
>>
>>    public void
>> contributePolicyValueProviderSource(MappedConfiguration&lt;String,
>> PolicyValueProvider&gt; config) {
>>         config.add("name", new PolicyValueProvider(){
>>                @Override
>>                public String getValue(Policy policy) {
>>                        return policy.getHolder().getName();
>>                }
>>        });
>>        config.add("firstname", new PolicyValueProvider(){
>>                @Override
>>                public String getValue(Policy policy) {
>>                        return policy.getHolder().getFirstName();
>>                }
>>        });
>>    }
>>
>> *
>> DocumentGenerator.java*
>>
>> @Inject
>>        private static PolicyValueProviderSource policyValueProvider;
>>
>>        public DocumentGenerator() {
>>                 for(String v : policyValueProvider.getConfiguredFields()) {
>>                        System.out.println(v+" : ");
>>                }
>>        }
>>
>>
>>
>>
>> But now I always get a NullPointerException on *policyValueProvider* in
>> *DocumentGenerator.*
>>
>> --
>> View this message in context:
>> http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4825438.html
>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>

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


Re: Console app with Tapestry IoC configuration

Posted by Chris Poulsen <ma...@nesluop.dk>.
Hi,

I thought the @Inject was a part of the web app goodness that only works in
those special packages and constructor injection is the way to go when using
the IoC stand-alone?

(This page shows an example using this:
http://wiki.apache.org/tapestry/Tapestry5HowToIocOnly )

-- 
Chris

On Wed, Sep 21, 2011 at 9:37 AM, nquirynen <na...@pensionarchitects.be>wrote:

> I have made a new project and have now this:
>
> *Main.java*
>
> public static void main(String[] args) {
>         RegistryBuilder builder = new RegistryBuilder();
>        builder.add(AppModule.class);
>
>        Registry registry = builder.build();
>        registry.performRegistryStartup();
>
>                DocumentGenerator gen = new DocumentGenerator();
>
>        registry.cleanupThread();
>        registry.shutdown();
>        }
>
> *AppModule.java*
>
> public static void bind(ServiceBinder binder)
>    {
>        binder.bind(PolicyValueProviderSource.class,
> PolicyValueProviderSourceImpl.class);
>     }
>
>
>    public void
> contributePolicyValueProviderSource(MappedConfiguration&lt;String,
> PolicyValueProvider&gt; config) {
>         config.add("name", new PolicyValueProvider(){
>                @Override
>                public String getValue(Policy policy) {
>                        return policy.getHolder().getName();
>                }
>        });
>        config.add("firstname", new PolicyValueProvider(){
>                @Override
>                public String getValue(Policy policy) {
>                        return policy.getHolder().getFirstName();
>                }
>        });
>    }
>
> *
> DocumentGenerator.java*
>
> @Inject
>        private static PolicyValueProviderSource policyValueProvider;
>
>        public DocumentGenerator() {
>                 for(String v : policyValueProvider.getConfiguredFields()) {
>                        System.out.println(v+" : ");
>                }
>        }
>
>
>
>
> But now I always get a NullPointerException on *policyValueProvider* in
> *DocumentGenerator.*
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4825438.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Console app with Tapestry IoC configuration

Posted by nquirynen <na...@pensionarchitects.be>.
I have made a new project and have now this:

*Main.java*

public static void main(String[] args) {
        RegistryBuilder builder = new RegistryBuilder();
        builder.add(AppModule.class);
        
        Registry registry = builder.build();
        registry.performRegistryStartup();
		
		DocumentGenerator gen = new DocumentGenerator();	

        registry.cleanupThread();
        registry.shutdown();
	}

*AppModule.java*

public static void bind(ServiceBinder binder)
    {
    	binder.bind(PolicyValueProviderSource.class,
PolicyValueProviderSourceImpl.class);
    }
    
    
    public void
contributePolicyValueProviderSource(MappedConfiguration&lt;String,
PolicyValueProvider&gt; config) {
    	config.add("name", new PolicyValueProvider(){
    		@Override
    		public String getValue(Policy policy) {
    			return policy.getHolder().getName();
    		}
    	});
    	config.add("firstname", new PolicyValueProvider(){
    		@Override
    		public String getValue(Policy policy) {
    			return policy.getHolder().getFirstName();
    		}
    	});
    }

*
DocumentGenerator.java*

@Inject
	private static PolicyValueProviderSource policyValueProvider;
	
	public DocumentGenerator() {
		for(String v : policyValueProvider.getConfiguredFields()) {
			System.out.println(v+" : ");
		}
	}




But now I always get a NullPointerException on *policyValueProvider* in
*DocumentGenerator.*

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4825438.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Console app with Tapestry IoC configuration

Posted by nquirynen <na...@pensionarchitects.be>.
Steve Eynon wrote:
> 
> I believe the @Inject is working just fine - you just have to access
> the service *after* the class has been constructed!
> 

Yep that was my problem.

Thanks for the explanation!


--
View this message in context: http://tapestry.1045711.n5.nabble.com/Console-app-with-Tapestry-IoC-configuration-tp4822836p4832923.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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