play加载application.conf和加载mybatis的configuration.xml流程

application.conf作为play框架的配置文件,里面配置了各种属性,那么当框架运行的时候,到底是怎么去加载的呢?

刚看了下它的代码,它是在Play类中定义了一个静态变量 public static Properties configuration;

然后其中使用  readConfiguration() 去读取配置文件

public static void readConfiguration() {
        configuration = readOneConfigurationFile("application.conf", new HashSet<String>());
        // Plugins
        pluginCollection.onConfigurationRead();
    }

读取完 application.conf配置文件,configuration进行了初始化,然后通过 configuration.getProperty()方法,来获取配置中的属性

  private SqlSessionFactory getSqlSessionFactory() {
        if (sessionFactory == null) {
            String resource = Play.configuration.getProperty("mybatis.configuration");
            Reader reader;

            try {
                reader = Resources.getResourceAsReader(resource);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            Properties prop = new Properties();

            prop.put("url", Play.configuration.getProperty("db.url"));
            prop.put("user", Play.configuration.getProperty("db.user"));
            prop.put("pass", Play.configuration.getProperty("db.pass"));
            prop.put("driver", Play.configuration.getProperty("db.driver"));

            sessionFactory = new SqlSessionFactoryBuilder().build(reader, prop);

        }

        return sessionFactory;
    }
View Code

其中会根据该语句 

String resource = Play.configuration.getProperty("mybatis.configuration");

Reader reader;

reader = Resources.getResourceAsReader(resource);

去加载mybatis的配置文件,configuration.xml,大致就是这样子吧,我觉得

posted @ 2015-02-28 10:34  genpys  阅读(1158)  评论(0编辑  收藏  举报