Tom Xu

 

Spring 学习笔记(实例化方式 spring bean作用域 spring依赖注入 spring自动扫描 spring和struts整合步骤)

 
spring bean 实例化的方式
        1.默认情况下是调用bean对象的默认构造函数来示例话bean
        2.调用工厂的静态方法来实例化bean
                <bean id="bean实例name" class="工厂类" factory-method="工厂的静态方法"></bean>
        3.调用工厂方法来实例化bean
                 <bean id="工厂实例name" class="工厂类"  class="工厂类名">
                 <bean id="bean实例name" factory-name="工厂实例name",factory-method="工厂的非静态方法">
spring bean 的作用域 (可以使用scope="singleton/prototype/request/session/global session"来配置)
        1.默认作用域(singleton)情况下:
        bean的实例化是在容器实例化(被主动申请加载)时就实例化
        将lazy-init="true" (延迟初始化) 不会再容器实例化实例化bean (不建议使用)
        用init-method="methodName" 可以指定bean被实例化时调用的方法
        用destory-method="methodName" 可以制定容器调用close方法后bean执行的方法
         2.配置prototype(原型)作用域:
       bean的实例化是在调用getBean方法时被实例化的
       每次调用一个getBean方法都回返回一个新的对象

Spring Dependency Injection(Spring的依赖注入)
       1.使用映射的方式注入
<bean id="personDao" class="接口实现类路径"></bean>   
<bean id="persionService" class="接口实现的类路径">
    <property name="personDao" ref="personDao"></property>   <!--反射赋值-->    
    <property name="name"  value="tom test!!!!!!!!"></property> <!--为基本类型赋值-->
            <property name="name"  value="100"></property> <!--为基本类型赋值-->
</bean>
       2.使用内部bean 的方式注入
        <bean id="persionService" class="实现的类路径">
             <property name="persionDao">
                    <bean  class="dao实现路径"/>
             </property>
        </bean>
      3.集合类型的注入
                  <bean id="personService" class="实现类路径">
                     <property name="sets">
                        <set>                                                                    <!--此处可以为list-->
                                <value>一个value</value>
                                <value>两个value</value>
                        </set>
                         <map>
                                 <entry key="key-1" value="value-1"/>
                                <entry key="key-2" value="value-2"/>
  <entry key="key-3" value="value-3"/>
                         </map>
                     </property>
                   </bean> 
      4.使用构造器参数注入(调用特定的构造函数)
             如果PersonServiceBean中的有构造函数PersonServiceBean(PersonDao personDao,String name){}
                <bean id="personDao11111111" class="类实现路径"><bean>
                <bean id="personService" class="类实现路径">
                    <constructor-arg index="0" type="自定义type类的路径" ref="personDao111111">
                    <constructor-arg index="1" type="基本类型可以省去......"  value="111111111">
                </bean>       
      5.使用Field注入(用于注解的方式)
        属性的值注入
        @Resource private PersonDao personDao;     /* 先找到 容器中的 bean的对应名称进行匹配,若无,找类型匹配 */   
         属性的set方法注入
        @Resource
        public void setXXX(Object object){}     

Spring 自动扫描和管理(扫描的方式去自动管理组建)
引入类配置<context:component-scan base-package>
                 @Service @ Resposity
          通过class 类名首字母小写 @Service("对象名")来获得,也可以  @scope("prototype") 来设置初始化类的属性
         可以在方法名上面@PostConstruct 可以制定初始化方法
         可以在方法名上面@PreDestory 可以在bean实例被摧毁之前执行的方法。

Spring和Struts2的整合
1.找到Struts2-Spring-plugin-.XX.jar包和Spring.jar包 加入到web项目的lib包下
2.配置WEB-INF/web.xml文件,加入与struts整合的监听器
     <!-- 
   <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:beans.xml</param-value>
</context-param>
     -->
    <!-- 若applicationContext.xml文件不在WEB-INF 下则需要将将路径放入上面参数中-->
<listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
        
3.在struts.xml文件中加入spring常量(structs中对象的创建叫给spring)
     <constant name="struts.objectFactory" value="spring"/>    
    <action class="loginAction" name="loginAction_*"  method="{1}">
          <result name="success" type="redirect">index.jsp</result> 
   </action>

4.创建applicationContext.xml文件,文件可以放在web-inf目录下,这样可以不用在1中加入context的配置
<?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 id="loginAction" class="xxxxxxxxxxxxxxxxxxxxxxx" scope="prototype"/>
</beans>
注意:(1)struts2中需要加入prototype(原型)的配置,这样才能满足struts2为每次请求创建一个对象的服务方式  
          (2)此处的id 名需要与3中的class同名

posted on 2012-11-04 22:43 Tom Xu 阅读(2355) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