Spring初步学习

一、Spring学习

1.1什么是Spring
Spring是一个主要解决了对象依赖问题及现有技术的整合问题,同时彻底解决了事务方面的问题。
1.2自定义Spring
Spring实质是配置对象,创建工厂生产对象,使用对象时都要从工厂中获取
Spring之父
学习Spring 主要是学习IOC、DI、AOP、声明事务
轮子理论:现在的技术如果已经很成熟,那么就直接包容它,拿来用。
技术以实用为准,

二、Spring快速入门

导入jar包
commons-lohhing.jar
spring-bean.release.jar
spring-context.release.jar
spring-core.release.jar
spring-expression.release.jar
编写applicationContext.xml配置文件
找到schema约束文件  找到bean约束
xsd-configuration.html

三、Spring原理分析

1.创建配置文件
2.编写配置文件(加入一些对象的配置信息)
3.Spring本身就是一个大工程,内部可以认为是xml文件解析,加上反射技术,就可以根据用户的配置信息,生成相应对象。
4.提供一个getBean()方法,供用户直接从工厂中获取对象

Spring创建同类型同属性值的对象,只会创建一次。单例对象
Spring在加载,读取配置文件,直接生成相应对象。

三、Spring对象生成以及注值方式

生成方式:默认使用类的无参构造来创建对象,静态工厂,实例工厂
1.无参构造创建对象
    <bean id="zhangsan" class="cn.lx.domain.Customer">
    <property name="custId" value="200"></property>
    <property name="custName" value="张小三"></property>
    </bean>  
2.静态工厂创建对象 

    public class StaticFatory {
        public static Customer getStaticCustomer(){
            return new Customer();
        }   
    }
    <bean id="staticFactory" class="cn.lx.factory.StaticFatory" factory-method="getStaticCustomer">

3.实例工厂创建对象
    public class InstanceFactory {
        public Customer getInstance(){
            return new Customer();
        }
    }

    <bean id="instanceFactory" class="cn.lx.factory.InstanceFactory"></bean> 
    <bean id="c1" factory-bean="instanceFactory" factory-method="getInstance"/>
    <bean id="c2" factory-bean="instanceFactory" factory-method="getInstance"/>

注值方式:setter注值,构造构造函数注值,p名称空间注值,注对象
1.setter注值
    <bean id="zhangsan" class="cn.lx.domain.Customer" >
    <property name="custId" value="200"></property>
    <property name="custName" value="张小三"></property>
    </bean>  
2.构造函数注值
    <!-- 构造 函数注值-->
    <bean id="lisi" class="cn.lx.domain.Customer">
    <!--
        构造函数参数
        index表示构造函数对应参数的位置,从0开始
        type表示参数的类型 java.lang,类型
        value表示参数的值

      -->
    <constructor-arg index="0" type="java.lang.Long" value="3000"></constructor-arg>
    <constructor-arg index="1" type="java.lang.String" value="张三"></constructor-arg>
    </bean>    
3.p名称空间注值
    <bean
    xmlns:p="http://www.springframework.org/schema/p">
    <!--p名称空间注值  -->
<bean id="wangwu" class="cn.lx.domain.Customer" p:custId="200" p:custName="王五"></bean>  
4.注入对象
    普通注入
    <bean id="zhangsan" class="cn.lx.domain.Customer" scope="">
    <property name="custId" value="200"></property>
    <property name="custName" value="张小三"></property>
    </bean>  
    <bean id="zhangsan" class="cn.lx.domain.LinkMan" >

    <property name="customer" ref="张小三"></property>
    </bean> 
    构造器注对象:
<constructor-arg index="1" type="包名.类名" ref="zhangsan"></constructor-arg>
    P名称空间注对象
<bean id="aj" class="cn.itcast.domain.Linkman " p:custId="200" p:customer-ref="cgx"></bean>

关闭工厂 
    applicationContext子类.close()

四、Spring对象补充

4.1 对象生命周期
    <bean id="" class=""  init-method="" destroy-method=""></bean>
        init-method 就是指定初始化方法
        deftory-method 就是指定销毁方法
    1、当spring配置文件加载时,就会完成对象的实例化工作
    2、初始化,就会调用初始化方法
    3、当ApplicationContext对象销毁后,执行销毁方法
