Spring
spirng相关技术
- Spring data
- Spring boot
- Spring framework
- Spring social
IOC控制反转:(DI:依赖注入)
搭建Spring环境
-
下载jar 网址:jar包地址
下载之后是一个 压缩包:spring-framework-4.3.9.RELEASE-dist.zip -
在libs目录下,引入(4+1)个jar包
spring-aop.jar 开发AOP特性时需要的jar包
spring-beans.jar 处理Bean的jar
spring-context.jar 处理spring上下文的jar
spring-core.jar spring核心jar
spring-expression.jar spring表达式
三方提供的jar日志:
commons-logging.jar 日志 -
编写配置文件
a. 为了编写时生成一些配置信息,可以给eclipse增加一些支持spring的插件:spring tool suite或在eclipse上面安装插件
方式一:直接给eclipse增加支持spring的插件:spring tool suite
步骤:下载springsource-tool-suite-版本.RELEASE-e版本-updatesite.zip,然后在eclipse中安装,Help-install new softwore
方式二:直接下载sts工具(相当于一个集合Spring tool suite的eclipse),下载地址:stsb. 新建 bean configuration… - applicationContext.xml
-
开发Spring程序(IOC)
//创建一个spring上下文对象:context
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//执行SpringIOC容器中获取id为student的对象
Student student = (Student)context.getBean("student");
SpringIOC(控制反转与依赖注入)
一般方式创建一个对象
- 之前Java中获取一个对象需要 Student student = new Student(); student.setXxx();
- 简单工厂
- IOC(超级工厂)
SpringIOC(控制反转):依赖注入
控制反转:将创建对象、属性值 的方式 进行反转,从new、setXxx() 反转为了从springIOC容器getBean()
依赖注入:将属性值 注入给了属性,将属性注入给了bean,将bean注入给了ioc容器(就相当于配置文件applicationContext.xml)
总结:IOC/DI.无论是要什么对象,都可以直接去springIOC容器中获取,而不需要自己创建一个(new/setXxx())
因此之后的ioc分为两步:
- 先给springioc中存放对象并赋值
- 直接拿
IOC容器赋值:
- 如果是简单类型(8个基本+String,value);
- 如果是对象类型,ref=“需要引用的id值”,因此实现了对象之间的依赖关系
依赖注入的3种方法:
- 通过
set
方法注入<property name="name" value="zs"></property>
- 通过构造方法注入
<!-- 通过构造方法注入 -->
<!-- 构造方法中的顺序可以用index来索引赋值 -->
<constructor-arg value="c++" type ="String" index="0" name="Cname"></constructor-arg>
<constructor-arg value="100"></constructor-arg>
<!-- 将teacher1对象注入到course中 -->
<constructor-arg ref="teacher"></constructor-arg>
需要注意:如果<constructor-arg>
的顺序与构造方法参数的顺序不一致,则需要通过type或者index或name
指定,一般用name
指定
- p命名空间
<beans>
xmlns:p="http://www.springframework.org/schema/p"
<!-- 通过 p赋值 -->
<bean id="course" class="org.st.entity.Course" p:cname="java" p:courseHour="200" p:teacher-ref="ls">
</beans>
- 简单类型:
p:属性名=“属性值” - 引用类型(除了String外)
p:属性名-ref-“引用的id”
注意多个p赋值的时候 要有空格
注意:
无论是String还是int/short/long等类型,在赋值时,都是value=“值”
因此建议 此种情况配合name/type进行区分
例:注入各种集合类型: List Set map properties
- set方法
<bean id="collectiobDmo" class="org.st.entity.AllCollectionType">
<property name="listElement">
<list>
<!-- 属性赋值 -->
<value>足球</value>
<value>篮球</value>
</list>
</property>
<property name="arrayElement">
<array>
<value>苹果</value>
<value>香蕉</value>
</array>
</property>
<property name="setElement">
<set>
<value>白桦</value>
<value>松榛</value>
</set>
</property>
<property name="mapElement">
<map>
<entry>
<key><value>foot</value></key>
<value>足球</value>
</entry>
<entry>
<key><value>pp</value></key>
<value>兵兵球</value>
</entry>
</map>
</property>
<property name="propsElement">
<props>
<prop key="foot1">足球1</prop>
<prop key="foot2">足球2</prop>
</props>
</property>
</bean>
- 构造赋值(相似)
注意:
赋空值null:直接写
<property name="name">
<null/>
</property>
赋值 “ ”
<property name="name">
<value></value>
</property>
在ioc中定义bean的前提:该bean的类 必须提供无参构造
优于自动装配(只适合于ref类型):约定优于配置
<bean id="course" class="org.st.entity.Course" autowire ="byName">
autowire ="byId"
:Course类中有一个属性teacher(属性名),并且该ioc容器中恰好有一个bean的id: bean的id值 = 类得属性名
byName:自动寻找:其他bean的id值=该Course类的属性名
byType:其他bean类型(class) 是否与该Course的ref属性类型一致(注意:此种方式 必须满足:当前容器中只能有一个bean满足条件)
constructor:其他bean的类型(class)是否与 该Course类的构造方法参数的类型一致,此种方式的本质是byName
可以在头文件中 一次性将该ioc容器的所有bean 同一设置成自动装配:<beans ... default-autowire="byName">
自动装配虽然可以减少代码量,但是会降低代码的可读性