一、简介
Spring 是一个开源框架.
Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.
Spring 是一个 IOC[Inversion of control](DI[Dependency Injection]) 和 AOP[Aspect Oriented Programming] 容器框架.
二、特点
- 轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
依赖注入(DI — dependency injection、IOC) - 面向切面编程(AOP — aspect oriented programming)
- 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
- 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
- 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的 SpringMVC 和 持久层的 Spring JDBC)
三、ioc/di
ioc(Inversion of Control,控制倒转)
这是spring的核心,贯穿始终。所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系。
di(Dependency Injection,依赖注入)
比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了spring我们就只需要告诉spring,A中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。
四、初始化spring容器
//具体文件系统的文件路径
ApplicationContext context = new FileSystemXmlApplicationContext("xxx.xml");
//相对路径class目录
ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml");
五、bean 的声明
-
在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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="名字" class="类型"> </bean> </beans>
-
使用注解
@Component:当对组件的层次难以定位的时候使用这个注解 @Controller:表示控制层的组件 @Service:表示业务逻辑层的组件 @Repository:表示数据访问层的组件
六、依赖注入的方式
-
在xml声明
-
基于setter注入
<bean id="xxx" class="com.xxx.pojo.Student" > <property name="name" value="张三"/> </bean> <bean id="t1" class="com.xxx.pojo.Teacher"> <property name="name" value="王老师"/> </bean> <bean id="t2" class="com.xxx.pojo.Teacher"> <property name="name" value="陈老师"/> </bean>
-
构造器注入
<bean id="l" class="com.baobaotao.anno.LogonService"> <constructor-arg ref="logDao"></constructor-arg> <constructor-arg ref="userDao"></constructor-arg> </bean>
-
基于工厂注入(现在已经不用)
-
-
使用注解
@Value(value=“xxx”)
七、Bean 的 Scope属性
Scope
--singleton 单例(默认)
--prototype 原型
八、自动注入
-
在需要自动注入的bean的声明autowire属性。
- byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
- byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.
-
使用注解自动注入
-
@Autowired 注解自动装配具有兼容类型的单个 Bean属性(根据类型注入,若需要根据名字注入@Qualifier(“xxx”) )
-
@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为 Bean 的名称
-
@Inject 和 @Autowired 注解一样也是按类型匹配注入的 Bean, 但没有 reqired 属性
建议使用 @Autowired 注解
-
九、集合注入
-
list注入
<!-- 给list注入值 可以有相同的多个对象 --> <property name="xxxx"> <list> <ref bean="xxx" /> <ref bean="xxx"/> </list> </property>
-
set注入
<!-- 给set注入值 不能有相同的对象 --> <property name="xxxx"> <set> <ref bean="xxx" /> <ref bean="xxx"/> </set> </property>
-
map注入
<!-- 给map注入值 只要map中的key值不一样就可以装配value --> <property name="xxxx"> <map> <entry key="key" value-ref="value" /> <entry key="key" value-ref="value" /> </map> </property>
十、扫描包
-
添加xml 验证
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
- 在beans标签加上
<context:component-scan base-package=“xxxx” />
- 在beans标签加上
加上包扫描后,我们可以在扫描的包上使用spring 注解