Spring-IOC
spring-ico(控制反转) :把对象的创建,初始化,销毁等工作交给Spring容器来执行
由Spring容器控制对象的生命周期
对象的创建的过程:
1、写一个java类
2、写一个spring的配置文件,把该类放入到spring容器中
< bean id=“helloWorld” class=“类的路径”></ bean>
3、启动spring容器
4、把对象从spring容器中取出来
5、对象调用方法
说明:bean中的id值是唯一的,不能出现相同的ID值
spring的scope
-
spring创建对象默认是单例模式
-
如果把一个list声明在该bean的属性中,这样list成了共享的数据,所以一般情况下把list声明在方法中
-
把spring创建的bean改成prototype模式,只需要写一个属性:scope为prototype
spring什么时候创建对象
1、在默认情况下,spring容器启动的时候,就创建对象了
有利于开发:
当spring容器启动的时候,如果配置文件中有错误,则会报错
2、当从spring容器中获取bean,当bean调用方法的时候创建对象
如果所有的bean按照这种模式加载,当spring容器启动的时候是不会报错的
如果一个bean中有大量的数据,需要的是用到的情况下加载
spring容器的初始化和销毁:
前提条件:spring中的bean是单例的
1、在一个bean中可以提供init方法和destroy方法
2、当创建对象完成以后,直接执行init方法,该方法由spring容器来执行
3、当spring容器关闭的时候,执行destroy方法,该方法由spring容器来执行,由该方法释放资源
说明:
如果spring中的bean是多例的情况下,spring容器不负责对象的销毁,由程序员把该对象设置为null
如果spring中的bean是多例,这个时候,不管在配置文件中lazy-init设置成什么样的值
在context.getBean时才要创建对象,而且不负责销毁
applicationContext.XML ----(Spring容器)
<?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">
<!--将javabean放入spring容器中管理
spring创建对象默认是单例设计模式-->
<!--将javabean放入spring容器中 构造器实例化-->
<bean id="test" class="IOC.dao.TestDaoImpl" >
<property name="string" value="555"/>
</bean>
<!--别名-->
<alias name="test" alias="good"/>
<!--静态工厂实例化 : 从指定工厂类中的调用静态方法创建对象
引入工厂类,类中定义静态方法getInstance,该方法返回new TestDaoImpl;那这个bean就是TestDaoImpl对象
-->
<bean id="testFactory" class="IOC.dao.TestFactory" factory-method="getInstance"/>
<!--实例工厂实例化 : 引入工厂bean 调用非静态方法创建对象 -->
<bean id="void" factory-bean="testFactory" factory-method="getInstance1"/>
*******************************************************************
客户端的调用
启动spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("IOC/test/applicationContext.xml");
把对象从spring容器取出来
TestDaoImpl t2 = (TestDaoImpl) context.getBean("test");
t2.sayHello();
spring的DI:依赖注入
原理:
1、启动spring容器
2、spring容器为person创建对象
3、利用property中的name属性可以把set方法拼接出来
4、利用java的反射机制,给属性赋值
说明:在默认的情况下,第3、4步在spring容器启动的时候完成了,把这样的过程叫做bean的装配
public class Person{
private Long id;
private String name;
private Student student;
private Set sets;
private List lists;
private Map map;
private Properties properties;
}
<bean id="student" class="IOC.dao.Student"/>
<bean id="person" class="IOC.dao.Person" scope="singleton">
<!--基本属性赋值-->
<property name="name" value="网二"/>
<property name="id" value="222"/>
<!--引用属性-->
<property name="student" ref="student"/>
<!--集合属性赋值-->
<property name="set">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="list">
<set>
<value>1</value>
<value>2</value>
<value>3</value>
</set>
</property>
<property name="map">
<map>
<entry key="key1" value="ma111"/>
<entry key="key2" value-ref="student"/>
<entry key="key3" value="ma111"/>
</map>
</property>
<property name="properties">
<props>
<prop key="p111">倒计时卡鹿鼎记上课啦</prop>
<prop key="p22">倒而我却记上课啦</prop>
</props>
</property>
</bean>
**************************************
<!--构造器赋值-->
<bean id="person1" class="IOC.dao.Person" scope="prototype">
<constructor-arg index="0" value="mazi"/>
<constructor-arg index="1" ref="student1"/>
</bean>
<bean id="student1" class="IOC.dao.Student"/>