zoukankan      html  css  js  c++  java
  • Spring学习 Ioc篇(一 )

         一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现。就从Ioc篇开始学习。

    1、实例化spring容器的两种方法

          方法一:在类路径下寻找配置文件来实现Spring容器的实例化

    Java代码  
    1. ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});  

         方法二:在文件系统路径下寻找配置文件进行Spring容器的实例化

    Java代码  
    1. ApplicationContext cx= new FileSystemXmlApplicationContext(new String[]{"D:\applicationContext.xml"});  

         在实际的应用中建议使用第一种方式,在程序开发中比较灵活,与路径无关。同时,sping可以指定多个配置文件,由数组传入。

    2、实例化bean的三种方式

           方法一:使用类构造器实例化

    Java代码  
    1. //接口  
    2. package com.cvicse.service;  
    3. public interface PersonService {  
    4.     public void save();  
    5. }  
    6. //实现类  
    7. package com.cvicse.service.impl;  
    8. import com.cvicse.service.PersonService;  
    9. public class PersonServiceBean implements PersonService {  
    10.     public void save(){  
    11.         System.out.println("我是save()方法");  
    12.     }  
    13. }  

        在applicationContext.xml的XML中配置如下:

    Java代码  
    1. <bean id="personService" class="com.cvicse.service.impl.PersonServiceBean"></bean>  

       在应用类中调用

    Java代码  
    1. ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});  
    2. PersonService personService = (PersonService)cx.getBean("personService");  
    3.         personService.save();  

           方法二:使用静态工厂方法实例化

        在方法一的基础上添加一个静态工厂方法,专门实例化bean

      

    Java代码  
    1. package com.cvicse.service.impl;  
    2. public class PersonServiceBeanFactory {  
    3.     public static PersonServiceBean createPersonServiceBean(){  
    4.         return new PersonServiceBean();  
    5.     }  
    6. }  

       在applicationContext.xml的XML中配置如下:

    Java代码  
    1. <bean id="personService" class="com.cvicse.service.impl.PersonServiceBeanFactory"   
    2.           factory-method="createPersonServiceBean"/>  

       在应用类中调用

    Java代码  
    1. ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});  
    2. PersonService personService = (PersonService)cx.getBean("personService");  
    3.         personService.save();   

           方法三:使用实例工厂方法实例化

           在方法一的基础上添加一个工厂方法类,用于bean的实例化

     

    Java代码  
    1. package com.cvicse.service.impl;  
    2. public class PersonServiceBeanFactory {  
    3.     public PersonServiceBean createPersonServiceBean(){  
    4.         return new PersonServiceBean();  
    5.     }  
    6. }  

       在applicationContext.xml的XML中配置如下:

    Java代码  
    1. <bean id="personServiceFactory" class="com.cvicse.service.impl.PersonServiceBeanFactory"/>  
    2. <bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean"/>  

       在应用类中调用

    Java代码  
    1. ApplicationContext cx= new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});  
    2. PersonService personService = (PersonService)cx.getBean("personService");  
    3.         personService.save();   

    3、bean的作用域范围

        (1) singleton:默认的情况下在容器启动只有一个bean的实例化对象,在容器实例化的时候初始化。如果添加了lazy-init=“true”则会延迟加载。

        (2) prototype:每次调用都会初始化一个新的实例对象

    Java代码  
    1. <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"/>  

        (3) request

        (4) session

        (5)global session

     4、spring容器管理bean的生命周期

          bean默认的情况下是在sping容器实例化的时候被创建,在spring容器关闭之前被销毁。

          在配置文件中可以为每个bean指定初始化方法和销毁方法

    Java代码  
    1. <strong> <bean id="personService"  
    2.             class="cn.com.impl.PersonServiceBean"  
    3.             init-method="initMethod" destroy-method="destroyMethod"></bean></strong>  

     






  • 相关阅读:
    ExtJS小技巧
    Oracle 表的行数、表占用空间大小,列的非空行数、列占用空间大小 查询
    NPM 私服
    IDEA 不编译java以外的文件
    SQL 引号中的问号在PrepareStatement 中不被看作是占位符
    Chrome 浏览器自动填表呈现淡黄色解决
    批量删除Maven 仓库未下载成功.lastupdate 的文件
    Oracle 11g 监听很慢,由于监听日志文件太大引起的问题(Windows 下)
    Hibernate 自动更新表出错 建表或添加列,提示标识符无效
    Hibernate 自动更新表出错 More than one table found in namespace
  • 原文地址:https://www.cnblogs.com/jeffen/p/6155481.html
Copyright © 2011-2022 走看看