在JSF中使用Autowired时出现NullPointerException

时间:2017-11-08 01:32:36

标签: spring jsf

我正在使用Spring-boot,JSF和MyBatis。问题是当我尝试使用MyBatis映射器时,我得到一个NullPointerException。 MyBatis连接工作正常,我已经在Autowired的单元测试中测试过它(就像我在Service类中做的那样)。

这是我的JSF控制器:

@ManagedBean
@ViewScoped
public class WeatherView implements Serializable {

    private List<Weather> weathers;

    @ManagedProperty(value = WeatherService.EL_NAME)
    private WeatherService weatherService;

    @PostConstruct
    public void init(){
        setWeathers(this.weatherService.getAll());
    }

    public List<Weather> getWeathers() {
        return this.weathers;
    }

    public void setWeathers(List<Weather> weathers) {
        this.weathers = weathers;
    }

    public void setWeatherService(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

WeatherService.java

@Service(WeatherService.BEAN_NAME)
@ManagedBean(name = WeatherService.BEAN_NAME)
@ApplicationScoped
public class WeatherService {

    public static final String BEAN_NAME = "weatherService";

    public static final String EL_NAME = "#{weatherService}";

    @Autowired
    private WeatherMapper weatherMapper; // <-- This is the MyBatis Mapper

    // The NullPointerExceptions is thrown here
    public List<Weather> getAll(){
        return weatherMapper.getAll();
    }

}

如果我使用Autowired注释,为什么weatherMapper为null?

1 个答案:

答案 0 :(得分:-1)

你应该检查“weatherMapper”类,我认为查询在那里返回null。