zoukankan      html  css  js  c++  java
  • spring3--IOC(xml配置注入)

    sring的配置文件及jar包说明

    applicationContext.xml 是spring的配置文件。spring是管理Bean的。所以主要是配置Bean,xml的根节点是Beans。

    bean节点:由spring框架创建管理的Jave对象成为Bean

    bean节点的属性id:表示对象的名称,唯一值。spring在创建完对象之后,就是赋值给名为id的引用类型。

    bean节点的属性class:java对象的全限定类型,不能是接口,要是可以创建的类。(框架使用反射创建对象)

    使用spring的最小的jar包:

    spring-expression-5.0.11.RELEASE.jar   //表达式
    
    commons-logging-1.2.jar     //引用的外部日志包
     
    spring-beans-5.0.11.RELEASE.jar    //核心bean包
    
    spring-context-5.0.11.RELEASE.jar   //核心包
    
    spring-core-5.0.11.RELEASE.jar   //核心包
    

      

    一个spring demo 

    ApplicationContext 接口,是spring的容器对象。它的实现类有 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext。
    两者的区别在于:配置文件所放的位置不同。ClassPathXml 一般表示类路径相关。FileSystemXml 磁盘目录,项目根目录。

    代码结构:

     关键类代码:

    package com.cn.dao;
    
    public interface SQLDao {
    
        public void insert();
    }
    
    
    
    package com.cn.dao;
    
    public class DoSomeThingsMysqlDao implements SQLDao{
    
        public void insert(){
            System.out.println("do insert");
        }
    }
    
    
    
    package com.cn.dao;
    
    public class DoSomeThingsOracleDao implements SQLDao{
    
        public void insert(){
            System.out.println("oracle do insert");
        }
    }
    
    
    package com.cn.service;
    
    import com.cn.dao.DoSomeThingsOracleDao;
    
    public class DoSomeThingsService {
    
        private DoSomeThingsOracleDao dao;
        public void add(){
            System.out.println("service do insert");
            dao.insert();
        }
        public DoSomeThingsOracleDao getDao() {
            return dao;
        }
        public void setDao(DoSomeThingsOracleDao dao) {
            this.dao = dao;
        }
        
    }
    
    
    package com.cn.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.cn.service.DoSomeThingsService;
    
    public class Test {
    
        public static void main(String[] args) {
            String resource = "applicationContext.xml";
        //ApplicationContext 接口,是spring的容器对象。它的实现类有 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext。
        //两者的区别在于:配置文件所放的位置不同。ClassPathXml 一般表示类路径相关。FileSystemXml 磁盘目录,项目根目录。
    
            ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
            DoSomeThingsService service=  (DoSomeThingsService) ac.getBean("doSomeService");
            service.add();
        }
    }

    配置文件:

    <?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.xsd"
        >
        
      <!-- id 表示对象的名称,唯一值。spring在创建完对象之后,就是赋值给名为id的引用类型。 -- >
      <!-- java对象的全限定类型,不能是接口,要是可以创建的类。(框架使用反射创建对象) -->
    <bean id="doSomeDao" class="com.cn.dao.DoSomeThingsOracleDao"/> <bean id="doSomeService" class="com.cn.service.DoSomeThingsService" > <property name="dao" ref="doSomeDao" /> </bean> </beans>

     结果输出:

    JavaBean的装配及其作用域,始末方法

    Bean的装配就是,创建Bean对象,并给对象的属性赋值。

    默认的装配方式:

        调用类的无参构造方法。

    JavaBean的五个作用域:

        singleton:单例模式。通过spring Ioc容器来管理bean,使用singleton定义的Bean从始至终将只有一个实例对象。

        prototype:原型模式。定义为prototype的bean, 每次通过容器的getBean方法获取时,都将产生一个新的Bean的实例。

        request:(在web项目使用spring时有效) 每次http请求将会产生不同的Bean实例

        session:(在web项目使用spring时有效) 对于每次http session,使用session定义的bean都将产生一个新的实例

        globalsession:(在web项目使用spring时有效)  global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。

    常用的就是singleton 和 prototype。

    如果不指定Bean的作用域,Spring默认使用singleton作用域。

    Java在创建Java实例时,需要进行内存申请;销毁实例时,需要完成垃圾回收,这些工作都会导致系统开销的增加。

    因此,prototype作用域Bean的创建、销毁代价比较大。

    而singleton作用域的Bean实例一旦创建成功,可以重复使用。

    在DoSomeThingsOracleDao 增加构造函数
    
        public DoSomeThingsOracleDao() {
            System.out.println("dao construction");
        }
    
    在 DoSomeThingsService 增加构造函数
    
        public DoSomeThingsService() {
            System.out.println("DoSomeThingsService construction");
        }
    
    定义的始末方法
    public void destroyA() {
            System.out.println("DoSomeThingsService destroy method");
        }
    
    
    测试方法如下
    
        public static void main(String[] args) {
            String resource = "applicationContext.xml";
            ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
            
            DoSomeThingsService service=  (DoSomeThingsService) ac.getBean("doSomeService");
            
            DoSomeThingsService service2=  (DoSomeThingsService) ac.getBean("doSomeService");
            
        }
    
    dao construction    //dao  是singleton 的,在初始化容器的时候创建
    DoSomeThingsService construction  //Service 是 prototype 的 ,每次getbean时创建一次。
    
    DoSomeThingsService init method
    DoSomeThingsService construction

    配置文件如下:

    <?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.xsd"
        >
        
        <bean id="doSomeDao" class="com.cn.dao.DoSomeThingsOracleDao" />
        <!-- 
        1.bean标签的属性说明。
            bean表示注册一个java bean对象。  
            id 表示容器创建对象之后 赋值给名为id的引用对象。
            class 注册是类。全限定类名。
        2.bean的作用域的说明。
            scope 类的作用域。singleton,prototype,request,session,globalsession。
            单例模式singleton的类,都是在容器初始化的时候创建的
            prototype原型模式,是在 getBean的时候创建的。
        3.bean的初始方法和 结束方法的定义。
            init-method 方法原型: public void 自定义方法名(){};
            destroy-method 方法原型: public void 自定义方法名(){};
        -->
        <bean id="doSomeService"  class="com.cn.service.DoSomeThingsService" destroy-method="destroyA" 
            init-method="initA" scope="prototype">
            <property name="dao" ref="doSomeDao" />
        </bean>
        
    </beans>

    上述例子说明了:

      1.单例模式singleton的类,都是在容器初始化的时候创建的。原型模式,是在 getBean的时候创建的。

      2.单例模式在容器中,始终只有一个。原型模式每次getBean新建一个。

      3.bean的初始化方法,在创建对象的时候,执行完构造函数之后执行。

    修改配置文件及测试方法如下:

        <bean id="doSomeService"  class="com.cn.service.DoSomeThingsService" destroy-method="destroyA" 
            init-method="initA">
            <property name="dao" ref="doSomeDao" />
        </bean>
    
    
        public static void main(String[] args) {
            String resource = "applicationContext.xml";
            ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
            
            DoSomeThingsService service=  (DoSomeThingsService) ac.getBean("doSomeService");
            ClassPathXmlApplicationContext cpac = (ClassPathXmlApplicationContext)ac;
            cpac.close();
            
        }

    dao construction
    DoSomeThingsService construction
    DoSomeThingsService init method
    七月 21, 2019 8:40:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Sun Jul 21 20:40:24 CST 2019]; root of context hierarchy
    DoSomeThingsService destroy method

     

     说明:只有单例模式的对象,在容器关闭的时候会执行销毁方法。

    类型注入的例子

    给DoSomeThingsService 增加int的属性
    
    private int intValue;
    
    增加get,set方法。
    
    配置文件 修改如下
    
        <bean id="doSomeService"  class="com.cn.service.DoSomeThingsService" destroy-method="destroyA" 
            init-method="initA">
            <property name="dao" ref="doSomeDao" />
            <property name="intValue" value="5" />
        </bean>
    
    
    测试代码:
    
        public static void main(String[] args) {
            String resource = "applicationContext.xml";
            ApplicationContext ac = new ClassPathXmlApplicationContext(resource);
            
            DoSomeThingsService service=  (DoSomeThingsService) ac.getBean("doSomeService");
            System.out.println(service);
            
        }
    
    
    
    输出:
    
    DoSomeThingsService init method
    DoSomeThingsService [intValue=5]

    引用类型还可用如下方式注入

        <bean id="doSomeService"  class="com.cn.service.DoSomeThingsService" destroy-method="destroyA" 
            init-method="initA">
            <!-- <property name="dao" ref="doSomeDao" /> -->
            <property name="dao">
                <ref bean="doSomeDao"/>
            </property>
            <property name="intValue" value="5" />
        </bean>
  • 相关阅读:
    poj 2418 Hardwood Species
    hdu 3791 二叉搜索树
    九度oj 1544 数字序列区间最小值
    九度oj 1525 子串逆序打印
    九度oj 1530 最长不重复子串
    九度oj 1523 从上往下打印二叉树
    P1190 接水问题
    P1179 数字统计
    P1083 借教室
    P1079 Vigenère 密码
  • 原文地址:https://www.cnblogs.com/llq1214/p/11165682.html
Copyright © 2011-2022 走看看