简体   繁体   English

wicket @SpringBean无法创建bean

[英]wicket @SpringBean can not create bean

I have a project on Eclipse, Wicket, Spring, Hibernate. 我有一个关于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? 服务变量是null? In any another places when I use this constuction "service" is not null and working well. 在我使用这个构造的任何其他地方,“服务”不是空的并且运行良好。 Please help me to solve this problem. 请帮我解决这个问题。

@SpringBean works only in any Subclass of Component. @SpringBean仅适用于Component的任何Subclass。

You need to do the following in your Constructor 您需要在构造函数中执行以下操作

Wicket 1.4 Wicket 1.4

  InjectorHolder.getInjector().inject(this);

Wicket 1.5+ 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 请参阅'通用IDataProvider实施'@ http://stronglytypedblog.blogspot.com/2009/03/wicket-patterns-and-pitfalls-1.html

Enjoy 请享用

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. 对于那些是Wicket / Spring环境的新手来说,更多的上下文 - 正如bert所指出的那样,@ SpringBean只能在Component的任何Subclass中工作,所以你需要手动驱动注入。 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: 并确保Injector在Wicket应用程序中设置 - 类似于:

public WicketApplication 

    @Override
    protected void init() {
        // make sure Spring injector is available and set up
        getComponentInstantiationListeners().add(new SpringComponentInjector(this));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM