You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Christian Helmbold <ch...@yahoo.de> on 2009/02/28 20:29:36 UTC

NullPointerExceptions due to missing Spring constructor injection - Workaround

I've found a workaround. Not elegant, but it works:

public class ArticlePage extends WebPage
{
    @SpringBean
    private ArticleRepository repository;
    private Article article;

    public ArticlePage()
    {
        InjectorHolder.getInjector().inject(this);
        construct(repository.findByName("index"));
    }

    private void construct(Article article)
    {
        add(new Label("name", new PropertyModel(article, "name")));
        //...
    }

}

It looks a bit better than service locator pattern but not much. Any other suggestions?

Regards
Christian

-- 
http://www.groovy-forum.de



----- Ursprüngliche Mail ----
> Von: Christian Helmbold <ch...@yahoo.de>
> An: users@wicket.apache.org
> Gesendet: Samstag, den 28. Februar 2009, 20:14:02 Uhr
> Betreff: AW: NullPointerExceptions due to missing Spring constructor injection
> 
> Hi Martjin,
> 
> it is not possible to compile the code without static. Without static I get the 
> compiler error: "cannot reference repository before supertype constructor has 
> been called". But I cannot write 
> public ArticlePage()
> {
>        super(); 
>        this();
> }
> because each of them must be the first statement in the constructor. And I 
> cannot inject the reference directly into the constructor because of the 
> mentioned restriction of Wickets Spring integration (no constructor injection).
> 
> The only way out of this dilemma seems to be not to inject the repository. But 
> if I cannot use injected reference within constructors I don't know why I should 
> use Dependency Injection (DI). The constructors are very important in most 
> classes of Wicket applications. I could use the the service locator pattern with 
> it's pros and cons. But I hope there is a solution with DI ...
> 
> Regards
> Christian
> 
> -- 
> http://www.groovy-forum.de
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org



      


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


Re: NullPointerExceptions due to missing Spring constructor injection - Workaround

Posted by Igor Vaynberg <ig...@gmail.com>.
a) dont start a new thread for the same problem
b) you dont need this InjectorHolder.getInjector().inject(this); since
your repository is no longer abstract.

-igor

On Sat, Feb 28, 2009 at 11:29 AM, Christian Helmbold
<ch...@yahoo.de> wrote:
> I've found a workaround. Not elegant, but it works:
>
> public class ArticlePage extends WebPage
> {
>    @SpringBean
>    private ArticleRepository repository;
>    private Article article;
>
>    public ArticlePage()
>    {
>        InjectorHolder.getInjector().inject(this);
>        construct(repository.findByName("index"));
>    }
>
>    private void construct(Article article)
>    {
>        add(new Label("name", new PropertyModel(article, "name")));
>        //...
>    }
>
> }
>
> It looks a bit better than service locator pattern but not much. Any other suggestions?
>
> Regards
> Christian
>
> --
> http://www.groovy-forum.de
>
>
>
> ----- Ursprüngliche Mail ----
>> Von: Christian Helmbold <ch...@yahoo.de>
>> An: users@wicket.apache.org
>> Gesendet: Samstag, den 28. Februar 2009, 20:14:02 Uhr
>> Betreff: AW: NullPointerExceptions due to missing Spring constructor injection
>>
>> Hi Martjin,
>>
>> it is not possible to compile the code without static. Without static I get the
>> compiler error: "cannot reference repository before supertype constructor has
>> been called". But I cannot write
>> public ArticlePage()
>> {
>>        super();
>>        this();
>> }
>> because each of them must be the first statement in the constructor. And I
>> cannot inject the reference directly into the constructor because of the
>> mentioned restriction of Wickets Spring integration (no constructor injection).
>>
>> The only way out of this dilemma seems to be not to inject the repository. But
>> if I cannot use injected reference within constructors I don't know why I should
>> use Dependency Injection (DI). The constructors are very important in most
>> classes of Wicket applications. I could use the the service locator pattern with
>> it's pros and cons. But I hope there is a solution with DI ...
>>
>> Regards
>> Christian
>>
>> --
>> http://www.groovy-forum.de
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: NullPointerExceptions due to missing Spring constructor injection - Workaround

Posted by Adriano dos Santos Fernandes <ad...@gmail.com>.
Christian Helmbold wrote:
> I've found a workaround. Not elegant, but it works:
>
> public class ArticlePage extends WebPage
> {
>     @SpringBean
>     private ArticleRepository repository;
>     private Article article;
>
>     public ArticlePage()
>     {
>         InjectorHolder.getInjector().inject(this);
>         construct(repository.findByName("index"));
>     }
>
>     private void construct(Article article)
>     {
>         add(new Label("name", new PropertyModel(article, "name")));
>         //...
>     }
>
> }
>
> It looks a bit better than service locator pattern but not much. Any other suggestions?
Create a abstract (or not) function on the base class and call it from 
the base (now without arguments) constructor. Override this function to 
return what you want based on "repository".


Adriano


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


AW: NullPointerExceptions due to missing Spring constructor injection - Solution

Posted by Christian Helmbold <ch...@yahoo.de>.
Excuse my bit by bit mailings but I don't want to keep the solution secret. The problem with my first try was the missing implicit call to super() within the constructor because I used this() instead. The line
InjectorHolder.getInjector().inject(this);
is not necessary when I put the code in a separate method. this works:

public class ArticlePage extends WebPage
{
    @SpringBean
    private ArticleRepository repository;
    private Article article;

    public ArticlePage()
    {
        // no need to use InjectorHolder here
        construct(repository.findByName("index"));
    }

    private void construct(Article article)
    {
        add(new Label("name", new PropertyModel(article, "name")));
        // ...
    }

}

Is there something left to be improved?

Dependency Injection seems to be a bit tricky with wicket. Maybe something for the second edition of wicket in action ...

Regards
Christian

-- 
http://www.groovy-forum.de


      


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