Spring核心容器及两个核心容器引发的问题
项目结构
配置文件
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaoge</groupId> <artifactId>spring</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> </dependencies> </project>
-
bean.xml
<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.xiaoge.service.impl.AccountServiceImpl"></bean> <bean id="accountDao" class="com.xiaoge.dao.impl.AccountDaoImpl"></bean> </beans>
持久层接口
-
AccountDao
package com.xiaoge.dao; /** * @Author: 潇哥 * @DateTime: 2020/3/19 下午7:31 * @Description: TODO */ public interface AccountDao { public void saveAccount(); }
持久层实现类
-
AccountDaoImpl
package com.xiaoge.dao.impl; import com.xiaoge.dao.AccountDao; /** * @Author: 潇哥 * @DateTime: 2020/3/19 下午7:31 * @Description: TODO */ public class AccountDaoImpl implements AccountDao { public void saveAccount() { System.out.println("保存成功!"); } }
业务层接口
-
AccountService
package com.xiaoge.service; /** * @Author: 潇哥 * @DateTime: 2020/3/19 下午7:26 * @Description: 账户业务层接口 */ public interface AccountService { /** * 模拟保存 */ public void saveAccount(); }
业务层实现类
-
AccountServiceImpl
package com.xiaoge.service.impl; import com.xiaoge.dao.AccountDao; import com.xiaoge.dao.impl.AccountDaoImpl; import com.xiaoge.service.AccountService; /** * @Author: 潇哥 * @DateTime: 2020/3/19 下午7:28 * @Description: 账户业务层实现类 */ public class AccountServiceImpl implements AccountService { private AccountDao accountDao = new AccountDaoImpl(); public AccountServiceImpl(){ System.out.println("对象已创建"); } public void saveAccount() { accountDao.saveAccount(); } }
main
-
Client
package com.xiaoge.ui; import com.xiaoge.service.AccountService; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; /** * @Author: 潇哥 * @DateTime: 2020/3/19 下午7:35 * @Description: 模拟表现层, 调用业务层 */ public class Client { /** * 获取spring的Ioc核心容器, 并根据id获取对象 * * ApplicationContext的三个常用实现类 * 1. ClassPathXmlApplicationContext: 它可以加载类路径下的配置文件, 要求配置文件必须在类路径下. 不在的话. 加载不了. (1-2比较 1更常用) * 2. FileSystemXmlApplicationContext: 它可以加载磁盘任意路径下的配置文件(必须有访问权限) * * * 3. AnnotationConfigApplicationContext: 它是用于读取注解创建容器的. * * 核心容器的两个接口引发的问题: * ApplicationContext: 单例对象使用 采用此接口 * 它在构建核心容器时, 创建对象采取的策略是采用立即加载的方式. 也就是说, 只要一读取完配置文件马上就创建配置文件中的对象. * BeanFactory: 多例对象使用 * 它在构建核心容器时, 创建对象采取的策略是采用延迟加载的方式. 也就是说, 什么时候根据id获取对象了, 什么时候才真正的创建对象. * @param args */ public static void main(String[] args) { // 1. 获取核心容器对象 /* ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); 注意: mac 或 linux 在绝对路径前要加个 /(源码中对路径做了处理, 把去掉了开始的/) ApplicationContext ac = new FileSystemXmlApplicationContext("/" + "/Users/xiaoge/Desktop/bean.xml"); */ // 2. 根据id获取Bean对象 // ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); /* 两种方式都行: 第一种: 获取的是object类型自己强转 第二种: 传递字节码, 自动帮你转换成对应的类型 */ // AccountService as = (AccountService)ac.getBean("accountService"); // AccountDao ad = ac.getBean("accountDao", AccountDao.class); // System.out.println(as); // System.out.println(ad); /*********************BeanFactory 这种已经弃用***************************/ Resource resource = new ClassPathResource("bean.xml"); BeanFactory factory = new XmlBeanFactory(resource); AccountService as = (AccountService)factory.getBean("accountService"); System.out.println(as); } } // 运行结果 对象已创建 com.xiaoge.service.impl.AccountServiceImpl@6ea6d14e