IOC控制反转核心:
1. ApplicationContext与BeanFactory
- 1.获取spring的Ioc核心容器,并根据id获取对象。
- 2.ApplicationContext的三个常用实现类:
ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)
ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
ApplicationContext ac= new FileSystemXmlApplicationContext("E:\\JAVA\\JAVA1\\IDEA程序\\javaee-黑马\\Spring_黑马\\SpringIOC\\src\\main\\resources\\bean.xml");
AnnotationConfigApplicationContext:它是用于读取注解创建容器的,是明天的内容。
- 3.核心容器的两个接口引发出的问题:
ApplicationContext: 单例对象适用 常采用此接口
它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
BeanFactory: 多例对象使用
它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。
BeanFactory 延迟加载
Resource resource=new ClassPathResource("bean.xml");
BeanFactory factory=new XmlBeanFactory(resource);
IAccountService ias= (IAccountService)ac.getBean("accountService");
System.out.println(ias);
- 4. 根据id获取bean对象的2种方式
2. 案例
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--把对象的创建交给spring来管理-->
<bean id="accountService" class="com.jh.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.jh.dao.impl.AccountDaoImpl"></bean>
</beans>
dao持久层
IAccountDao接口
package com.jh.dao;
/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 模拟保存账户
*/
void saveAccount();
}
AccountDaoImpl实现类
package com.jh.dao.impl;
import com.jh.dao.IAccountDao;
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
public void saveAccount(){
System.out.println("保存了账户");
}
}
service业务层
IAccountService接口
package com.jh.service;
/**
* 账户业务层的接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
AccountServiceImpl实现类
package com.jh.service.impl;
import com.jh.dao.IAccountDao;
import com.jh.dao.impl.AccountDaoImpl;
import com.jh.service.IAccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao = new AccountDaoImpl();
public AccountServiceImpl(){
System.out.println("对象创建了");
}
public void saveAccount(){
accountDao.saveAccount();
}
}
Client表现层
package com.jh.ui;
import com.jh.dao.IAccountDao;
import com.jh.service.IAccountService;
import com.jh.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* 模拟一个表现层,用于调用业务层
* */
public class Client {
public static void main(String[] args) {
//--------ApplicationContext 立即加载----------------------
//1.获取核心容器对象
ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
//ApplicationContext ac= new FileSystemXmlApplicationContext("E:\\JAVA\\JAVA1\\IDEA程序\\javaee-黑马\\Spring_黑马\\SpringIOC\\src\\main\\resources\\bean.xml");
//2.根据id获取Bean对象
IAccountService as= (IAccountService)ac.getBean("accountService");
IAccountDao adao=ac.getBean("accountDao",IAccountDao.class);
System.out.println(as);//com.jh.service.impl.AccountServiceImpl@1dd92fe2
System.out.println(adao);//com.jh.dao.impl.AccountDaoImpl@6b53e23f
System.out.println(as.toString());//com.jh.service.impl.AccountServiceImpl@1dd92fe2
}
}