Spring ioc

     Spring IOC  即控制反转                                                                                                                                   

  •   创建bean时什么时候该用多例什么时候该用单例
  •   创建bean 的几种方式 
    • property 
    • 构造函数
    • 自动注入
    • 属性注入
  •   bean 的Annotation 

   主要是会这几个方面来实践 spring  IOC 

      创建bean时什么时候该用多例什么时候该用单例                                                                                                                                               
   
    首先了解 bean的单多例;

   一般来说spring 创建 bean 一般都是单例

  单例:
  <bean id="user" class="user 类的地址" scope="singleton" >

  多例:
  <bean id="user" class="user类的地址" scope="prototype" >

  那么什么时候该是单例, 什么时候该是多例:
 
  这个主要看这个类,的属性值会不会随着数量增多而改变:  换句话说: 这个类会根据不同的线程是否会得到不同的值  一般如果这个类只有方法, 那么是单例, 如果这个类有  属性, 那么很大程度上是多例;

    创建bean 的几种方式                                                                                                                                                                                              
 
       property注入:
      
       第一步: 在bean.xml 文件里创建所有类的bean 
       第二步: 对有依赖的类完成setter 和 getter 方法:
       其原因是因为:

     <bean id="userService" class="类的地址">
          <!--name 中的值会在userService 对象中调用setXX 方法注入, 诸如: name="userDao"
           在具体注入的时候会调用 setUserDao (IUserDao userDao) 来完成注入
            ref ="userDao" 表示是配置文件中的bean 中的所创建的dao 的id--!>
       <property name="userDao" ref="userDao"></property>
     </bean>

        构造函数注入:
       
     <bean  id="类名" class="类名所在地址">
       <contructor-arg  ref="id名称"></contructor-arg> 
     </bean>


        自动注入:
    
   byName 表示 根据 类里的set名字来注入
   byType 表示根据类里的对象来注入
   <bean  id="类名" class="类名所在地址" autowire ="byName">
   </bean>
    
 
    简单地在这里写上 defauit-autowire ="byName", 所有的都自动注入 ,一般不建议这么做, 因为导致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:aop="http://www.springframework.org/schema/aop"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" defauit-autowire ="byName">


  
          属性注入: 

          demo: 

          <bean  id="user" class="org.zttc.itat.spring.model.User">
                <property name="id" value="1"/>
                <property name="username" value="悟空"/>
          </bean>

 

<!--对于UserAction而言, 里面对的属性值得状态会根据不同线程得到不同的值, 所以应该使用多例--!>
   <bean id ="userAction" class=" org.zttc.itat.spring.action.UserAction" scope="prototype">
     <property name="userService" ref="userService"/>  <!--引用--!>
     <property name="user" ref="user">
     <property name="id" value="12">
    <!--同样的注入列表, 但是也不常用--!>
      <property name="names">
          <list>
                <value>aaa</value>
               <value>bbb</value>
               <value>ccc</value>
         </list>
      </property>
</bean>

     bean 的Annotation                                                                                                                                                                                                       


    1 :  第一步 : 在bean.xml 文件配置
  
     <!--打开spring的Annotation 支持>
     <context: annotation-config/>
     <!--设定Spring 去那些包中找annotation -->
     <context:compotent-scan base-package="">

    2: 第二步 : 给每个类设置bean  
                   为每个类添加标签

   3: 第三部: 依赖注入

       3.1: autowire  标签是默认类型注入
       3.2: resource  标签是默认名称注入
       
    4: 标签:

           @Component ("类名") // 公共创建bean 的 Annotation 
           @Repository("类名") // @Repsitory 一般用于dao的注入
           @Controller ("类名") // mvc的控制层一般使用@Controller
           @Service("类名") // 业务层一般使用@Service
           @Scope("prototype")//  设置多例
            





  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值