麻绳先生

做一些记录性的工作

Spring的IOC概述

通过工厂模式

通过工厂模式,应用和资源得以分开,降低耦合。

控制反转

把创建对象的权利交给框架或工厂,它包括依赖注入和依赖查找。

spring基于XML的IOC环境搭建和入门

1
2
3
4
5
//获取核心容器对象
ApplicationContent ac = new ClassPathXmlApplicationContent("bena.xml");
//根据id获取Bean对象
IAccountService as = (IAccountService)as.getBean("accountService");
IAccountDao adao = ac.getBean("accountDao", IAccountDao.class);
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd>
<!--把对象的创建交给spring来管理-->
<bean id="accountService" class="com.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.dao.impl.AccountDaoImpl"></bean>
</beans>

核心容器的两个接口

ApplicationContent

在构建核心容器时,创建对象采用立即加载的方式;单例对象适用;

BeanFactory

在构建核心容器时,创建对象采用延时加载的范式;多例对象适用;