spring4(一)

1、Spring的概述

1-1 什么是Spring

enter description here
Spring:SE/EE开发的一站式框架。
 一站式框架:有EE开发的每一层解决方案。
  WEB层 :SpringMVC
  Service层 :Spring的Bean管理,Spring声明式事务
  DAO层 :Spring的Jdbc模板,Spring的ORM模块

1-2 Spring的版本

Spring3.x和Spring4.x

2、Spring的入门(IOC)

2-1 什么是IOC

IOC: Inversion of Control(控制反转)。
  控制反转:将对象的创建权反转给(交给)Spring。

2-2 下载Spring的开发包

官网:http://spring.io/
网盘:https://pan.baidu.com/s/1sbn1ZcKd0V1nz3ET-fQH1A

2-3 解压Spring的开发包

enter description here

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

2-4 创建web项目,引入jar包

enter description here
enter description here

2-5 传统方式存在的问题

问题:
  如果底层的实现切换了,需要修改源代码,能不能不修改程序源代码对程序进行扩展?
spring底层的原理
enter description here

2-6 将实现类交给Spring管理

1.导入步骤2-4的六个包https://pan.baidu.com/s/121K9cdnDaRzXrjacrpfyWA
2.在项目src下新建一个xml文件,建议命名为applicationContext.xml
3.在spring的解压路径下
spring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
将页面滑动至最低端,最后一个即为要引入的约束

扫描二维码关注公众号,回复: 11875705 查看本文章
<?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">
	<!-- spring入门的配置 -->
	<bean id="userService" class="com.qgc.spring.demo1.UserServiceImpl"></bean>
</beans>

2-7 编写测试类

public void demo1() {
    
    
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) applicationContext.getBean("userService");
		userService.save();
	}

2-8 IOC和DI

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

  • 依赖
    Class A{

}

Class B{
  public void xxx(A a){

}
}

  • 继承:is a
  • 聚合:has a
//下面的save方法需要依赖一个name的属性
public class UserServiceImpl implements UserService {
    
    
	private String name;
	public void setName(String name) {
    
    
		this.name = name;
	}
	@Override
	public void save() {
    
    
		System.out.println("业务层实现了。。。。"+name);
	}

}
<!-- 在spring的配置文件中进行配置即可-->
	<bean id="userService" class="com.qgc.spring.demo1.UserServiceImpl">
		<property name="name" value="林德德" />
	</bean>
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService userService = (UserService) applicationContext.getBean("userService");
		userService.save();

3、Spring的工厂类

3-1 Spring工厂类的结构图

enter description here
ApplicationContext继承BeanFactory。

3-2 BeanFactory :老版本的工厂类

BeanFactory:调用getBean的时候,才会生成类的实例。

3-3 ApplicationContext :新版本的工厂类

ApplicationContext:加载配置文件的时候,就会将Spring管理的类都实例化。
ApplicationContext有两个实现类
  ClassPathXmlApplicationContext :加载类路径下的配置文件
  FileSystemXmlApplicationContext :加载文件系统下的配置文件

4、Spring中bean的配置

4-1 <bean>标签的id和name的配置

id :使用了约束中的唯一约束。里面不能出现特殊字符的。
name :没有使用约束中的唯一约束(理论上可以出现重复的,但是实际开发不能出现的)。里面可以出现特殊字符。

4-2 Bean的生命周期的配置(了解)

init-method :Bean被初始化的时候执行的方法
destroy-method :Bean被销毁的时候执行的方法(Bean是单例创建,工厂关闭)

4-3 Bean的作用范围的配置(重点)

scope :Bean的作用范围
  singleton :默认的,Spring会采用单例模式创建这个对象。
  prototype :多例模式。(Struts2和Spring整合一定会用到)
  request :应用在web项目中,Spring创建这个类以后,将这个类存入到request范围中。
  session :应用在web项目中,Spring创建这个类以后,将这个类存入到session范围中。
  globalsession :应用在web项目中,必须在porlet环境下使用。但是如果没有这种环境,相对于session。

5、Spring的Bean管理(XML方式)

5-1 Spring的Bean的实例化方式(了解)

Bean已经都交给Spring管理,Spring创建这些类的时候,有几种方式:

5-1-1 无参构造方法的方式(默认)

  • 编写类
    enter description here
  • 编写配置
    enter description here

5-1-2 静态工厂实例化的方式

  • 编写Bean2的静态工厂
    enter description here
  • 配置
    enter description here

5-1-3 实例工厂实例化的方式

  • Bean3的实例工厂
    enter description here
  • 配置
    enter description here

5-2 Spring的属性注入

enter description here

5-2-1 构造方法的方式的属性注入

<!-- 使用构造方法进行属性注入-->
	<bean id="car" class="com.qgc.spring.demo3.Car">
		<constructor-arg name="name" value="凯迪拉克" />
		<constructor-arg name="price" value="250000" />
	</bean>

5-2-2 Set方法的方式的属性注入

  • Set方法属性注入
	<bean id="car2" class="com.qgc.spring.demo3.Car2">
		<property name="name" value="奥迪" />
		<property name="price" value="600000" />
	</bean>
  • Set 方法对象注入
    enter description here

5-2-3 P名称空间的属性注入(Spring2.5以后)

通过引入p名称空间完成属性的注入:
写法:
 普通属性 p:属性名=”值”
 对象属性 p:属性名-ref=”值”

  • p名称空间的引入
    将第一行的beans复制一行,末尾改为p,xmlns后加上:p即可
    enter description here
	<!-- 使用p名称空间的方式注入 -->
	<bean id="car2" class="com.qgc.spring.demo3.Car2" p:name="奇瑞qq" p:price="30000"></bean>
	<!-- p名称空间注入属性 -->
	<bean id="employee" class="com.qgc.spring.demo3.Employee" p:name="林德德" p:car-ref="car2"></bean>

5-2-4 SpEL的属性注入(Spring3.0以后)

SpEL:Spring Expression Language,Spring的表达式语言。
语法:
 #{SpEL}

	<!-- spEl可以调用方法 -->
	<bean id="carInfo" class="com.qgc.spring.demo3.carInfo"></bean>
	<bean id="car2" class="com.qgc.spring.demo3.Car2">
		<property name="name" value="#{carInfo.name}" />
		<property name="price" value="#{carInfo.price()}" />
	</bean>
		<bean id="employee" class="com.qgc.spring.demo3.Employee">
		<property name="name" value="#{
     
     '三德子'}"/>
		<property name="car" value="#{car2}"/>
	</bean>

5-3 集合类型的属性注入(了解)

<!-- 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>

6、Spring的分模块开发的配置

6-1 在加载配置文件的时候,加载多个

enter description here

6-2 在一个配置文件中引入多个配置文件

enter description here

猜你喜欢

转载自blog.csdn.net/m_awdawdw/article/details/103501223
今日推荐