spring学习前置
maven
项目结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PckDwXFo-1598319869073)(E:\学习之路\初级阶段\截图\截图.png)]
main
java:java项目代码文件
resources:资源库
webapp:JSP、CSS、JS
WEB-INF:web.XML
test:测试
target:存放项目构建后的文件和目录,maven构建的时候生成的
工厂模式解耦
1、需要一个配置文件来配置service和到dao
配置内容:唯一标识------限制类名(key-value)
2、通过读取配置文件的配置内容、反射创建对象
单例对象需创建一个Map,用于存放所要创建的对象,称之为容器。
引入Spring框架(maven项目为例):
pom.XML中引入Spring依赖。
一 IOC
将之前创建对象的“new”方法,改为factory.getBean(“bean的名称”);相当于通过一个中间商获取资源。目的是削减耦合。
maven 项目中在resources文件夹中创建XML文件-添加bean的key-value对—id=" XXX" class=“XXX”
<?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="helloImpl" class="com.lijie.Dao.Impl.helloImpl"></bean>
<bean id="helloSpringImpl" class="com.lijie.Service.Impl.helloSpringImpl"></bean>
</beans>
在创建对象时,第一步,获取核心容器对象,并通过ID值获取Bean对象。
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");//定义接口,new实现类
//第二步,通过ID获取Bean对象
IhelloSpring helloSpringImpl=(IhelloSpring) ac.getBean("helloSpringImpl");//类型强转
Ihello helloImpl=ac.getBean("helloImpl",Ihello.class);//定义返回类型:类型.class
1、ApplicationContext的三个常用类:
- ClassPathXmlApplicationContext:加载类路径下的配置文件,要求配置文件必须在类路径下,否则无法实现
- FileSystemXmlApplicationContext:可以加载磁盘任意路径 下的配置文件(有访问权限)
- AnnotationConfigApplicationContext:用于读取注解创建容器
2、核心容器的两个接口(ApplicationContext和BeanFactory)引出的问题
BeanFactory是Spring容器的顶层接口-----------ApplicationContext是它的子接口
ApplicationContext:(采用此接口开发)
它在构建核心容器时,创建采取对象的策略是采用立即加载的方式,也就是读完配置文件马上创建配置文件中的对象。-----------------单例对象适用
BeanFactory:
构件核心容器时,创建采取 对象的策略是采用延迟加载的方式,就是说什么时候根据ID获取对象,什么时候创建对象。------------多列对象适用
3、Spring对bean的管理细节:
-
创建bean的三种方式
a 创建默认构造函数--------配置文件中,bean标签只配以class和 id属性。采用bean默认构造函数创建bean对象。
< bean id="helloImpl" class="com.lijie.Dao.Impl.helloImpl">< /bean>
b 使用普通工厂中的方法创建对象(无默认构造函数,且我们无法修改获得默认构造函数:如jar包中的bean)
其中ifactory是一个工厂类,包含了一个返回bean(helloImpl)的函数。获取helloImpl函数如下:
<bean id="ifactory" class="com.lijie.factory.Ifactory"></bean> <bean id="helloImpl" factory-bean="ifactory" factory-method="gethelloImpl"></bean>
c 使用工厂中的静态方法创建对象(针对于类中的静态方法创建对象,存入Spring容器中)
<bean id="helloImpl" class="com.lijie.factory.StaticFactory" factory- method="gethelloImpl"></bean>
-
bean对象的作用范围
bean标签的scope属性
作用:指定bean的作用范围(能不能取到这个bean对象)
取值:常用单例或多例
singleton:单例(默认值)
prototype:多列的
request:作用与web应用请求范围
session:作用与web应用会话范围
global-session:全局会话范围(全局session,理解为多台服务器共有的session)
-
bean对象的生命周期
单例:
出生:当容器创建时,对象出生
死亡:容器销毁,对象死亡
多例:
出生:调用ID获取对象时,对象出生
死亡:当对象长时间不用,由Java垃圾回收器回收。
4、Spring依赖注入
说明:Spring由核心容器IOC降低程序的耦合度(依赖关系),对依赖关系的管理就叫做依赖注入。理解为数据在配置时初始化初值,一般用于数据内容不常修改的情况。如数据库密码等。
注入语法:使用construtor-arg标签
标签位置:bean标签内部
标签中的属性:
type: 用于指定要注入的数据的类型,且该类型是构造函数中某个/些参数类型
index: 用于指定要注入的数据类型
name:用于指定给构造函数中指定名称的参数赋值
======以上用于构造函数中参数赋值
value:用于提供基本类型和String类型的数据
ref: 用于指定其他bean类型数据(在ioc中配置过的bean)
三类注入数据:
a 基本类型和String
b 在配置文件中配置过的bean类型
c 复杂类型/集合类型
用于list结构集合注入的标签
list | array | set
用于Map结构集合注入的标签
map | props
<!--使用set方法给复杂类型注入-->
<bean id="student2" class="com.lijie.factory.student2">
<property name="mylist">
<list>
<value>AAA</value>
<value>BBb</value>
<value>CCC</value>
</list>
</property>
<property name="myStr">
<list>
<value>AAA</value>
<value>BBb</value>
<value>CCC</value>
</list>
</property>
<property name="myMap">
<map>
<entry key="1" value="李杰"></entry>
<entry key="2" value="张色小"></entry>
</map>
</property>
</bean>
三种注入方式:
第一种:使用构造函数提供
<!--student类中无默认构造函数 构造函数为name+age+brithday,需将类成员初始化后才能创建-->
<bean id="student" class="com.lijie.factory.student">
<constructor-arg name="name" value="lijie"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="brithday" value="2019/1/1"></constructor-arg>
</bean>
第二种:使用set方法提供 更常用
前提:bean对象中要存在set方法。 关键标签property
<bean id="student1" class="com.lijie.factory.student1">
<property name="age" value="18"></property>
<property name="name" value="李杰"></property>
<property name="brithday" value="2018/4/4"></property>
</bean>
第三种:使用注解方法提供
见下章