程序员社区

ApplicationContext的三个常用实现类

在这里插入图片描述
在这里插入图片描述
FileSystemXmlApplicationContext:可以加载磁盘路径下的配置文件(不常用)

public class Client {
    public static void main(String[] args) {
        ApplicationContext context = 
			new FileSystemXmlApplicationContext("C:\\Users\\ghh\\Desktop\\bean.xml");
        AccountService service = context.getBean("accountService", AccountService.class);
        AccountDao accountDao = context.getBean("accountDao", AccountDao.class);
        System.out.println(service);
        System.out.println(accountDao);
    }
}

ClassPathXmlApplicationContext:可以加载类路径下的配置文件
要求配置文件必须在类路径下面

public class Client {
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        AccountService service = context.getBean("accountService", AccountService.class);
        AccountDao accountDao = context.getBean("accountDao", AccountDao.class);
        System.out.println(service);
        System.out.println(accountDao);
    }
}

AnnotationConfigApplicationContext:读取注解创建容器

赞(0) 打赏
未经允许不得转载:IDEA激活码 » ApplicationContext的三个常用实现类

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区