package com.lwb.ui;
import com.lwb.dao.IAccountDao;
import com.lwb.service.IAccountService;
import com.lwb.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
/**
* 获取spring的Ioc核心容器,并根据id获取对象
*
* ApplicationContext的三个常用实现类
* ClassPathXmlApplication:它可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了(更常用)
* FileSystemXmlApplication:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
*
* AnnotationConfigApplicationContext:它是用于读取注解创建容器的
*
* @param args
*/
public static void main(String[] args) {
//1.获取核心容器对象
// ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext ac= new FileSystemXmlApplicationContext("C:\\Users\\lwb\\IdeaProjects\\day01_spring\\src\\main\\resources\\bean.xml");
//2.根据id获取Bean对象(两种方式)
IAccountService as = (IAccountService) ac.getBean("accountService");
System.out.println(as);
for(int i=0;i<3;i++){
IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
System.out.println(adao);
}
}
}
com.lwb.service.impl.AccountServiceImpl@770c2e6b
com.lwb.dao.impl.AccountDaoIml@1a052a00
com.lwb.dao.impl.AccountDaoIml@1a052a00
com.lwb.dao.impl.AccountDaoIml@1a052a00
可以看到,spring中是单例模式。