zoukankan      html  css  js  c++  java
  • Spring day01

    1 实例化Spring容器

    新建springday01项目
    1.F盘jar/Spring/first/五个jar包拷贝到lib下,复制xml文件到项目first包下
    2.First.java测试如何启动容器

    First.java:

    public static void main(String[] args) {
    //因为配置文件放在src下的first包下(文件路径可以随便,但是代码中也要改变)
    String cfg = "first/applicationContext.xml";
    ApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
    System.out.println(ctx);
    }

    注:参考jar文件和xml文件在F:CodejarSpringfirst文件夹下。

    2 使用Spring容器创建bean

    1.复制xml文件到container/instance下,分别使用构造器(重点掌握)、静态工厂(了解)、实例工厂(了解)实例化bean
    2.ExampleBean必须实现序列化接口,以及无参构造器
    3.分别进行测试

    代码如下:

    xml代码:

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- id要唯一,类名要写完整 -->
    <bean id="eb1" class="container.instance.ExampleBean"/>
    <!-- 虽然这个类我们自己没写,但是它一定有一个无参构造器 -->
    <bean id="cal1" class="java.util.GregorianCalendar"/>
    <!-- 此方法做了解,Calendar是一个抽象类,它没有无参构造器,第三句代表调用容器的静态方法 -->
    <!-- 静态工厂方式创建一个对象,其中,factory-method属性用来指定静态方法名 -->
    <bean id="cal2" class="java.util.Calendar"
    factory-method="getInstance"/>
    <!-- 使用实例工厂方式创建一个对象,第二个参数为别的bean的id,第三个参数表示
    调用第二个bean的getTime方法 -->
    <bean id="date1" factory-bean="cal1"
    factory-method="getTime"/>
    </beans>

    ExampleBean.class:

    //演示容器如何创建序列化对象
    public class ExampleBean implements Serializable {

    public ExampleBean(){
    System.out.println("ExampleBean的无参构造器");
    }
    }

    TestCase.class:

    package container.instance;


    import java.util.Calendar;
    import java.util.Date;

    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /*测试类*/
    public class TestCase {

    @Test
    public void test1(){
    //启动容器
    String cfg = "container/instance/applicationContext.xml";
    ApplicationContext ac = new ClassPathXmlApplicationContext(cfg);
    //让容器创建一个对象,如果不写第二个参数,则返回Object,所以,我们加上第二个参数,即这个
    //Bean所属的类
    ExampleBean eb = ac.getBean("eb1",ExampleBean.class);
    System.out.println(eb);

    Calendar cal1 = ac.getBean("cal1", Calendar.class);
    System.out.println(cal1);

    Calendar cal2 = ac.getBean("cal2",Calendar.class);
    System.out.println(cal2);

    Date date1 = ac.getBean("date1",Date.class);
    System.out.println(date1);
     }
    }

    运行结果:

    3 bean的作用域

    1.复制xml文件到container/other下,使用构造器实例化bean
    2.MessageBean实现序列化接口,无参构造器,初始化函数,销毁函数,调用函数
    3.test1:
    测试验证容器创建的bean默认是单例,即不管实例化多少次,都是同一个,但若xml中scope为prototype,则为多例
    test2:
    xml中配置init-method,创建对象时,会调用构造函数以及初始化函数
    配置destroy-method,close容器时,会调用销毁函数,容器关闭只在单例singleton模式下进行
    test3:
    测试即使不创建bean,容器也会实现预创建好,如果想不提前创建bean对象,则lazy-init=“true”延迟实例化
    4.stu包为类似的作业,练习bean生命周期

    xml文件代码:

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    <!-- prototype:原型,其作用是让容器创建多个实例,
    而singleton只创建一个实例 scope="prototype"-->

    <bean id="mb1" class="container.other.MessageBean"
    />




    <!-- init-method属性:指定初始化方法,注意,销毁方法只针对单例,此处若
    scope为原型,则销毁方法不执行 -->

    <bean id="mb2"
    class="container.other.MessageBean"
    init-method="init"
    destroy-method="destroy"
    />


    <!--lazy-init属性如果为true,表示延迟实例化 lazy-init="true"-->
    <bean id="mb3"
    class="container.other.MessageBean"
    init-method="init"
    destroy-method="destroy"
    />


    </beans>

     MessageBean.class代码:

    package container.other;

    import java.io.Serializable;

    public class MessageBean implements Serializable{

    public MessageBean(){
    System.out.println("MessageBean的无参构造器");
    }
    //初始化方法
    public void init(){
    System.out.println("初始化...");
    }

    public void sendMsg(){
    System.out.println("发送消息...");
    }
    //销毁方法
    public void destroy(){
    System.out.println("销毁资源");
    }
    }

    TestCase代码:

    package container.other;

    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class TestCase {

    @Test
    public void test1(){
    String cfg = "container/other/applicationContext.xml";
    ApplicationContext ac =
    new ClassPathXmlApplicationContext(cfg);
    MessageBean mb1 = ac.getBean("mb1",MessageBean.class);
    MessageBean mb2 = ac.getBean("mb1",MessageBean.class);
    System.out.println(mb1==mb2);
    //true 说明多次实例化,但实际值实例话一次,修改scope为prototype,则
    //每getBean一次,则实例化一次,所以为false
    }

    @Test
    public void test2(){
    String cfg = "container/other/applicationContext.xml";
    //ApplicationContext ac =
    //关闭容易应该使用AbstractApplicationContext
    AbstractApplicationContext ac =
    new ClassPathXmlApplicationContext(cfg);
    MessageBean mb2 = ac.getBean("mb2",MessageBean.class);
    mb2.sendMsg();
    //关闭容器
    ac.close();
    }

    @Test
    public void test3(){
    String cfg = "container/other/applicationContext.xml";
    ApplicationContext ac =
    new ClassPathXmlApplicationContext(cfg);
    //此处即使没有getBean,也会实例化,因此使用lazy-init会延迟实例化。
    MessageBean mb3 = ac.getBean("mb3",MessageBean.class);
    }
    }

    测试结果:

    test1:

    test2:

    test3:

     对于init-lazy此处显示结果不是很正确,具体参考tmooc文档说明。

    5 Setter注入

    1.复制xml文件到包setter下
    2.Computer实现序列化接口,构造函数以及set/get方法
    3.xml中使用setter注入声明bean
    4.测试,输出bean属性
    注意,此处只涉及了一个bean,下面的一个例子涉及到三个bean

    Computer.class:

    package setter;

    import java.io.Serializable;

    public class Computer implements Serializable {
    private String mainboard;
    private String hdd;
    private String ram;


    public Computer() {
    System.out.println("Computer()........");
    }
    public String getMainboard() {
    return mainboard;
    }
    public void setMainboard(String mainboard) {
    this.mainboard = mainboard;
    }
    public String getHdd() {
    return hdd;
    }
    public void setHdd(String hdd) {
    this.hdd = hdd;
    }
    public String getRam() {
    return ram;
    }
    public void setRam(String ram) {
    this.ram = ram;
    }

    }

    在applicationContext.xml中声明这个bean,追加代码如下:

    <bean id="computer" class="setter.Computer">
    <property name="mainboard" value="技嘉"></property>
    <property name="hdd" value="希捷"></property>
    <property name="ram" value="金士顿"></property>
    </bean>

    TestCase 测试代码:

    package setter;

    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class TestCase {

    @Test
    public void test1(){
    String cfg = "setter/applicationContext.xml";
    ApplicationContext ac =
    new ClassPathXmlApplicationContext(cfg);
    Computer computer = ac.getBean("computer",Computer.class);
    System.out.println(computer.getMainboard());
    System.out.println(computer.getHdd());
    System.out.println(computer.getRam());
    }
    }

    运行结果:

    6 构造器注入

    步骤一:创建bean

    创建一个手机类MobilePhone,代码如下:

     
    1. package com.tarena.bean;
    2. import java.io.Serializable;
    3. public class MobilePhone implements Serializable {
    4.     private String cpu;
    5.     private String ram;
    6.     public MobilePhone(String cpu, String ram) {
    7.         this.cpu = cpu;
    8.         this.ram = ram;
    9.     }
    10.     public String getCpu() {
    11.         return cpu;
    12.     }
    13.     public void setCpu(String cpu) {
    14.         this.cpu = cpu;
    15.     }
    16.     public String getRam() {
    17.         return ram;
    18.     }
    19.     public void setRam(String ram) {
    20.         this.ram = ram;
    21.     }
    22. }

    步骤二:声明bean

    在applicationContext.xml中声明这个bean,追加代码如下:

     
    1.     <!--构造器注入 -->
    2.     <bean id="phone" class="com.tarena.bean.MobilePhone">
    3.         <constructor-arg index="0" value="ARM"/>
    4.         <constructor-arg index="1" value="2G"/>
    5.     </bean>

    步骤三:写测试代码

    在TestCase中增加测试方法test6,追加代码如下:

     
    1.     /**
    2.      * 构造器注入
    3.      */
    4.     @Test
    5.     public void test6() throws SQLException {
    6.         String cfg = "applicationContext.xml";
    7.         AbstractApplicationContext ctx =
    8.             new ClassPathXmlApplicationContext(cfg);
    9.         MobilePhone phone =
    10.             ctx.getBean("phone", MobilePhone.class);
    11.         System.out.println(phone.getCpu());
    12.         System.out.println(phone.getRam());
    13.     }

    步骤四:执行测试

    执行测试方法test6,效果如下图:

  • 相关阅读:
    Neko's loop HDU-6444(网络赛1007)
    Parameters
    SETLOCAL
    RD / RMDIR Command
    devenv 命令用法
    Cannot determine the location of the VS Common Tools folder.
    'DEVENV' is not recognized as an internal or external command,
    How to change Visual Studio default environment setting
    error signing assembly unknown error
    What is the Xcopy Command?:
  • 原文地址:https://www.cnblogs.com/lchzls/p/5769447.html
Copyright © 2011-2022 走看看