【发布时间】:2017-11-08 01:32:36
【问题描述】:
我正在使用 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 为空?
【问题讨论】: