spring概要

spring_day01

1.spring介绍
spring并不局限于某一层.
spring是对象的容器,帮我们"管理"项目中的所有对象

2.spring搭建
1>导包 4+2
2>准备类
3>书写配置(src/applicationContext.xml)
4>书写代码测试

3.spring中的概念
ioc: 反转控制. 创建对象的方式反转了.从我们自己创建对象,反转给spring(程序)来创建.

di: 依赖注入.将必须的属性注入到对象当中.是实现ioc思想必须条件.

beanFactory与ApplicationContext

4.配置文件详解

bean元素
id: 给Bean起个名字 不能重复,不能使用特殊字符.早期属性.
name:给Bean起个名字 能重复,能使用特殊字符.后来属性.
class:类的完整类名
生命周期属性
init-method 指出初始化方法
destory-method 指出销毁方法
作用范围
scope:
singleton(默认值):单例.创建容器时会立即创建单例对象
prototype :多例.每次获得对象时,才会创建对象,并且每次都会创建新的对象
分模块开发
<import />

5.Bean的创建方式
*空参构造创建
静态工厂
实例工厂

6.注入方式
*set方法
*构造方法
p名称空间
spEL表达式

7.复杂属性注入
Array
List
Map
Properties
8.在WEB环境中使用Spring容器
1>导包 4+2+1(spring-web)
2>在web.xml中配置listener => ContextLoaderListener
|-配置参数,指定spring配置路径
3>在Action中,使用工具类获得容器.
|-WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)


-----------

复习

一.注解代替xml配置
准备工作:
4+2 + spring-aop包
xml中导入context约束
在xml中开启扫描包中类的注解
注解:
@Component("BeanName") 将对象注册到spring容器
|- @Controler
|- @Service
|- @Repository

@Scope 指定对象的作用范围
|- singleton
|- prototype

@Value 值类型属性注入

@Autowired 自动属性注入.根据类型注入.
@Qulifier 指定注入的对象的名称

@Resource 指定对象的名称注入

@PostConstruct 初始化方法
@PreDestory 销毁方法

二.spring AOP开发

aop思想: 纵向重复,横向抽取.
|- filter中
|- 动态代理
|- interceptor中

spring AOP: 封装了动态代理技术.来体现aop.


springaop实现: 可以对所有对象进行代理
|- 动态代理 代理需要实现接口.
|- cglib代理 对目标对象继承代理.

springaop名词:
join point: 连接点.所有可以织入通知的方法.
point cut : 切入点.需要|已经织入通知的方法.
advice: 需要增强的代码.
weaving: 动词.将通知应用的切点的过程.
target: 目标对象.
proxy: 代理对象
aspect: 切面. 切入点+通知

步骤:
1.导包
4+2
2 aop+aspect
2 aop联盟+weaving
2.准备目标对象

3.准备通知类
前置通知
后置通知 方法正常结束
环绕通知
异常拦截通知
后置通知 无论如何都执行

4.配置文件中配置,导入aop约束
1>目标对象
2>通知对象
3><aop:config>
<aop:ponint-cut id="切点名称" expression="execution(切点表达式)" />
<aop:aspect ref="通知对象名称" >
<aop:before method="" ponintcut-ref="" />
<aop:after-returning method="" ponintcut-ref="" />
<aop:around method="" ponintcut-ref="" />
<aop:after-throwing method="" ponintcut-ref="" />
<aop:after method="" ponintcut-ref="" />
</aop:aspect>

扩展:使用注解完成aop
1.导包
4+2
2 aop+aspect
2 aop联盟+weaving
2.准备目标对象

3.准备通知类
4.配置文件中配置,导入aop约束
1>目标对象
2>通知对象
3><aop:aspect-autoproxy> 开启注解aop

5.注解
@Aspect 指定当前类是通知类

@Before 前置通知方法
@after-returning 后置通知方法
@around 环绕通知方法
@after-throwing 异常拦截通知方法
@after 后通知方法

@PointCut 抽取切点表达式

 

posted on 2017-12-16 13:55  huodaihao  阅读(106)  评论(0编辑  收藏  举报

导航