我从春季服务中获得NullPointerException



我正在尝试构建一个带有Spring + JSF的基本网页。基本上,我有一个HelloControllerHelloSpringService.它在类HelloController返回helloSpringService.sayHello();行上给出错误。


public class HelloController {

    @ManagedProperty("#{helloSpringService)")
    private HelloSpringService helloSpringService;
    public String showHello() {
        return helloSpringService.sayHello();
    }
    public void setHelloSpringService(HelloSpringService helloSpringService) {
        this.helloSpringService = helloSpringService;
    }
    public HelloSpringService getHelloSpringService() {
        return helloSpringService;
    }
}

import org.springframework.stereotype.Service;
@Service
public class HelloSpringService {
    public String sayHello() {
        return "hellofromspringservice";
    }
}

使用 @Autowired 而不是 @ManagedProperty("#{helloSpringService)")

您已经创建了 bean HelloSpringService 作为服务 bean,因此 Spring 核心负责 bean 的生命周期,因此您必须使用 @Autowired 而不是 @ManagedProperty 来调用它。

helloSpringService

自动连线,而不是作为托管属性包含在

相关内容

最新更新