Spring 学习笔记《第一部》

来源:互联网 发布:申请淘宝账号注册官网 编辑:程序博客网 时间:2024/06/10 00:14

Spring  2010-11-3 author:heguikun
添加三个框架的的顺序是:
Struts---Spring--Hibernate;
添加时应注意事项:1把包都添加到Web-INF的lib目录下
                                        2.把添加Spring的包中如果同时存在asm.jar和asm-2-2.3.jar
                                           就把后面的asm-2-2.3.jar删除,避免包冲突!
                                       3.web.xml文件中添加对Spring 的初始化进行监听
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
这样在启动tomcat 时就可以知道Spring 是否可用。
                                    4.在Struts配置文件中添加代码,把Struts交给Spring 管理
                                   <!--Struts交给Spring 管理:spring-webmvc-struts.jar中org.springframework.web.struts包
                                           DelegatingRequestProcessor.class  -->
  <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />
                                   
5.Spring和Struts的配置文件都要放在Web-INF下
                                   6.看如下Spring的配置

================================Spring配置文件开始=======================================
<?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:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--Spring提供的session工厂 -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation" value="classpath:hibernate.cfg.xml">
  </property>
 </bean>
 <!--根部Dao -->
 <bean id="BaseHibernateDAO" class="com.aptech.jb.epet.dao.Impl.BaseHibernateDAO"/>
<!--根部Dao的实现类 -->
 <bean id="AllBizImpl" class="com.aptech.jb.epet.biz.Impl.AllBizImpl">
  <property name="all" ref="BaseHibernateDAO" />
 </bean>
<!--宠物PetAction -->
 <bean name="/pet" class="com.aptech.jb.epet.action.PetAction">
  <property name="allBiz" ref="AllBizImpl"></property>
 </bean>
<!--日记DiaryAction -->
 <bean name="/Petdiary"
  class="com.yourcompany.struts.action.DiaryAction">
  <property name="allBiz" ref="AllBizImpl" />
 </bean> 
<!--员工ProperyAction --> 
 <bean name="/propery" class="com.yourcompany.struts.action.ProperyAction">
  <property name="allBiz" ref="AllBizImpl"/>
 </bean>
</beans>
=================================Spring配置文件开始====================================
注意以上文件:根部BaseHibernateDAO  交给业务的AllBizImpl实现类
AllBizImpl交给Action
但Action 的bean中没有id属性了,而改为name属性:name="/pet" 值是Struts的配置文件Axtion的path="/pet" 的值
而且要用Spring 帮实例化必须要有get() set() 方法才能取到对象,用了Spring取到对象后在Action中也要Spring 取
到对象的值,如果不是会报错。详细原因还没弄清楚??

1.三种实例化bean的方式
    1. 使用构造器实例化
       <bean id="orderService" Class="heguikun.OrderServiceBean"/>
    2. 使用静态工厂方法实例化
       <bean id="personService" Class="heguikun.service,OrderFactory" factory-method="createOrder"/>
         工厂类如下:
       public Class OrderFactory{
         public static OrderServiceBean createOrder()
           {
                   return new OrderServiceBean();
            }
         }
    3.使用实例化工厂方法实例化
       <bean id="personServiceFactory" Class="heguikun.service,OrderFactory" />
       <bean id="personService" factory-bean="personServiceFactory" factory-method="createOrder"/>
       工厂方法如下:
        public Class OrderFactory{
          public OrderServiceBean createOrder()
           {
                   return new OrderServiceBean();
            }
         }
     《3跟2对比少了个static而已》

2.指定bean 的作用域 1.singleton((单例)默认,每次调用都是同一个实例就是说他们的引用地址一样)
                    2.prototype(每次调用都返回新的对象,就是地址不同)
                    3.request 4.session 5.global session(全局)

3.单例时(singleton)是在启动容器时实例化bean
  圆形时(prototype)是在调用bean的方法时实例化
  (singleton)但如果设置lazy-init="true" 就是延迟初始化后它不会在启动容器时实例化bean
  在<beans> 中写这句话他就会所有的bean都会延迟,但是少用这个属性,因为有错的话要做运行期才能发现。
4.初始化方法 init-method="初始化执行的方法"。
  destroy-method="销毁方法",一般用于释放资源,不写则在关闭容器是销毁
5.依赖注入对象
  1.基本类型注入:
      属性注入要有set()
  2.注入其他bean
 
  3.使用内部bean,但该bean不能被其他bean使用
 
6.集合类型的注入:(通过get()set()注入)
   1.set()  2.list ()  3.properties()  4.map() (他们必需要有get()set()才能注入)
下面请看 spring配置文件的注入:
<beans>
    <bean id="personservice" class="heguikun.service.impl.PersonServiceBean">
      <!--1.set()-->
 <property name="sets">
           <set>
              <value>第一个</value>
              <value>第二个</value>
              <value>第三个</value>
            </set>
       </property>
   <!--2.list ()-->
       <property name="lists">
           <list>
              <value>第一个list</value>
              <value>第二个list</value>
              <value>第三个list</value>
            </list>
       </property>
   <!--3.properties()-->
       <property name="properties">
           <props>
              <prop key="key1">第一个prop</prop>
              <prop key="key2">第二个prop</prop>
              <prop key="key3">第三个prop</prop>
            </props>
       </property>
   <!--4.map()-->
       <property name="maps">
           <map>
               <entry key="keymap" value="第1个map"/>
              <entry key="keymap" value="第2个map"/>
              <entry key="keymap" value="第3个map"/>
           </map>
       </property>
    </bean>
</beans>
<!--=======下面是配置文件的语法==========-->
7.通过构造器注入(即构造方法)
<!--接口的实现类-->
   <bean id="personDao" class="heguikun.service.impl.PersonBean">
      <!--使用接口服务的一个类-->
         <!--这个类有一个接口型的属性和一个name属性,然而在构造方法中给属性赋值-->
   <bean id="personservice" class="heguikun.service.impl.PersonServiceBean">
       <constructor-arg index="0"  type="heguikun.dao.personDao" ref="personDao"/>
       <constructor-arg index="1"  type="是基本数据类型可以省掉这个属性"  value="何桂坤"/>
  </bean>

8.最优雅的Field注入
  解决配置文件的臃肿....
 我们要 在beans中加入这些命名空间:
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/><!--表示打开注解-->
这个配置隐式注册了多个 注释进行处理的处理器:AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,
Required
AnnotationBeanPostProcessor

AutowiredAnnotationBeanPostPr处理器对应@Autowired
CommonAnnotationBeanPostProcessor处理器对应@Resource


@Autowired按类型进行装配,就是根据他的类型查找想要的benan进行注入
@Resource 默认按名称装配,名称找不到,再按类型
下面使用:@Resource注解是 j2e提供的(@Autowired是spring提供的)
看如下实现类:
import javax.annotation.Resource;//要导入这个命名空间
public class PersonServiceBean implements PersonService{
   @Resource(name="persondao")   private   personDao personDao;//get()set()方法都不用些
   public PersonServicebean(){}
   public void save() {
        personDao.add();//注解成功后(就是注入成功)就可直接调用这个对象的方法了
     }
}
如 @Resource(name="persondao")的name属性省了的话就根据类型查找,不省就先根据名
 字找,反正先根据名字,找不到再根据类型