最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论