springbean的装配

来源:互联网 发布:腾讯绿标域名跳转代码 编辑:程序博客网 时间:2024/05/17 04:09
scope singleton 单例模式无论有多少个Bean引用它 始终指向同一个对象
prototype 原型模式 每次通过Spring容器获取prototype定义的bean时 容器都将创建一个新的bean实例
request 当提出一次Http请求 容器会返回该bean的同一个实例 对不同的http请求则会产生一个新的bean 且此bean仅在HttpRequest内有效
session 在一次HttpSession 容器返回该Bean同一个实例 对不同的http请求则会产生一个新的bean 且此bean仅在HttpSession内有效
globalSession 在一个全局的HttpSession中容器返回该Bean的同一个实例 仅在使用portlet context 时有效
Bean生命周期
1.Spring实例化Bean
2.利用依赖注入配置bean中的属性值
3.如果bean实现了BeanNameAware接口 则Spring调用Bean 的setBeanName()方法传入当前Bean的Id值
4.如果bean实现了BeanFactoryAware接口 则调用bean 的setBeanFactory()方法传入当前工厂实例的引用
5.如果bean实现了ApplicationContextAware接口 则spring会调用setApplicationContext()方法传入ApplicationContext实例的引用
6.如果bean实现了BeanPostProcessor 接口则spring调用postProcessorBeforereInitialzation()方法
7.如果bean实现了InitializingBean接口 则spring会调用afterpropertieszSet()方法
8.如果在配置文件中通过init-method 属性指定了初始化方法则调用该初始化方法
9.如果bean实现了BeanPostProcessor 会调用 postProcessAfterInitalizing()方法
10.此时bean实例化完成 就可以被使用了 会保存在spring容器中 直至销毁
11.如果bean实现了DisposableBean接口 则spring会调用destroy()方法
12.如果在配置文件中通过destroy-method属性指定了bean的销毁方法 则调用该方法
bean 的装配方式 依赖关系的注入
基于XML装配
基于Annotation的装配 注解
@Compontent 描述spring中的bean 
@Repository DAO的标识为spring的bean
@Service 作用于业务层 将业务层的类标识为spring的Bean
@Constroller 作用于控制层 用于将控制层的类标识为spring中的bean
@Autowired 用于对Bean的属性变量,属性的set方法及构造函数进行标注 配合相应的注解完成bean的自动配置
@Resource 跟@Autowired功能相同 @Autowired按照bean类型装配 而@Resource按照bean的实例名称进行装配 name属性bean的实例名  type 解析为bean的实例类型
@Qualifier 跟@Autowired联合使用 会将默认的按bean类型装配修改
<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
autowire="byName" 根据bean的名字自动装配
byType 根据bean的类型装配
construtor 根据构造方法参数的数据类型 联合bytype进行装配
autodetect 如果发现默认的构造函数 用constructor模式 否则用bytype模式
no 不使用自动装配




AOP 面向切面编程
Aspect  Oriented  programing
横向抽取机制 取代了传统的纵向继承体系 事务处理 日志管理 权限控制 异常处理等
AOP框架    Spring      AspectJ
上一刀  下一刀  中间注入 


AOP术语
 Joinpoint 连接点  被拦截到的点 
Pointcut 切入点 只对那些连接点进行拦截 
Advice 通知 拦截到的连接点之后要做的事情
Target 目标  代理的目标对象
Weaving 织入 把增强代码应用到目标上 生成代理对象的过程
Proxy 代理 生成的代理对象
Aspect 切面  切入点和通知的结合


代理 


JDK代理 CGLIB代理





原创粉丝点击