Java后端避坑——属性为Boolean的gettter方法和is方法同时存在违反JavaBean规范

在作用户登陆的时候,一般咱们会判断当前帐号是否可用,将这个属性设置为Boolean类型。而咱们定义好的JavaBean须要实现UserDetails 接口来规范用户属性,而后重写里面的方法来判断当前的用户是否可用。这时候重写的is方法就会跟Mybatis自动生成的getter方法同时存在。以下面的实例程序。java

定义一个普通的Bean:

public class Hr implements UserDetails {
    private Boolean enabled;//帐号是否可用
    public String getEnabled() {
        return enabled;
    }
    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }
    **
     * 帐户是否可用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return enabled;
    }
}
复制代码

程序编译时出现的错误以下:

o.m.spring.mapper.MapperFactoryBean : Error while adding the mapper 'interface com.chb.vhr.mapper.HrMapper' to configuration. org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/chb/vhr/mapper/HrMapper.xml'. Cause: org.apache.ibatis.reflection.ReflectionException: Illegal overloaded getter method with ambiguous type for property enabled in class class com.chb.vhr.bean.Hr. This breaks the JavaBeans specification and can cause unpredictable results.spring

llegal overloaded getter method with ambiguous type for property enabled in class class com.chb.vhr.bean.Hr.
(该语句的意思是:Hr类中启用的属性类型不明确,非法重载getter方法)
由于isEnable至关于getEnable,致使JavaBean里面有两个getEnabled方法,违反了JavaBean的规范,只要将其中一个删掉就能够了。apache

聚沙成塔,滴水穿石!app

相关文章
相关标签/搜索