案例的前期准备 本文使用的案例是账户的业务层和持久层的依赖关系解决。在开始 spring 的配置之前,我们要先准备一下环境。
由于我们是使用 spring 解决依赖关系,并不是真正的要做增删改查操作,所以此时我们没必要写实体类。
创建普通的 Maven 工程
导入 spring 依赖( pom.xml )
1 2 3 4 5 <dependency >     <groupId > org.springframework</groupId >      <artifactId > spring-context</artifactId >      <version > 5.2.12.RELEASE</version >  </dependency > 
创建业务层接口
1 2 3 4 5 6 7 public  interface  IAccountService  {            void  saveAccount () ; } 
创建业务层接口的实现类
1 2 3 4 5 6 7 8 9 10 public  class  AccountServiceImpl  implements  IAccountService  {              private  IAccountDao  accountDao  =  new  AccountDaoImpl ();          @Override      public  void  saveAccount ()  {         accountDao.saveAccount();     } } 
创建持久层接口
1 2 3 4 5 6 7 public  interface  IAccountDao  {         void  saveAccount () ; } 
创建持久层接口的实现类
1 2 3 4 5 6 7 public  class  AccountDaoImpl  implements  IAccountDao  {    @Override      public  void  saveAccount ()  {         System.out.println("保存成功.." );     } } 
基于 XML 的配置 在 resources 下 new 一个 xml
让 spring 管理资源,在配置文件中配置 service 和 dao
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?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" >              <bean  id ="accountService"  class ="cn.imzjw.service.impl.AccountServiceImpl" />           <bean  id ="accountDao"  class ="cn.imzjw.dao.impl.AccountDaoImpl" />  </beans > 
测试配置是否成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public  class  Client  {         public  static  void  main (String[] args)  {                  ApplicationContext  ac  =  new  ClassPathXmlApplicationContext ("bean.xml" );                  IAccountService  service  =  ac.getBean("accountService" , IAccountService.class);         System.out.println(service);         IAccountDao  dao  =  (IAccountDao) ac.getBean("accountDao" );         System.out.println(dao);     } } 
运行结果:
1 2 cn.imzjw.service.impl.AccountServiceImpl@5688764c cn.imzjw.dao.impl.AccountDaoImpl@3092bnnk 
Spring 基于 XML 的 IOC 细节 BeanFactory 才是 Spring 容器中的顶层接口。 ApplicationContext 只是它的子接口。
BeanFactory 和 ApplicationContext 的区别:
创建对象的时间点不一样。ApplicationContext:它在构建核心容器时,创建对象采取的策略是立即加载的方式,也就是说,只要一读取配置文件马上就创建配置文件中的配置的对象 BeanFactory:它在构建核心容器时,创建对象采取的策略是延迟加载的方式,也就是说,什么时候根据 id 获取对象,什么时候才真正的创建对象  ApplicationContext 接口的实现类 ClassPathXmlApplicationContext:它是从类的根路径下加载配置文件。推荐使用这种 
FileSystemXmlApplicationContext:它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
例如:
1 ApplicationContext  ac  =  new  FileSystemXmlApplicationContext ("C:\\Users\\garvey\\Desktop\\bean.xml" );
AnnotationConfigApplicationContext:当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。
bean 标签和管理对象 bean 标签:用于配置对象让 spring 来创建的。 默认情况下它调用的是类中的无参构造函数 。如果没有无参构造函数 则不能创建成功。1 2 <bean  id ="accountService"  class ="cn.imzjw.service.impl.AccountServiceImpl"         scope ="singleton"  init-method ="init"  destroy-method ="destroy" /> 
标签中的属性:
id:给对象在容器中提供一个唯一标识。用于获取对象。
class:指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
scope:指定对象的作用范围。
singleton:单例的,也是默认值 prototype:多例的 request:作用于 WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中 session:作用于 WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中 global-session:作用于集群环境的会话范围 (全局会话范围),当不是集群坏境时,它就是 session init-method:指定类中的初始化方法名称。
destroy-method:指定类中销毁方法名称。
bean 的生命周期 单例对象(singleton) 多例对象(prototype) 一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:出生:当应用加载,创建容器时,对象就被创建了。 活着:只要容器在,对象一直活着。 死亡:当应用卸载,销毁容器时,对象就被销毁了。  生命周期出生:当使用对象时,创建新的对象实例。 活着:只要对象在使用中,就一直活着。 死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。  
实例化 Bean 的三种方式 使用默认无参构造函数,在 spring 的配置文件中使用 bean 标签,配置 id 和 class 属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建 bean 对象,此时如果类中没有构造函数,则对象无法创建
1 <bean  id ="accountService"  class ="cn.imzjw.service.impl.AccountServiceImpl" /> 
使用实例工厂的方法创建对象 (使用某个类中的方法来创建对象,并存入 spring 容器)
首先创建一个工厂类(假设 该类是存在于 jar 包之中的,我们无法通过修改源码的方式来提供构造函数)
cn.imzjw.factory.InstanceFactory
1 2 3 4 5 6 7 8 9 10 public  class  InstanceFactory  {        public  IAccountService getAccountService ()  {         return  new  AccountServiceImpl ();     } } 
作用就是使用 InstanceFactory 类中的 getAccountService 方法来创建对象,并存入 spring 容器
此时 xml 就应该这样写
1 2 <bean  id ="instanceFactory"  class ="cn.imzjw.factory.InstanceFactory" /> <bean  id ="accountService"  factory-bean ="instanceFactory"  factory-method ="getAccountService" /> 
id:指定 bean 的 id,用于从容器中获取 class:指定实例工厂的全限定类名 factory-bean:用于指定实例工厂 bean 的 id factory-method:用于指定实例工厂中创建对象的方法 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入 spring 容器)
还是一样,创建个类,使用工厂中的静态方法来创建对象
cn.imzjw.factory.StaticFactory
1 2 3 4 5 6 public  class  StaticFactory  {    public  static  IAccountService getAccountService ()  {         return  new  AccountServiceImpl ();     } } 
xml
1 2 <bean  id ="accountService"  class ="cn.imzjw.factory.StaticFactory"         factory-method ="getAccountService" /> 
id:指定 bean 的 id,用于从容器中获取 class:指定静态工厂的全限定类名 factory-method:指定生产对象的静态方法 
spring 的依赖注入 依赖注入(Dependency Injection)
它是 spring 框架核心 IOC 的具体实现。 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。 IOC 解耦只是降低他们的依赖关系,但不会消除。
例如:我们的业务层仍会调用持久层的方法。 那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。 简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
构造函数注入 set 方法注入 p名称空间注入 集合属性注入 顾名思义,就是使用类中的构造函数,给成员变量赋值。
注意:赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public  class  AccountServiceImpl  implements  IAccountService  {         private  String name;     private  Integer age;     private  Date date;     public  AccountServiceImpl (String name, Integer age, Date date)  {         this .name = name;         this .age = age;         this .date = date;     }     @Override      public  void  saveAccount ()  {         System.out.println("name="  + name + ", age="  + age + ", date="  + date);     } } 
在xml中涉及的标签为constructor-arg,出现的位置在 bean 标签的内部
1 2 3 4 5 6 7 <bean  id ="accountService"  class ="com.itheima.service.impl.AccountServiceImpl" >     <constructor-arg  name ="name"  value ="张三" > </constructor-arg >      <constructor-arg  name ="age"  value ="18" > </constructor-arg >      <constructor-arg  name ="date"  ref ="now" > </constructor-arg >  </bean > <bean  id ="now"  class ="java.util.Date" > </bean > 
标签中的属性:
type:指定参数在构造函数中的数据类型 index:指定参数在构造函数参数列表的索引位置,参数索引的位置是从 0 开始 name:指定参数在构造函数中的名称 (更常用 )⭐ ============以上三个都是找给谁赋值的,下面两个指的是赋什么值的=================
它能赋的值是基本数据类型和 String 类型 用于指定其他的 bean 类型数据,指的就是在 spring 的 ioc 核心容器中出现过的 bean 对象 在main方法中测试运行
1 2 3 4 5 6 7 public  static  void  main (String[] args)  {    ApplicationContext  ac  =  new  ClassPathXmlApplicationContext ("bean.xml" );     IAccountService  service  =  ac.getBean("accountService" , IAccountService.class);     service.saveAccount(); } 
运行结果:
1 name=张三, age=18 , date=Mon Jan 18  18 :13 :00  CST 2021  
构造函数注入的优势与弊端:
优势:在获取 bean 对象时,注入数据是必须的,否则对象无法创建成功。
弊端:改变了 bean 对象的实例化方式,使我们在创建对象时如果用不到这些数据也必须提供。
顾名思义,就是在类中提供需要注入成员的 set 方法。
重新 new 一个类取名为 AccountServiceImpl2
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 public  class  AccountServiceImpl2  implements  IAccountService  {    private  String name;     private  Integer age;     private  Date date;     public  void  setName (String name)  {         this .name = name;     }     public  void  setAge (Integer age)  {         this .age = age;     }     public  void  setDate (Date date)  {         this .date = date;     }     @Override      public  void  saveAccount ()  {         System.out.println("name="  + name + ", age="  + age + ", date="  + date);     } } 
在xml中涉及的标签为property,出现的位置在 bean 标签的内部
1 2 3 4 5 6 7 <bean  id ="accountService2"  class ="cn.imzjw.service.impl.AccountServiceImpl2" >     <property  name ="name"  value ="李四" />      <property  name ="age"  value ="22" />      <property  name ="date"  ref ="now" />  </bean > <bean  id ="now"  class ="java.util.Date" /> 
标签中的属性:
name:用于指定注入时所调用的 set 方法名称 value:用于给属性赋值基本数据类型或者 String 类型 ref:用于指定其他的 bean 类型数据,指的就是在 spring 的 ioc 核心容器中出现过的 bean 对象 实际开发中,此方式用的较多。⭐
在main方法中测试运行
1 2 3 4 5 6 7 public  static  void  main (String[] args)  {    ApplicationContext  ac  =  new  ClassPathXmlApplicationContext ("bean.xml" );     IAccountService  service  =  ac.getBean("accountService2" , IAccountService.class);     service.saveAccount(); } 
运行结果:
1 name=李四, age=22 , date=Mon Jan 18  18 :25 :21  CST 2021  
优势:创建对象时没有明确的限制,可以直接使用构造函数。
弊端:如果某个成员必须有值,则获取对象有可能 set 方法没有执行。
此方式是通过在 xml 中导入 p 名称空间,使用 p:propertyName 来注入数据,它的本质仍然是调用类中的 set 方法实现注入功能。
new一个类取名为 AccountServiceImpl3
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public  class  AccountServiceImpl3  implements  IAccountService  {    private  String name;     private  Integer age;     private  Date date;     public  void  setName (String name)  {         this .name = name;     }     public  void  setAge (Integer age)  {         this .age = age;     }     public  void  setDate (Date date)  {         this .date = date;     }     @Override      public  void  saveAccount ()  {         System.out.println("name="  + name + ", age="  + age + ", date="  + date);     } } 
使用了p名称空间注入的方式需给配置文件导入约束
1 xmlns:p="http://www.springframework.org/schema/p" 
xml代码如下
1 2 3 4 5 6 7 8 9 10 11 12 <?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:p ="http://www.springframework.org/schema/p"         xsi:schemaLocation ="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd" >    <bean  id ="accountService3"  class ="cn.imzjw.service.impl.AccountServiceImpl3"              p:name ="王五"  p:age ="23"  p:date-ref ="now" />     <bean  id ="now"  class ="java.util.Date" />  </beans > 
在main方法中测试运行
1 2 3 4 5 6 7 public  static  void  main (String[] args)  {    ApplicationContext  ac  =  new  ClassPathXmlApplicationContext ("bean.xml" );     IAccountService  service  =  ac.getBean("accountService3" , IAccountService.class);     service.saveAccount(); } 
运行结果:
1 name=王五, age=23 , date=Mon Jan 18  18 :34 :52  CST 2021  
顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。
我们这里注入List、Set、Map、Properties
new一个类取名为 AccountServiceImpl4
具体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 public  class  AccountServiceImpl4  implements  IAccountService  {    private  String[] str;     private  List<String> list;     private  Set<String> set;     private  Map<String, String> map;     private  Properties prop;     public  void  setStr (String[] str)  {         this .str = str;     }     public  void  setList (List<String> list)  {         this .list = list;     }     public  void  setSet (Set<String> set)  {         this .set = set;     }     public  void  setMap (Map<String, String> map)  {         this .map = map;     }     public  void  setProp (Properties prop)  {         this .prop = prop;     }     @Override      public  void  saveAccount ()  {         System.out.println("str="  + Arrays.toString(str) + ", list="  + list + ", set="  + set + ", map="  + map + ", prop="  + prop);     } } 
xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <?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:p ="http://www.springframework.org/schema/p"         xsi:schemaLocation ="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.xsd" >    <bean  id ="accountService4"  class ="cn.imzjw.service.impl.AccountServiceImpl4" >                   <property  name ="str" >              <array >                  <value > 小嘉</value >                  <value > 是真的</value >                  <value > 帅啊</value >              </array >          </property >                   <property  name ="list" >              <list >                  <value > 我是</value >                  <value > list</value >              </list >          </property >                   <property  name ="set" >              <set >                  <value > 我是</value >                  <value > set</value >              </set >          </property >                   <property  name ="map" >              <map >                  <entry  key ="小嘉"  value ="是真的帅啊" />                                   <entry  key ="我真的喜欢" >                      <value > 小嘉</value >                  </entry >              </map >          </property >                   <property  name ="prop" >              <props >                  <prop  key ="小嘉" > 真的帅啊</prop >                  <prop  key ="我是" > Properties</prop >              </props >          </property >      </bean >  </beans > 
在注入集合数据时,只要结构相同,标签可以互换,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 <bean  id ="accountService3"  class ="cn.imzjw.service.impl.AccountServiceImpl3" >          <property  name ="str" >          <list >              <value > 小嘉</value >              <value > 是真的</value >              <value > 帅啊</value >          </list >      </property >           <property  name ="list" >          <array >              <value > 我是</value >              <value > list</value >          </array >      </property >           <property  name ="set" >          <list >              <value > 我是</value >              <value > set</value >          </list >      </property >           <property  name ="map" >          <props >              <prop  key ="小嘉" > 真的帅啊</prop >              <prop  key ="我是" > Properties</prop >          </props >      </property >           <property  name ="prop" >          <map >              <entry  key ="小嘉"  value ="是真的帅啊" />                           <entry  key ="我真的喜欢" >                  <value > 小嘉</value >              </entry >          </map >      </property >  </bean > 
在main方法中测试运行
1 2 3 4 5 6 7 public  static  void  main (String[] args)  {    ApplicationContext  ac  =  new  ClassPathXmlApplicationContext ("bean.xml" );     IAccountService  service  =  ac.getBean("accountService4" , IAccountService.class);     service.saveAccount(); } 
运行结果:
1 str=[小嘉, 是真的, 帅啊], list=[我是, list], set=[我是, set], map={我是=Properties, 小嘉=真的帅啊}, prop={我真的喜欢=小嘉, 小嘉=是真的帅啊}