wicket @SpringBean can not create bean
Asked Answered
P

2

9

I have a project on Eclipse, Wicket, Spring, Hibernate. Every thing works normaly except : when I try

public class SortableContactDataProvider extends SortableDataProvider<User>
{
    @SpringBean
    private Service service;

    public Iterator<User> iterator(int first, int count)
    {
        //SortParam sp = getSort();
        return service.findAllUsers().subList(0, 15).iterator();
    }
...

the service variable is null? In any another places when I use this constuction "service" is not null and working well. Please help me to solve this problem.

Painstaking answered 9/7, 2010 at 7:6 Comment(1)
Had the same Problem and I just used @Autowired instead of @SpringBean to solve it. Don`t ask me why.Caput
A
17

@SpringBean works only in any Subclass of Component.

You need to do the following in your Constructor

Wicket 1.4

  InjectorHolder.getInjector().inject(this);

Wicket 1.5+

  org.apache.wicket.injection.Injector.get().inject(this);

See 'generic IDataProvider implementation' @ http://stronglytypedblog.blogspot.com/2009/03/wicket-patterns-and-pitfalls-1.html

Enjoy

Affiant answered 9/7, 2010 at 8:10 Comment(2)
I dont see InjectorHolder in Wicket 6. How to do this in wicket 6?Undulant
This has changed with with wicket 1.5. see cwiki.apache.org/confluence/display/WICKET/…Affiant
R
0

A bit more of context for those who are newbies to Wicket/Spring environment - as bert, pointed out, @SpringBean works only in any Subclass of Component so you'll need to drive the injection manually. This is a 2 step process:

Drive the injection in your class, something as:

public class SortableContactDataProvider extends SortableDataProvider<User>
{
    @SpringBean
    private Service service;

    public SortableContactDataProvider(){
        Injector.get().inject(this); // set up the injection
    }

    public Iterator<User> iterator(int first, int count)
    {
        return service.findAllUsers().subList(0, 15).iterator();
    }
}

And make sure the Injector is set up in Wicket application - something like:

public WicketApplication 

    @Override
    protected void init() {
        // make sure Spring injector is available and set up
        getComponentInstantiationListeners().add(new SpringComponentInjector(this));
    }
}
Rubbery answered 9/5, 2014 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.