Spring02_IOC使用(XML)
案例介绍准备:
1、使用案例:
解决账户的业务层和持久层的依赖关系
2、Spring的开发包
下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring
解压后的目录结构:
docs:API和开发规范
libs:jar包和源码
schema:约束
特别说明:
Spring5版本使用的是jdk8编写的,所以要求jdk必须在8以上,同时tomcat要求在8.5以上
3、代码准备
//创建业务层接口和实现类 /** * 账户的业务层接口 */ public interface IAccountService { /** * 保存账户(此处只是模拟,并不是真的要保存) */ void saveAccount(); } /** * 账户的业务层实现类 */ public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao = new AccountDaoImpl();//此处的依赖关系有待解决 @Override public void saveAccount() { accountDao.saveAccount(); } }
//创建持久层接口和实现类 /** * 账户的持久层接口 */ public interface IAccountDao { /** * 保存账户 */ void saveAccount(); } /** * 账户的持久层实现类 */ public class AccountDaoImpl implements IAccountDao { @Override public void saveAccount() { System.out.println("保存了账户"); } }
基于XML方式的IOC使用:
1、入门案例:
1)拷贝必备jar包
2)在类的根路径下创建任意名字的一个xml文件(不能包含中文名字)
3)给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"> </beans>
4)让spring管理资源,在配置文件中配置service和dao
<!-- bean 标签:用于配置让 spring 创建对象,并且存入 ioc 容器之中 id 属性:对象的唯一标识。 class 属性:指定要创建对象的全限定类名 --> <!-- 配置 service --> <bean id="accountService" class="service.impl.AccountServiceImpl"> </bean> <!-- 配置 dao --> <bean id="accountDao" class="dao.impl.AccountDaoImpl"></bean>
5)测试配置
public static void main(String[] args) { //1.使用 ApplicationContext 接口,就是在获取 spring 容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2.根据 bean 的 id 获取对象 IAccountService aService = (IAccountService) ac.getBean("accountService"); System.out.println(aService); IAccountDao aDao = (IAccountDao) ac.getBean("accountDao"); System.out.println(aDao); }
2、配置细节介绍:
1)spring中工程的类结构图
2)BeanFactory和ApplicationContext的区别
a:BeanFactory才是spring容器的顶层接口,ApplicationContext只是它的子接口
b:BeanFactory创建对象的时机:什么时候使用什么时候创建
ApplicationContext创建对象的时机:只要读取了配置文件,默认情况就会创建对象
3)ApplicationContext接口的常用实现类
3、IOC中bean标签介绍以及管理对象的细节
1)bean标签
2)bean标签的作用范围和生命周期
3)实例化bean的三种方式
方式一:使用默认的无参构造函数
<!--在默认情况下: 它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。 --> <bean id="accountService" class="service.impl.AccountServiceImpl"/>
方式二:spring管理静态工厂,使用静态工厂的方法创建对象
/** * 模拟一个静态工厂,创建业务层实现类 */ public class StaticFactory { public static IAccountService createAccountService(){ return new AccountServiceImpl(); } }
<!-- 此种方式是: 使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器 id 属性:指定 bean 的 id,用于从容器中获取 class 属性:指定静态工厂的全限定类名 factory-method 属性:指定生产对象的静态方法 --> <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="createAccountService"></bean>
方式三:spring管理实例工厂,实例工厂的方法创建对象
/** * 模拟一个实例工厂,创建业务层实现类 * 此工厂创建对象,必须现有工厂实例对象,再调用方法 */ public class InstanceFactory { public IAccountService createAccountService(){ return new AccountServiceImpl(); } }
<!-- 此种方式是: 先把工厂的创建交给 spring 来管理。 然后在使用工厂的 bean 来调用里面的方法 factory-bean 属性:用于指定实例工厂 bean 的 id。 factory-method 属性:用于指定实例工厂中创建对象的方法。 --> <bean id="instancFactory" class="com.itheima.factory.InstanceFactory"></bean> <bean id="accountService" factory-bean="instancFactory" factory-method="createAccountService"></bean>
4)spring的依赖注入
a:依赖注入(Dependency Injection)的概念(是spring框架IOC的核心体现)
我们在程序的编写时,通过控制反转把对象的创建交给了spring容器,但是实际代码中不可能没有依赖的出现。ioc解耦只是降低了依赖,不是消除。例如:我们的业务层任然会调用持久层方法。
那么这种业务层和持久层的依赖关系,使用spring后就交给了spring管理。就是等待框架把持久层对象传递给业务层,而不用我们自己获取。
b:注入方式
方式一:构造函数注入
顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置 的方式,让 spring 框架来为我们注入。具体代码如下: public class AccountServiceImpl implements IAccountService { private String name; private Integer age; private Date birthday;
public AccountServiceImpl(String name, Integer age, Date birthday) { this.name = name; this.age = age; this.birthday = birthday; } @Override public void saveAccount() { System.out.println(name+","+age+","+birthday); } }
<!-- 使用构造函数的方式,给 service 中的属性传值 要求: 类中需要提供一个对应参数列表的构造函数。 涉及的标签: constructor-arg 属性: index:指定参数在构造函数参数列表的索引位置 type:指定参数在构造函数中的数据类型 name:指定参数在构造函数中的名称 用这个找给谁赋值 =======上面三个都是找给谁赋值,下面两个指的是赋什么值的============== value:它能赋的值是基本数据类型和 String 类型 ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean --> <bean id="accountService" class="service.impl.AccountServiceImpl"> <constructor-arg name="name" value="张三"></constructor-arg> <constructor-arg name="age" value="18"></constructor-arg> <constructor-arg name="birthday" ref="now"></constructor-arg> </bean> <bean id="now" class="java.util.Date"></bean>
方式二:set方法注入
顾名思义,就是在类中提供需要注入成员的 set 方法。具体代码如下: public class AccountServiceImpl implements IAccountService { private String name; private Integer age; private Date birthday; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public void saveAccount() { System.out.println(name+","+age+","+birthday); } }
<!-- 通过配置文件给 bean 中的属性传值:使用 set 方法的方式 涉及的标签: property 属性: name:找的是类中 set 方法后面的部分 比如setName,则name的值就是name,如果为setUserName,则name的值就是userName ref:给属性赋值是其他 bean 类型的 value:给属性赋值是基本数据类型和 string 类型的 --> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="name" value="test"></property> <property name="age" value="21"></property> <property name="birthday" ref="now"></property> </bean> <bean id="now" class="java.util.Date"></bean>
方式三:使用p名称空间注入(本质还是调用的set方法)
此种方式是通过在 xml 中导入 p 名称空间,使用 p:propertyName 来注入数据,它的本质仍然是调用类中的 set 方法实现注入功能。 /** * 使用 p 名称空间注入,本质还是调用类中的 set 方法 */ public class AccountServiceImpl4 implements IAccountService { private String name; private Integer age; private Date birthday; public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public void saveAccount() { System.out.println(name+","+age+","+birthday); } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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"> <bean id="accountService" class="service.impl.AccountServiceImpl4" p:name="test" p:age="21" p:birthday-ref="now"/> </beans>
5)注入集合属性
顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。 我们这里介绍注入数组,List,Set,Map,Properties。具体代码如下: public class AccountServiceImpl implements IAccountService { private String[] myStrs; private List<String> myList; private Set<String> mySet; private Map<String,String> myMap; private Properties myProps; public void setMyStrs(String[] myStrs) { this.myStrs = myStrs; } public void setMyList(List<String> myList) { this.myList = myList; } public void setMySet(Set<String> mySet) { this.mySet = mySet; } public void setMyMap(Map<String, String> myMap) { this.myMap = myMap; } public void setMyProps(Properties myProps) { this.myProps = myProps; } @Override public void saveAccount() { System.out.println(Arrays.toString(myStrs)); System.out.println(myList); System.out.println(mySet); System.out.println(myMap); System.out.println(myProps); } }
<!-- 注入集合数据 List 结构的: array,list,set Map 结构的 map,entry,props,prop --> <bean id="accountService" class="service.impl.AccountServiceImpl"> <!-- 在注入集合数据时,只要结构相同,标签可以互换 --> <!-- 给数组注入数据 --> <property name="myStrs"> <set>/array/list <value>AAA</value> <value>BBB</value> <value>CCC</value> </set> </property> <!-- 注入 list 集合数据 --> <property name="myList"> <array>/set/list <value>AAA</value> <value>BBB</value> <value>CCC</value> </array> </property> <!-- 注入 set 集合数据 --> <property name="mySet"> <list>/array/set <value>AAA</value> <value>BBB</value> <value>CCC</value> </list> </property> <!-- 注入 Map 数据 --> <property name="myMap"> <props>/map <prop key="testA">aaa</prop> <prop key="testB">bbb</prop> </props> </property> <!-- 注入 properties 数据 --> <property name="myProps"> <map>/props <entry key="testA" value="aaa"></entry> <entry key="testB"> <value>bbb</value> </entry> </map> </property> </bean>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术