/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 模拟保存账户
*/
void saveAccount();
}
/**
* 账户业务层的接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
public void saveAccount(){
System.out.println("保存了账户");
}
}
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao = new AccountDaoImpl();
public void saveAccount(){
accountDao.saveAccount();
}
}
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
/**
*
*获取IOC的核心容器,并根据id获取对象
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 两种不同的方式获取Bean对象
IAccountService as = (IAccountService) ac.getBean("accountService");
IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
System.out.println(as);
System.out.println(adao);
// as.saveAccount();
}
}
它可以加载路径下的配置文件,要求配置文件必须在路径下,否则加载不了(配置文件所在的目录必须要在IDEA中设置为任何类型的目录但是一般设置为Resource类型的目录)
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
它可以加载磁盘下任意路径下的配置文件(必须有访问权限)
加载方式如下:
ApplicationContext ac = new FileSystemXmlApplicationContex("C:\\user\\greyson\\...")
它是用于读取注解创建容器的