目录
IOC:控制反转
:将对象的创建权交给Spring,主要是为了解耦合,代码模块之间的耦合度太高,一旦添加模块,或者修改代码,则牵扯的代码太多,工作繁重,不利用代码的维护。
:DI:依赖注入,前提必须有IOC的环境,Spring管理创建这个类的时候将类的依赖的属性注入(设置)进来。
工厂类
BeanFactory:老版本的工厂类
BeanFactory:调用getBean的时候,才会生成类的实例。
ApplicationContext:新版本的工厂类
:加载配置文件的时候,就会将spring管理的类都实例化
ApplicationContext的两个实现类:
ClassPathXmlApplicationContext:加载类路径下的配置文件
FileSystemXmlApplicationContext :加载文件系统下的配置文件
Bean的作用范围的配置Scope
- singleton :默认的,Spring会采用单例模式创建这个对象。
- prototype :多例模式。(Spring整合一定会用到)
- request :应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中。
- session :应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中。
- globalsession :应用在web项目中,必须在porlet环境下使用。但是如果没有这种环境,相对于session。
配置属性注入
构造方法配置
此时类里使用的是有参构造函数,name="类里的属性name/price"
<!--构造方法配置属性注入-->
<bean id="car" class="com.service.spring.Car">
<constructor-arg name="name" value="宝马"/>
<constructor-arg name="price" value="200"/>
</bean>
方法测试 :getBean("对应的是bean标签里的id/name")
@Test
//构造方法注入属性
public void test(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
}
set方法配置
-----为什么要引入set方法,因为要注入对象属性,构造方法办不到
此时类里写的是set方法
普通属性的注入,除了<property>标签换了以外,没有不同
<!--set方法配置属性注入,因为set可以注入对象属性-->
<bean name="car" class="com.service.spring.Car">
<property name="name" value="宝马"></property>
<property name="price" value="200"/>
</bean>
对象属性的注入:ref是指向已经配置好且你需要注入的对象的<bean id/name>
<bean id="duixiang" class="com.service.spring.duixiang">
<property name="name" value="绽放"/>
//ref指向Car这类的bean的id
<property name="car" ref="car"/>
</bean>
P名称空间的属性注入(Spring2.5以后)
- 通过引入p名称空间完成属性的注入:
- 写法:
- 普通属性 p:属性名=”值”
- 对象属性 p:属性名-ref=”值”
- 写法:
p名称空间的引入
xmlns:p="http://www.springframework.org/schema/p"
使用p名空间
<!--p名称空间-->
//引入car的两个属性
<bean name="car" class="com.service.spring.Car" p:name="白马" p:price="200"/>
//引入duixiang的一个普通属性,一个对象属性
<bean name="duixiang" class="com.service.spring.duixiang" p:name="往东" p:car-ref="car"/>
SpEL的属性注入(Spring3.0以后)
<!--Spel的属性注入-->
<bean name="carinfo" class="com.service.spring.CarInfo"></bean>
<bean name="car" class="com.service.spring.Car">
//carinfo的name属性的值赋值过来
<property name="name" value="#{carinfo.name}"/>
<property name="price" value="#{200}"/>
</bean>
<bean name="duixiang" class="com.service.spring.duixiang">
<property name="name" value="#{'对象'}"/>
//duixiang类里创建了private Car car;这里是将<bean id/name="car">的car对象付给这个私有对象(包括属性name/price)
<property name="car" value="#{car}"/>
</bean>
集合类型的注入
数组,List,Set,Map
<!-- Spring的集合属性的注入============================ -->
<!-- 注入数组类型 -->
<bean id="collectionBean" class="com.itheima.spring.demo5.CollectionBean">
<!-- 数组类型 -->
<property name="arrs">
<list>
<value>王东</value>
<value>赵洪</value>
<value>李冠希</value>
</list>
</property>
<!-- 注入list集合 -->
<property name="list">
<list>
<value>李兵</value>
<value>赵如何</value>
<value>邓凤</value>
</list>
</property>
<!-- 注入set集合 -->
<property name="set">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<!-- 注入Map集合 -->
<property name="map">
<map>
<entry key="aaa" value="111"/>
<entry key="bbb" value="222"/>
<entry key="ccc" value="333"/>
</map>
</property>
</bean>
分模块配置
在加载配置文件的时候,加载多个
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config1.xml","spring-config2.xml");
在配置文件中导入另一个配置文件
<import resource="spring-config2.xml"/>