简体   繁体   中英

How to autowire Spring bean in Wicket Session class?

I have a Wicket Session class as follows

public class IASession extends AuthenticatedWebSession {
    private static final long serialVersionUID = 3529263965780210677L;

    @SpringBean
    private UserService userService;

    public IASession(Request request) {
        super(request);
    }

    @Override
    public boolean authenticate(String username, String password) {

        // Get the user
        UserDetailsDTO user = userService.findByEmail(username);

        if(null != user && user.getPassword().equals(password))
            return true;
        else
            return false;
    }

    @Override
    public Roles getRoles() {
        Roles roles = new Roles();
        roles.add("SIGNED_IN");
        return roles;
    }
}

In this class, I am trying to autowire Spring service using wicket-spring annnotation @SpringBean . But when I am trying to login, it giving me error.

Last cause: null
WicketMessage: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at [StatelessForm [Component id = login-form]] on component [StatelessForm [Component id = login-form]] threw an exception

Wicket is unable to autowire the userService spring bean and that is why it's null .

What can I do to fix this?

Since the Session is not a Component or Behavior you'll have to overwrite the constructor and call Injector.get.inject(this). See the SpringComponentInjector doc.

    public IASession(Request request) {
       super(request);
       Injector.get().inject(this);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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