spring框架IOC实现方式——纯xml方式:bean信息定义全部配置在xml中。
 1、引入IOC依赖:
<!--引入Spring IoC容器功能-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.12.RELEASE</version>
        </dependency>
 
2、配置applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--跟标签beans,里面配置一个又一个的bean子标签,每一个bean子标签都代表一个类的配置-->
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        
    <bean id="accountDao" class="com.spStudy.dao.impl.JdbcAccountDaoImpl">
        <property name="ConnectionUtils" ref="connectionUtils"></property>
    </bean>
</beans>
 
3、JavaSE应用:
@Test
public void testIoC() throws Exception {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    // 不推荐使用
   //ApplicationContext applicationContext1 = new FileSystemXmlApplicationContext("文件系统的绝对路径");
    AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao");
    System.out.println(accountDao);
}
 
4、JavaWeb应用:
 使用ContextLoaderListener 监听器加载xml
 web.xml
<!--配置Spring ioc容器的配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!--使用监听器启动Spring的IOC容器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
servlet使用:
@Override
public void init() throws ServletException {
    WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    AccountDao accountDao= (AccountDao ) webApplicationContext.getBean("accountDao");
}
 
5、注:
 – 实例化Bean的三种方式
<!--  Spring ioc 实例化Bean的三种方式  -->
<!--  方式一:使用无参构造器(推荐)  -->
<bean id="connectionUtils" class="com.mls.spStudy.utils.ConnectionUtils"></bean>
<!--  方式二、三一般用来将我们自己创建的对象加入到Spring IOC 容器管理 -->
<!--  方式二:静态方法  -->
<bean id="connectionUtils" class="com.mls.spStudy.factory.CreatBeanFactory" factory-method="getInstanceStatic"></bean>
<!--  方式三:实例化方法  -->
<bean id="creatBeanFactory" class="com.mls.spStudy.factory.CreatBeanFactory"></bean>
<bean id="connectionUtils" class="com.mls.spStudy.factory.CreatBeanFactory" factory-bean="creatBeanFactory" factory-method="getInstance"></bean>
 
– bean标签常用属性:
 1)id:唯一标识,标识对象。
 2)class:类的全限定类名。
 3)factory-bean属性:⽤于指定创建当前bean对象的⼯⼚bean的唯⼀标识。当指定了此属性之后,class属性失效。
 4)factory-method属性:⽤于指定创建当前bean对象的⼯⼚⽅法,如配合factory-bean属性使⽤, 则class属性失效。如配合class属性使⽤,则⽅法必须是static的。
 5)init-method属性:⽤于指定bean对象的初始化⽅法,此⽅法会在bean对象装配后调⽤。必须是⼀个⽆参⽅法。
 6)destory-method属性:⽤于指定bean对象的销毁⽅法,此⽅法会在bean对象销毁前执⾏。它只 能为scope是singleton时起作⽤。
 7)scope:定义bean的作用范围
- singleton:单例,IOC容器中只有一个该对象,是默认选项。生命周期:当创建容器时,对象就被创建了(对象出⽣);只要容器在,对象⼀直活着(对象活着);当销毁容器时,对象就被销毁了(对象死亡)。即单例模式的bean对象⽣命周期与容器相同。
 - prototype:原型(多例),每次使用该类的对象,都会返回一个新的对象。生命周期:当使⽤对象时,创建新的对象实例(对象出⽣);只要对象在使⽤中,就⼀直活着(对象活着)。当对象⻓时间不⽤时,被java的垃圾回收器回收了(对象死亡)。多例模式的bean对象,spring框架只负责创建,不负责销毁。
 
– DI依赖注入的xml配置
 依赖注入分类
- 按照注入方式分类:
 
- 构造函数注入:利用构造函数实现对类成员的赋值
 
 <!--name:按照参数名称注入,index按照参数索引位置注入-->
<constructor-arg name="connectionUtils" ref="connectionUtils"/>
<constructor-arg name="name" value="zhangsan"/>
<constructor-arg name="sex" value="1"/>
<constructor-arg name="money" value="100.6"/>
 
- set方法注入:通过set方法实现注入(使用较多)
 
<!--set注入使用property标签,如果注入的是另外一个bean那么使用ref属性,如果注入的是普通值那么使用的是value属性-->
<property name="ConnectionUtils" ref="connectionUtils"/>
<property name="name" value="zhangsan"/>
<property name="sex" value="1"/>
<property name="money" value="100.3"/>
 
- 按照注入的数据类型分类
 
- 基本类型和string:注⼊的数据类型是基本类型或者是字符串类型的数据。
 
<property name="name" value="zhangsan"/>
<property name="sex" value="1"/>
<property name="money" value="100.3"/>
 
- 其他对象类型:注⼊的数据类型是对象类型
 
<property name="ConnectionUtils" ref="connectionUtils"/>
 
- 复杂类型(集合类型):注⼊的数据类型是Aarry,List,Set,Map,Properties中的⼀种类型
 
<property name="myArray">
    <array>
        <value>array1</value>
        <value>array2</value>
        <value>array3</value>
    </array>
</property>
<property name="myMap">
    <map>
        <entry key="key1" value="value1"/>
        <entry key="key2" value="value2"/>
    </map>
</property>
<property name="mySet">
    <set>
        <value>set1</value>
        <value>set2</value>
    </set>
</property>
<property name="myProperties">
    <props>
        <prop key="prop1">value1</prop>
        <prop key="prop2">value2</prop>
    </props>
</property>










