# 配置设计 Mario中所有的配置都可以在 `Mario` 全局唯一对象完成,将它设计为单例。 要运行起来整个框架,Mario对象是核心,看看里面都需要什么吧! - 添加路由 - 读取资源文件 - 读取配置 - 等等 由此我们简单的设计一个Mario全局对象: ```java /** * Mario * @author biezhi * */ public final class Mario { /** * 存放所有路由 */ private Routers routers; /** * 配置加载器 */ private ConfigLoader configLoader; /** * 框架是否已经初始化 */ private boolean init = false; private Mario() { routers = new Routers(); configLoader = new ConfigLoader(); } public boolean isInit() { return init; } public void setInit(boolean init) { this.init = init; } private static class MarioHolder { private static Mario ME = new Mario(); } public static Mario me(){ return MarioHolder.ME; } public Mario addConf(String conf){ configLoader.load(conf); return this; } public String getConf(String name){ return configLoader.getConf(name); } public Mario addRoutes(Routers routers){ this.routers.addRoute(routers.getRoutes()); return this; } public Routers getRouters() { return routers; } /** * 添加路由 * @param path 映射的PATH * @param methodName 方法名称 * @param controller 控制器对象 * @return 返回Mario */ public Mario addRoute(String path, String methodName, Object controller){ try { Method method = controller.getClass().getMethod(methodName, Request.class, Response.class); this.routers.addRoute(path, method, controller); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } return this; } } ``` 这样在系统中永远保持一个Mario实例,我们用它来操作所有配置即可。 ### 在`Boostrap`的`init`方法中使用 ```java @Override public void init(Mario mario) { Index index = new Index(); mario.addRoute("/", "index", index); mario.addRoute("/html", "html", index); } ``` 这样,一个简单的MVC后端已经形成了!接下来我们要将结果展现在JSP文件中,要做视图的渲染设计 LET'S GO! ## links * [目录]() * 上一节: [控制器设计](<3.controller.md>) * 下一节: [视图设计](<5.view.md>)