IOC
Spring的依赖包
pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
在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.itheima.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>
spring的ioc:通过标签去创建对象的。
测试代码:
/**
- 模拟一个表现层,用于调用业务层
*/
public class Client {
// 模拟Action
/**
* 测试由ApplicationContext对象获取spring容器中创建的对象
* @param args
*/
public static void main(String[] args) {
// 代码之间存在依赖关系(耦合)
// AccountService accountService = new AccountServiceImpl();
// 由spring创建对象(完成对象的解耦)
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过名称调用(通过spring容器中的id属性)(推荐)
AccountService accountService = (AccountService) ac.getBean("accountService");
// 通过类型调用(通过spring容器中的class属性)
// AccountService accountService = ac.getBean(AccountServiceImpl.class);
accountService.saveAccount();
}
}
【**ApplicationContext **接口的实现类 】
ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件 (推荐)FileSystemXmlApplicationContext
它是从磁盘路径下加载配置文件,配置文件可以在磁盘的任意位置AnnotationConfigApplicationContext
当我们使用注解配置容器对象时,需要使用此类来创建spring容器,它用来读取注解
id和name的配置
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的三种方式
第一种:采用无参构造的方式实列化的方式
(1)applicationContext.xml
<!--创建Bean的三种方式 -->
<!-- 第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
(2)需要无参的构造方法
public class AccountServiceImpl implements AccountService {
//可以不写,默认就是无参构造
public AccountServiceImpl(){
System.out.println("对象创建了");
}
}
第二种:采用静态工厂实列化的方式
(1)applicationContext.xml
<!-- 第二种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) -->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
(2)AccountServiceImpl.java
public class AccountServiceImpl implements AccountService {
}
(3)StaticFactory.java,静态工厂类
/**
* 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class StaticFactory {
public static AccountService getAccountService(){
return new AccountServiceImpl();
}
}
第三种:采用实例工厂(非静态的)实例化的方式
(1)applicationContext.xml
<!-- 第三种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器) -->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
(2)AccountServiceImpl.java
public class AccountServiceImpl implements AccountService {
}
(3)InstanceFactory.java