• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
zziy的java笔记
博客园    首页    新随笔    联系   管理    订阅  订阅
Spring知识点
  • IOC: Inversion of Control 控制反转

①自己控制→容器控制

②控制具体实现→控制抽象(接口)

 

 

  • DI:Dependency Injection 依赖注入

依赖于容器注入的对象

 

  • 注入方法

a) setter(重要)

<property name="userDAO" ref="u"/>

或

<property name="userDAO">
    <ref bean="u"/>
</property>

b) 构造方法

    <constructor-arg>
        <ref bean="u"/>
    </constructor-arg>

c) 接口注入

 

  • bean标签 name/id 区别:name可以加特殊字符
  • 生命周期 

  a) lazy-init 这个bean在容器初始化的时候不进行初始化,用到的时候初始化,应用启动很慢的时候用

  b)init-method destroy-methd 不要和prototype一起用(了解)

 

  • Annotation: 注解

a) 默认按类型by type

b) 如果想用byName,使用@Qulifier

c) 写在private field(第三种注入形式)(不建议,破坏封装)

d) 如果写在set上,@qualifier需要写在参数上

e)启用注解<context:annotation-config/>

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:annotation-config/>
</beans>

 

  • @Resource(重要)

   @Resource(name="xxx")

a) 加入:j2ee/common-annotations.jar

b) 默认按名称,名称找不到,按类型

c) 可以指定特定名称

d) 推荐使用

e) 不足:如果没有源码,就无法运用annotation,只能使用xml

 

  • @Component @Service @Controller @Repository

a) 初始化的名字默认为类名首字母小写

b) 可以指定初始化bean的名字

 

 

  • @PostConstruct = init-method; @PreDestroy = destroy-method;

 

posted on 2015-12-24 10:47  zziy  阅读(177)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3