最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

can't inject(?) spring @Service bean into jsf @Named managed bean via SpringBeanAutowiringSupport and @Autowired - Stack

matteradmin3PV0评论

spring and jsf both work on their own without any issues so I'm only including code which is relevant for the spring-jsf connection.

managed bean:

import jakarta.annotation.PostConstruct;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import .springframework.beans.factory.annotation.Autowired;
import .springframework.web.context.support.SpringBeanAutowiringSupport;

import java.io.Serializable;

@Named
@ViewScoped
public class ZManagedBean extends SpringBeanAutowiringSupport implements Serializable{
    private static final long serialVersionUID = 1L;

    @PostConstruct
    public void init() {
        System.out.println(getzService());
    }

    @Autowired
    ZService zService;

    public ZService getzService() {return zService;}
    public void setzService(ZService zService) {this.zService = zService;}


    public void register() {
        System.out.println("jsf alone works");
        zService.checkInjection();
    }

} 

its deploying successfully but gives an error when calling register().

important logs:

[INFO] [] [jakarta.enterprise.logging.stdout] [tid: _ThreadID=73 _ThreadName=admin-listener(3)] [levelValue: 800] [[
  .demoNoBoot2.zdemo.ZService@2816d266]]

first time initializing the managed bean, the service is injected successfully within the PostConstruct, but calling register() gives nullptrexc

[FATAL] [faces.context.exception.handler.log] [jakarta.enterprise.resource.webcontainer.faces.context] [tid: _ThreadID=59 _ThreadName=http-listener-1(3)] [levelValue: 1100] [[
  Cannot invoke ".demoNoBoot2.zdemo.ZService.checkInjection()" because "this.zService" is null]]

as the managed bean gets instantiated again (ViewScoped), injection fails (also in PostConstruct).

Making the managed bean @ApplicationScoped doesn't fix the problem (it still prints the zService address in the logs and also prints it again later (I think when I first call the page) but this time with zService address null)

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far