4.2 对象的作用域
bane上面配置一个属性  scope  
类别                 说明
sigleton           在spring loc 容器中仅存在一个bean实例,bean实例以单例方式存在
prototype         每次从容器中调用bean时,都返回一个新的实例,即存在多实例
request           每次http 请求都会创建一个新的bean,该作用域进适用于WebApplicationContext环境
session           同一个session中共享一个bean
主要使用singleton   prototype 
注意:配置Action类时,一定要配置scope="prototype"

五、spring、web、struts2整合

5.1 SprIng与web整合
目的是实现spring的配置文件只加载一次
注意导入jar包 spring-web.jar
1、首先配置web.xml文件     
<!-- 配置spring 与web项目结合
    需要配置一个监听器,实现启动服务器的时候加载spring配置文件,实现配置文件只加载一次
 -->
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <!--因为监听默认加载的spring文件路径是/web-inf/applicationContext.xml 
    name:
    路径value:classpath:applicationContext.xml
  -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

2、获取容器的方法
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
WebApplicationContext wac1 = (WebApplicationContext) this.getServletContext().getAttribute(
            WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
建议使用第一种方法
3、获取对象的方法   
Customer customer = (Customer) wac.getBean("c1");
5.2 SprIng与strutsb整合开发
注意导入jar 包
    struts-spring-plugin.jar
1、web.xml文件中红配置struts2核心控制器  StrutsPrepareAndExcuteFileter
2、web.xml 配置spring 对应的监听器   ContextLoaderLinstener
    同时配置初始化参数,用于加载spring配置文件   contextConfigLocation=classpath:applicationContext.xml
3、配置applicationContext.xml配置文件
4、更新程序,将所有获取对象的方式采用对象注入的方法获取
    Service层
        //使用对象注入的方式生成对象
        CustomerDao  customerDao;
        public void setCustomerDao(CustomerDao customerDao) {
            this.customerDao = customerDao;
        }
    Action类中
        //由对象注入的方式生成CustomserServcice对象
        private CustomerService customerService;

        public void setCustomerService(CustomerService customerService) {
            this.customerService = customerService;
        }
5、配置applicationContext.xml文件
      <bean id="customerDao" class="cn.lx.dao.imp.CustomerDaoImp" ></bean>
       <!-- 管理CustomerService -->
       <bean id="customerService" class="cn.lx.service.imp.CustomerServiceImp" >
            <property name="customerDao" ref="customerDao"></property>
       </bean>
       <!-- 管理CustomerAction
       scope=prototype 用于表示可以生成多实例
        -->
        <bean id="customerAction" class="cn.lx.web.action.CustomerAction"  scope="prototype">
            <property name="customerService" ref="customerService"></property>
       </bean>
        注意:配置Action时,一定要声明scope="prototype"属性,将生成的Action对象变为多实例
6、配置struts.xml文件
    注意:class需要指定在applicationContext.xml中配置对应的Action的id值的<bean id="" class="">
    <action name="customerAction_*" class="customerAction" method="{1}" >
    </action>
7、测试是否成功运行

注解开发Spring

1.加入aop.jar包
2.引入context.schema约束
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 测试使用注解进行开发 -->

    <!-- 代表在指定的base-package=“cn.lx”包开头的包中,都要进行组件扫描,也就是查找哪些使用了注解 -->

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

    </beans>
3、配置注解
    dao层
        类上@Repository("customerDao")//相当于<bean id="customerDao">
    service
        类上@Service("customerService")//<bean id="customerService">

        对象上@Autowired//按类型取值
            @Qualifier("customerDao")//相当于ref的取值
        private CustomerDao  customerDao;
        public void setCustomerDao(CustomerDao customerDao) {
            this.customerDao = customerDao;
        }
    action类
        类上@Controller("customerAction")//<bean id="customerAction">
            @Scope("prototype")//多实例

        对象上@Autowired
            @Qualifier("customerService")
        private CustomerService customerService;

        public void setCustomerService(CustomerService customerService) {
            this.customerService = customerService;
        }

你可能感兴趣的:(spring,技术)