Spring入门及IOC的XML配置

Spring概述

  • 什么是Spring
    Spring是一个开源框架,Spring是于2003年兴起的一个轻量级的java开发框架

IOC(控制反转)

  • 什么是IOC
    Inversion of Control (控制反转),将对象的创建权反转给(交给)Spring

开发包

docs:Spring的开发规范和API
libs:Spring的开发的jar和源码
schema:Spring的配置文件的约束

编写xml管理实现类

applicationContext.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>

IOC和DI

IOC:控制反转:将对象的创建权反转给Spring
DI:依赖注入,前提必须有IOC的环境,Spring管理这个类的时候将类的依赖的属性注入(设置)进来

Spring的工厂类

  • BeanFactory:老版本的工厂类
    调用getBean 的时候才会生成类的实例
  • ApplicationContext:新版本的工厂类
    继承了BeanFactory
    1. 加载配置文件的时候,就会将Spring管理的类都实例化
    2. 有两个实现类:
      ClassPathXmlApplicationContext:加载类路径下的配置文件
      FIleSystemXmlApplicationContext:加载文件系统下的配置文件

Spring的配置

  • bean 标签的id和name的配置
  1. id:使用了约束中的唯一约束,里面不能出现特殊字符
  2. name:没有使用约束中的唯一约束(理论上可以出现重复,但是实际开发不能出现),里面可以出现特殊字符
    Spring和Struts1框架整合的时候,会用name在前面加/
  • Bean的生命周期的配置
  1. init-method:Bean被初始化的时候执行的方法
  2. Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭时)
  • Bean的作用范围的配置(重点)
    scope:Bean的作用范围
    1. singleton:默认的,Spring会采用单例模式创建这个对象
    2. prototype:多例模式,(Struts2和Spring整合的时候会用)
    3. request:应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中
    4. session:应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中
    5. globalsession :应用在web项目中,必须在porlet环境下使用。但是如果没有这种环境,相对于session

Spring属性注入

  • 构造方法的属性注入
    对象类型的属性,把value替换为ref
<!-- 构造方法的方式 -->
	<bean id="car" class="com.itheima.spring.demo4.Car">
		<constructor-arg name="name" value="宝马"/>
		<constructor-arg name="price" value="800000"/>
	</bean>
  • Set方法的属性注入
    对象类型的属性,把value替换为ref
<!-- set方法的方式 -->
<bean id="car2" class="com.itheima.spring.demo4.Car2">
		<property name="name" value="奔驰"/>
		<property name="price" value="1000000"/>
	</bean>
  • P名称空间的属性注入(Spring2.5以后)
    配置xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
        
        
</beans>

使用:

<!-- 改为p名称空间的方式 -->
 <bean id="car2" class="com.itheima.spring.demo4.Car2" p:name="奇瑞QQ" p:price="30000"></bean>
	
	<bean id="employee" class="com.itheima.spring.demo4.Employee" p:name="王东" p:car2-ref="car2"></bean> 
  • SqEL的属性注入(Spring3.0以后)
<!-- SpEL的属性注入 -->
	<bean id="carInfo" class="com.itheima.spring.demo4.CarInfo">
	</bean>
	
	<bean id="car2" class="com.itheima.spring.demo4.Car2">
		<property name="name" value="#{carInfo.name}"></property>
		<property name="price" value="#{carInfo.calculatorPrice()}"></property>
	</bean>
	
	<bean id="employee" class="com.itheima.spring.demo4.Employee">
		<property name="name" value="#{'赵洪'}"></property>
		<property name="car2" value="#{car2}"></property>
	</bean>

Spring的分模块开发的配置

  1. 在加载配置文件的时候,加载多个
ApplicationContext applicationContext =
 new ClassPathApplicationContext("applicationContext.xml"
 ,"applicationContext2.xml");

  1. 在一个配置文件中引入多个配置文件
<import resource="appplicationContext2.xml" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值