簡體   English   中英

類中定義的方法不可訪問

[英]method defined in class inaccessible

我正在關注這個例子: https : //www.boraji.com/spring-boot-configurationproperties-example要制作 Java Spring 屬性的嵌套列表,經過混淆和簡化,如下所示:

conf.property :

abc=item1,item2,item3

AppProperties.java (位於 xyzproperties 包中):

@EnableConfigurationProperties
@ConfigurationProperties(prefix = "a")
@Configuration
public class AppProperties {
    private String version;
    private String email;
    public BProperties b = new BProperties();



    static class BProperties {
        public List<String> c;

        public List getC() {
            return c;
        }

        public void setC(List c) {
            this.c= c;
        }
    }
}

FieldValidator.java(位於 xyzvalidation 包中):

public class IsItemValidator implements ConstraintValidator<IsItem, Object> {

    @Autowired
    public AppProperties appProperties;
    //... bunch of other stuff...

    private boolean hasValidItem(final Object item) {


        return appProperties.getB().getC().contains(item);
    }
}

為簡單起見,我沒有編寫所有的 getter/setter,它們都在那里,並且是公開的一切。

我仍然得到:

java: 在 abcproperties.BProperties 中的 getC() 是在不可訪問的類或接口中定義的

我嘗試使用谷歌搜索並查看類似的問題,但沒有一個答案對我來說有意義......這里發生了什么? 我已經根據我閱讀的有關不同軟件包的答案之一公開了所有內容..

將您的 BProperties 類聲明為 public:

public static class BProperties {

在您的代碼中,它具有包私有可見性,因此 IsItemValidator 類無法看到它,因為它位於另一個包中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM