IOC 控制反转
我们在开发中,有些依赖关系是必须的,有些依赖关系可以通过优化代码来解除的。 请看下面的示例代码:
原来: 我们在获取对象时,都是采用new的方式。是主动的
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao = new AccountDaoImpl();
}
上面的代码表示: 业务层调用持久层,并且此时业务层在依赖持久层的接口和实现类。如果此时没有持久层实现类,编译将不能通过。这种编译期依赖关系,应该在我们开发中杜绝。我们需要优化代码解决。
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao = accountDao;
}
}
原来: 我们在获取对象时,都是采用new的方式,是主动的。
现在: 我们获取对象时,同时跟工厂要,有工厂为我们查找或者创建对象。是被动的。
这种被动接收的方式获取对象的思想就是控制反转(IOC),它是spring框架的核心之一。
明确ioc的作用: 削减计算机程序的耦合(解除我们代码中的依赖关系),将对象的创建和调用都交给spring容器去处理。
那么怎么使用spring的IOC解决程序耦合???
第1步:创建Dao创建接口AccountDao.java
/**
* 账户的持久层接口
*/
public interface AccountDao {
/**
* 模拟保存账户
*/
void saveAccount();
}
创建实现类AccountDaoImpl.java
/**
* 账户的持久层实现类
*/
public class AccountDaoImpl implements AccountDao {
public void saveAccount(){
System.out.println("AccountDaoImpl 保存了账户");
}
}
第2步:创建Service创建接口AccountService.java
/**
* 账户业务层的接口
*/
public interface AccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
创建接口的实现类AccountServiceImpl.java
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao ;
public AccountServiceImpl(){
System.out.println("对象创建了");
}
public void saveAccount(){System.out.println("AccountServiceImpl 保存了账户");
accountDao.saveAccount();
}
}
第3步:在resource下创建applicationContext.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.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="comdao.impl.AccountDaoImpl"></bean>
</beans>
注意:
id中不能出现特殊字符(id是容器中的唯一标识),name中可以出现特殊的字符(表示引用)。可以指定多个name,之间可以用分号(“;”)、空格(“ ”)或逗号(“,”)分隔开,如果没有指定id,那么第一个name为标识符,其余的为别名; 若指定了id属性,则id为标识符,所有的name均为别名。如:
<bean name="alias1 alias2;alias3,alias4" id="hello1" class="com.zyh.spring3.hello.HelloWorld"> </bean>
此时,hello1为标识符,而alias1,alias2,alias3,alias4为别名,它们都可以作为Bean的键值;
实例化Bean的三种方式
第一种:采用无参数的构造方法方式实例化(用的最多)applicationContext.xml
<!-- 第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
-->
<bean id="accountService" class="com.service.impl.AccountServiceImpl"></bean>
需要无参的构造方法
public class AccountServiceImpl implements AccountService {
//可以不写,默认就是无参构造
public AccountServiceImpl(){
System.out.println("对象创建了");
}
}
第二种:采用静态工厂实例化的方式
applicationContext.xml
<!-- 第二种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) -->
<bean id="accountService" class="com.factory.StaticFactory" factory-method="getAccountService"></bean>
AccountServiceImpl.java
public class AccountServiceImpl implements AccountService {
}
StaticFactory.java,静态工厂类
/**
* 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class StaticFactory {
public static AccountService getAccountService(){
return new AccountServiceImpl();
}
}
第三种:采用实例工厂(非静态的)实例化的方式
applicationContext.xml
<!-- 第三种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器) -->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
AccountServiceImpl.java
public class AccountServiceImpl implements AccountService {
}
InstanceFactory.java
/**
* 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class InstanceFactory {
public AccountService getAccountService(){
return new AccountServiceImpl();
}
}
总结:第一种无参构造的方式用的最多,第二种和第三种一般是调用spring底层写好类的时候会用到。
第4步:ApplicationContext接口对象
/**
模拟一个表现层,用于调用业务层
【ApplicationContext 接口的实现类 】
(1)ClassPathXmlApplicationContext:
它是从类的根路径下加载配置文件 推荐使用这种
(2)FileSystemXmlApplicationContext:
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。注意磁盘的权限
(3)AnnotationConfigApplicationContext:
当我们使用注解配置容器对象时,需要使用此类来创建spring 容器。它用来读取注解。
*/
public static void main(String[] args) {
// 测试ClassPathXmlApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 测试FileSystemXmlApplicationContext
ApplicationContext ac = new FileSystemXmlApplicationContext("d:/applicationContext.xml");
AccountService accountService = (AccountService) ac.getBean("accountService");
System.out.println(accountService);
AccountService accountService1 = (AccountService) ac.getBean("accountService");
System.out.println(accountService1);
}
【BeanFactory 和ApplicationContext 的区别 】
BeanFactory 才是Spring 容器中的顶层接口。
ApplicationContext 是它的子接口。
BeanFactory 和ApplicationContext 的区别:创建的方式都表示单例对象。
创建对象的时间点不一样。ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。(立即加载)
BeanFactory:什么时候使用什么时候创建对象。(延迟加载)【ApplicationContext 接口的实现类 】
(1)ClassPathXmlApplicationContext:(重点)它是从类的根路径下加载配置文件 推荐使用这种 。
(2)FileSystemXmlApplicationContext: 它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
(3)AnnotationConfigApplicationContext: 当我们使用注解配置容器对象时,需要使用此类来创建spring 容器。它用来读取注解。