urldecoder报错

来源:4-5 实现极简IoC容器

weixin_慕莱坞5067944

2023-05-24 16:10:16

public class ClassPathXmlApplicationContext implements ApplicationContext{
    private Map iocContainer = new HashMap();
    public ClassPathXmlApplicationContext(){
        try{
            String filePath = this.getClass().getResource("/applicationContext.xml").getPath();
            filePath = new URLDecoder().decode(filePath, "UTF-8");
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(filePath));
            List<Node> nodes = document.getRootElement().selectNodes("bean");
            for(Node node : nodes){
                Element ele = (Element) node;
                String id = ele.attributeValue("id");
                String className = ele.attributeValue("class");
                Class c = Class.forName(className);
                Object obj = c.newInstance();
                iocContainer.put(id, obj);
            }
            System.out.println("IOC Container initialized");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    @Override
    public Object getBean(String beanId) {
        return iocContainer.get(beanId);
    }
}

报错java: URLDecoder() has private access in java.net.URLDecoder

写回答

1回答

好帮手慕小蓝

2023-05-24

同学你好~

1.猜测同学使用的版本中,decode方法已经是静态方法了,所以需要将语句更换为URLDecoder.decode()这样的静态调用方式。

2.如果更换为静态调用方式也会报错,建议同学将代码更改回来,然后将鼠标放置在标红的代码上,将报错信息提供一下;

祝学习愉快~

0

0 学习 · 9886 问题

查看课程