Spring IOC

Spring IOC

1.Spring IOC 的注入

注入类实例化对象

手动注入

1.set,通常使用set

2.构造器注入

3.静态工厂注入

4.实例化工厂注入

set注入

1.在类中设置

private dao student;
public void setStudent(dao student) {
    this.student = student;
}

2.在配置文件中设置property 对象

ref 是指 注入的 bean 的 id 属性标签

<bean id="userService" class="com.service.UserService">
   <property name="student" ref = "userdao"></property>
</bean>

注入其他类型数据

  1. 注入基本数据集,string ,interge ,int

    <bean id="userService" class="com.service.UserService">
       <property name="student" value = "121312,,"></property>
    </bean>
    
  2. 注入list

    <bean id="userService" class="com.service.UserService">
       <property name="student">
        <value> keke</value>
        <value> keke</value>
        <value> keke</value>
        </property>
    </bean>
    
  3. 注入map

    <bean id="userService" class="com.service.UserService">
       <property name="student">
        <map>
              <entry>
                      <key>
                           <value>
                           </value>
                      </key>
              </entry>
        </map>
        </property>
    </bean>
    
自动注入

自动注入采用注解形式

  1. @Rescource 或 @Auwired和@Qualif
  2. 使用自动化注入的时候,在spring.xml配置文件中要添加一句
  3. 在需要注入的实例化对象下面加上注解
  4. 当一个类继承多个接口的时候,要用@Qualif 说明调用的注解类型
spring2.5后简化了setter 的注入方法

引入了p名称命名空间

// 业务对象UserDao set注入(提供set方法)
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
<bean id="userService" class="com.xxxx.service.UserService" 
        p:userDao-ref="userDao" 
</beans>

2.Spring 扫描器

作用:把需要在项目中显示的标签简化了

1.在spring.xml配置文件中设置要扫描的范围

<context:component-scan base-package = ""/>

2.在com包中的每一层使用特定的注解

dao 层 : @Repository

service层 : @Service

controller层 : @Controller

任意层 : @Component

3.Spring bean的作用域和生命周期

1.bean的作用域

默认情况下,我们从spring容器中调用的对象都是单例的

对于bean的作用域类型如下

singleton作用域:

注意

lazy-init 是懒加载,如果等于true 时的作用是指spring容器启动时不会去实例化这个bean对象,而是在程序调用的时候对它进行实例化,默认是false,即spring容器启动的时候就开启

默认情况下,被管理的bean只会IOC容器中存在一个实例,对于所有获取该Bean的操作Spring容器将只返回同一个Bean。

容器在启动的情况下就实例化所有singleton 的 bean对象,并缓存与容器中

lazy-init设置成false 好处:
  1. 可以发现潜在的配置问题
  2. bean对象存在于缓存中,使用时不需要去实例化bean,加快程序运行效率
什么对象适合单例模式

无状态或者状态不可改变的对象适合单例模式

prototype 作用域

通过scope = “prototype” 设置bean的类型,每次先spring容器请求获取bean都返回一个全新的bean,相对于singleton来说是不存在缓存bean的,每次都是根据bean重新定义一个全新的bean

web应用的作用域

1.request:每次请求都需要重新创建一个全新的bean

2.session:

3.globalSession

配置方式与基本的作用域相同,只是必须要有web环境的支持,并配置相应的容器监听器或者拦截器从而能应用这些作用域,

bean的生命周期
  1. bean定义:spring.xml中进行定义

  2. bean的初始化

    默认在IOC 加载时,实例化对象

    bean 初始话有两种

    //1.init-methond
    public class RoleService {
        // 定义初始化时需要被调用的方法
        public void init() {
            System.out.println("RoleService init...");
        }
    }
    //配置文件中bean后面加 init-method = "init()"
    //在创建的时候,就会默认调用
    
    //2.实现 org.springframework.beans.factory.InitializingBean 接口。
    public class RoleService implements InitializingBean {
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("RoleService init...");
        }
    }
    
  3. bean的使用

    1.BeanFactory

    2.ApplicationContext

  4. bean的销毁

    destroy-method

IOC/DI-控制反转和依赖注入
  将对象实例化的创建过程转交给外部容器(IOC容器 充当工厂角色)去负责;属性赋值的操作;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KeyichenCRAZY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值