ApplicationContext进行测试的简单使用

第一种:只有一个xml文件
public static void main(String[] args) {
        ApplicationContext ac = null;
        try {
            ac = new ClassPathXmlApplicationContext("beans.xml");
            //ac = new ClassPathXmlApplicationContext("classpath:beans.xml");
        } catch (Exception e) {
            LOGGER.error("spring启动错误", e);
        }


        Account test3 = (autoAccount) ac.getBean("account");
        test3.account();
}

 

 
第二种:有两个以上的文件,并且写出每个文件的名称
public static void main(String[] args) {
    ApplicationContext ac = null;
    try {
        ac = new ClassPathXmlApplicationContext(new String[] {"classpath:beans.xml", "classpath:quartz.xml"});
    } catch (Exception e) {
        LOGGER.error("spring启动错误", e);
    }


    Account test3 = (autoAccount) ac.getBean("account");
    test3.account();

}
 
第三种:通配符
public static void main(String[] args) {
        ApplicationContext ac = null;
        try {
            ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
        } catch (Exception e) {
            LOGGER.error("spring启动错误", e);
        }


        Account test3 = (autoAccount) ac.getBean("account");
        test3.account();
}
 
posted on 2018-02-26 13:58  荆棘Study  阅读(1048)  评论(0编辑  收藏  举报