spring的学习笔记-1

##bean scope  ,bean的生存范围

    在api文档里面显示有5个类型。如下:


    后面3个:request,session,global session,是在spring和web项目结合的时候可以配,如struts 2或者使用Spring mvc的时候。
    它默认是singleton,它在大多数情况下已经够用了,
    singleton的含义是:单例模式,无论你拿多少次Bean,都是拿的同一个Bean。
    
    scope="prototype"
    prototype含义是:原型,每次拿都是一个新的Bean。

##集合注入
    API文档里面的解释,值可以自己赋,也可以引用其他的Bean。
<bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setAdminEmails(java.util.Properties) call -->
    <property name="adminEmails">
      <props>
          <prop key="administrator">administrator@example.org</prop>
          <prop key="support">support@example.org</prop>
          <prop key="development">development@example.org</prop>
      </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
      <list>
          <value>a list element followed by a reference</value>
          <ref bean="myDataSource" />
      </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
      <map>
          <entry key="an entry" value="just some string"/>
          <entry key ="a ref" value-ref="myDataSource"/>
      </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
      <set>
          <value>just some string</value>
          <ref bean="myDataSource" />
      </set>
    </property>
    </bean>
##自动装配 AutoWire
    no   
    (Default) No autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.
    (默认值)不使用自动装配。 Bean的引用必须通过ref元素定义。更改默认设置,不建议较大规模的部署,因为在指定的合作者明确赋予更大的控制和清晰度。

    byName   
    Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property.
    通过名称来实现转配,必须保证实现它的setter方法。

    byType   
    Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set.
    如果允许的确切属性类型相同的bean存在于容器的属性自动装配。如果存在多个,一个致命的异常被抛出,这表明你可能不使用byType的自动装配该Bean 。如果没有匹配的bean,没有什么发生,属性页不会设置。

    constructor   
    Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised

    
    一个byName例子:
    1.UserService.java
 package com.service;
    import com.dao.UserDAO;
    import com.model.User;
    
    public class UserService {
        
        private UserDAO userDAO;  
        public void add(User user) {
            userDAO.save(user);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        public void setUserDAO(UserDAO userDAO) {
            this.userDAO = userDAO;
        }    
    
    }
    2.beans.xml
<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <bean name="userDAO" class="com.dao.impl.UserDAOImpl">
          <property name="daoId" value="1"></property>
    </bean>     
    <bean id="userService" class="com.service.UserService" scope="prototype" autowire="byType">
    </bean>     
</beans>
    在UserService.java文件中的属性名为:userDAO,然后用这个名字在beans.xml文件里面寻找响应的
    Bean。
    byType装配的话,只需要修改为:autowire="byType",但是注意只能有一个同类的Bean。

##生命周期
    1.Lazy-initialized beans (延迟初始化bean)
    ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化过程的一部分,ApplicationContext实例会创建并配置所有的singleton bean。通常情况下这是件好事,因为这样在配置中的任何错误就会即刻被发现(否则的话可能要花几个小时甚至几天).
    有时候这种默认处理可能并不是你想要的,有时候Bean太多了,启动太慢,所以你需要把一些设置为启动时创建。如果你不想让一个singleton bean在ApplicationContext初始化时被提前实例化,那么可以将bean设置为延迟实例化。一个延迟初始化bean将告诉IoC 容器是在启动时还是在第一次被用到时实例化.
    在XML配置文件中,延迟初始化将通过<bean/>元素中的lazy-init属性来进行控制。例如:
    <bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
   <bean name="not.lazy" class="com.foo.AnotherBean"/>
 
    当ApplicationContext实现加载上述配置时,设置为lazy的bean将不会在ApplicationContext启动时提前被实例化,而not.lazy却会被提前实例化。
    需要说明的是,如果一个bean被设置为延迟初始化,而另一个非延迟初始化的singleton bean依赖于它,那么当ApplicationContext提前实例化singleton bean时,它必须也确保所有上述singleton 依赖bean也被预先初始化,当然也包括设置为延迟实例化的bean。因此,如果Ioc容器在启动的时候创建了那些设置为延迟实例化的bean的实例,你也不要觉得奇怪,因为那些延迟初始化的bean可能在配置的某个地方被注入到了一个非延迟初始化singleton bean里面。

    2.init-method(初始化回调方法)

    实现初始化回调方法有:
        <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
        public class ExampleBean {
          public void init() {
              // do some initialization work
          }
        }        
        ...is exactly the same as...   
       <bean id="exampleInitBean" class="examples.AnotherExampleBean"/>   
        public class AnotherExampleBean implements InitializingBean {
        
          public void afterPropertiesSet() {
              // do some initialization work
          }
        }    
        ... but does not couple the code to Spring
    3.destroy-method
    通常,要避免使用DisposableBean标志接口而且不鼓励使用该接口,因为这样会将代码与Spring耦合在一起,有一个可选的方案是,在bean定义中指定一个普通的析构方法,然后在XML配置文件中通过指定destroy-method属性来完成。如下面的定义所示:
    
    <bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
    public class ExampleBean {
    
        public void cleanup() {
            // do some destruction work (like releasing pooled connections)
        }
    }
    
    ...效果与下面完全一样...
    
    <bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
    public class AnotherExampleBean implements DisposableBean {
    
        public void destroy() {
            // do some destruction work (like releasing pooled connections)
        }
    }
    ... 但是没有将代码与Spring耦合在一起。
    4.scope="prototype"
    destroy-method、init-method不要和scope="prototype"一起使用,使用了scope="prototype",就没有destroy-method了。scope="prototype"每次都会初始化一个新的Bean,所以调用几次就会执行几次init-method.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值