使用Spring的好处
Spring是轻量级的JAVAEE应用开源框架
- IOC:控制反转
- AOP:面向切面
- 粘合剂:整合其他技术和框架
什么是IOC
1、耦合
耦合:对象之间的依赖关系(service依赖于dao),耦合度越高维护的成本越高
产生耦合的原因:硬编码
2、IOC
控制:控制对象的创建
IOC (Inverse of Control)即控制反转:正传是自己创建对象;反转是由工厂创建依赖对象
什么是DI
DI(Dependency Injection)依赖注入:将依赖对象从容器中拿出来赋值给调用者
Spring的注入方式有哪些
1、构造方法注入
public UserServiceImpl(UserDao userDao, String msg){
this.userDao=userDao
this.msg=msg
}
<bean id="userService" class="com.by.dao.UserServiceImpl" >
<!--
name="userDao":构造方法的参数名称
ref="userDao":另一个bean标签的id
-->
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
<!--
index="userDao":构造方法的参数的索引位置
value="我靠":基本数据类型和String类型
-->
<constructor-arg index="0" value="我靠"></constructor-arg>
</bean>
2、set方法注入
public setUserDao(UserDao userDao){
this.userDao=userDao
}
public setMsg(String msg){
this.msg=msg
}
<bean id="userService" class="com.by.dao.UserServiceImpl" >
<property name="userDao" ref="userDao"></property>
<property name="msg" value="我操"></property>
</bean>
3、自动注入
//遍历map,并判断每个bean的类型,如果类型符合则赋值
<bean id="userService" class="com.by.dao.UserServiceImpl" autowire="byType"></bean>
BeanFactory和ApplicationContext区别
- ApplicationContext:子接口,加载配置文件时就创建对象
- BeanFactory:顶级接口,使用对象时才创建
spring bean五种作用域(bean放哪了)
| 属性 | 作用域 |
| singleton(单例): | ioc容器 |
| prototype(多例): | newInstance() |
| request | request域 |
| session | session域 |
| application | servletContext域 |
spring bean的生命周期
案例、
public class UserServiceImpl{
private UserDao userDao;
public UserServiceImpl(){
}
public setUserDao(){
}
public void init(){
}
public void destroy(){
}
}
<bean id="" class="" scope="singleton"
init-method="init" destroy-method="destroy" autowire="byType">
单例:
[容器启动]---->构造方法(实例化)---->set方法(注入)---->init方法(初始化)---->[容器关闭]---->destroy方法(销毁bean)
多例:
[使用对象]---->构造方法(实例化)---->set方法(注入)---->init方法(初始化)---->[JVM垃圾回收]---->destroy方法(销毁bean)
什么是AOP
1、为什么要使用AOP?
有一万个service类,要求每个方法执行前打印日志,执行后打印日志
2、什么是AOP?
AOP(Aspect Oriented Programming)面向切面编程:把程序中重复的代码抽取出来,使用动态代理的技术去执行,
从而实现对方法的增强(不修改源代码对方法进行增强)
代理模式的区别与联系
1)静态代理
角色:
公共接口:Star
真实角色:RealStar(周杰伦.唱歌)
代理角色:ProxyStar(宋吉吉.面谈.订机票.签合同.[调周杰伦唱歌].收尾款)
客户类:Client(小孩.调宋吉吉)
缺陷:1.大量代码重复 2.只能代理Star类型的对象
2)jdk动态代理
角色:
公共接口:Star
真实角色:RealStar(周杰伦.唱歌)
代理工厂:ProxyFactroy{生成ProxyStar(宋吉吉.面谈.订机票.签合同.[调周杰伦唱歌].收尾款)}
客户类:Client(小孩.调宋吉吉)
3)Cglib动态代理
cglib和jdk动态代理的区别:cglib真实角色没有接口
aop的核心概念
1.切点(pintcut)
切点(pintcut):要增强的方法,eg:add()、update()
切点表达式:
格式:execution([修饰符] 返回值 报名.类名.方法名(参数))
eg:execution(* com.by.service.*.*(..))
2.通知/增强(advice)
通知/增强(advice):要搞的事情,eg:日志
前置通知:aop:before
后置通知:aop:after-returning【try】
最终通知:aop:after【finally】
异常通知:aop:after-throwing【catch】
环绕通知:aop:around
try{
...
return aop:after-returning
}catch(Exception e){
...
aop:after-throwing
}finally{
...
aop:after
}
3.切面(aspect)
切面(aspect):把增强应用到切点上
常用注解
1)IOC注解
<!-- 告知spring框架,在读取配置文件,创建容器时扫描包,依据注解创建对象,并存入容器中 -->
<context:component-scan base-package="com.by"></context:component-scan>
@Controller:web
@Service:service
@Repository:dao
@Component:三层架构之外
@Scope("prototype"):指定bean的作用域范围,eg:singleton、prototype、request、session、application
2)DI注解
@Resource(name=""):按名称注入
@Autowired:按类型
@Value("${}"):注入基本类型和String类
3)AOP注解
开启spring对aop注解的支持
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Aspect:把当前类声明为切面类
@Before:前置通知,可以指定切入点表达式
@AfterReturning:后置【try】通知,可以指定切入点表达式
@AfterThrowing:异常【catch】通知,可以指定切入点表达式
@After:最终【finally】通知,可以指定切入点表达式
@Around:环绕通知,可以指定切入点表达式
3178



被折叠的 条评论
为什么被折叠?



