Spring 4.0 学习日记(2) --IOC 创建对象方式小记

来源:互联网 发布:怎样安装ubuntu双系统 编辑:程序博客网 时间:2024/06/05 06:09

对于IOC创建对象的时候一般有三种模式
1.无参的构造方法 其实就用默认构造方法
2.有参数的构造方法
3.工厂模式 分为静态和动态两种模式


无参的构造方法

被测试类

package com.wow.saber.Student;public class Student {    public Student (){        System.out.println("无参构造方法run......");    }    private String name;    public void setName(String name) {        this.name = name;    }    public void printInfo(){        System.out.println("学生信息打印......"+name);    }}

测试类

package com.wow.saber.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wow.saber.Student.Student;public class Test {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        Student stu = (Student)context.getBean("stu");        stu.printInfo();    }}

beans.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"         xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id = "stu" class = "com.wow.saber.Student.Student">        <property name="name" value = "TestInfoRun"></property>    </bean></beans>  

测试结果

无参构造方法run......学生信息打印......TestInfoRun

有参构造函数方法创建对象

对Student类做下修改

package com.wow.saber.Student;public class Student {    /*public Student (){        System.out.println("无参构造方法run......");    }    */    public Student (String name){        super();        this.name = name;    }    private String name;    /*public void setName(String name) {        this.name = name;    }*/    public void printInfo(){        System.out.println("学生信息打印......"+name);    }}

测试类不变

package com.wow.saber.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wow.saber.Student.Student;public class Test {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        Student stu = (Student)context.getBean("stu");        stu.printInfo();    }}

beans.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"    xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- <bean id = "stu" class = "com.wow.saber.Student.Student"> <property         name="name" value = "TestInfoRun"></property> </bean> -->    <bean id="stu" class="com.wow.saber.Student.Student">        <!-- index 构造函数参数下标 从0 开始 -->        <constructor-arg index="0" value="TestInfo1Run"></constructor-arg>    </bean>    <!-- <bean id = "stu" class = "com.wow.saber.Student.Student"> -->    <!-- name 对应的是构造函数的参数名(必须一样) -->    <!-- <constructor-arg name="name" value = "TestInfo2Run"></constructor-arg>         </bean> -->    <!-- <bean id = "stu" class = "com.wow.saber.Student.Student"> -->    <!--type 构造函数参数的类型 -->    <!-- <constructor-arg type="java.lang.String" value = "TestInfo3Run"></constructor-arg>         </bean> --></beans>  

打印结果

学生信息打印......TestInfo1Run学生信息打印......TestInfo2Run学生信息打印......TestInfo2Run

工厂方法创建对象

1.静态工厂方法创建 Bean

调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节.

要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名称. 最后, 使用 元素为该方法传递方法参数.


student类

package com.wow.saber.Student;public class Student {    /*public Student (){        System.out.println("无参构造方法run......");    }    */    public Student (String name){        super();        this.name = name;    }    private String name;    /*public void setName(String name) {        this.name = name;    }*/    public void printInfo(){        System.out.println("学生信息打印......"+name);    }}

工厂类

package com.wow.saber.FactoryStatic;import com.wow.saber.Student.Student;public class FactoryStatic {    public static Student getStu (String name){        return new Student(name);    }}

测试类

package com.wow.saber.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wow.saber.Student.Student;public class Test {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        Student stu = (Student)context.getBean("stuFactory");        stu.printInfo();    }}

beans.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"    xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- 通过静态工厂方法来配置bean:注意不是配置静态工厂的实例,而是配置bean实例 -->    <!--      class属性:指向静态工厂长方法的全类名     factory-method 属性:指向静态工厂方法的名字     constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg类配置参数      -->    <bean id="stuFactory" class="com.wow.saber.FactoryStatic.FactoryStatic"        factory-method="getStu">        <constructor-arg name="name" value="stuFactoryInfoRun"></constructor-arg>    </bean></beans>  

测试结果

学生信息打印......stuFactoryInfoRun

2.实例(动态 )工厂方法创建 Bean

实例工厂方法: 将对象的创建过程封装到另外一个对象实例的方法里. 当客户端需要请求对象时, 只需要简单的调用该实例方法而不需要关心对象的创建细节.

要声明通过实例工厂方法创建的 Bean

在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean在 factory-method 属性里指定该工厂方法的名称使用 construtor-arg 元素为工厂方法传递方法参数

原来的静态的Factory类

package com.wow.saber.FactoryStatic;import com.wow.saber.Student.Student;public class FactoryStatic {    public static Student getStu (String name){        return new Student(name);    }}

动态工厂类

package com.wow.saber.FactoryDynamic;import com.wow.saber.Student.Student;public class FactoryDynamic {    public  Student getStu (String name){        return new Student(name);    }}

Student类

package com.wow.saber.Student;public class Student {    /*public Student (){        System.out.println("无参构造方法run......");    }    */    public Student (String name){        super();        this.name = name;    }    private String name;    /*public void setName(String name) {        this.name = name;    }*/    public void printInfo(){        System.out.println("学生信息打印......"+name);    }}

测试类

package com.wow.saber.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wow.saber.Student.Student;public class Test {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        Student stu = (Student)context.getBean("stuFactory");        stu.printInfo();    }}

beans.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"    xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans.xsd">    <!-- 通过实体工厂的方法来配置bean (需要工厂单独配置一个实例) -->    <!-- 配置工厂的实例 -->    <bean id = "stuFactoryDynamic" class = "com.wow.saber.FactoryDynamic.FactoryDynamic"></bean>    <!-- 通过工厂实例方法来配置bean -->    <!--      factory-bean属性:指向实例工厂 方法的全类名     factory-method属性:指向实例工厂方法的名字      constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg类配置参数     -->     //注意测试类中的Student stu = (Student)context.getBean("stuFactory");调用的     //是这里的id 需要哪个实例化便调用哪个id    <bean id = "stuFactory" factory-bean = "stuFactoryDynamic" factory-method="getStu">        <constructor-arg name = "name" value = "stuFactoryDynamicInfoRun"></constructor-arg>    </bean></beans>  

打印结果

学生信息打印......stuFactoryDynamicInfoRun

以上

其实 说话 现在我也晕晕的…….

阅读全文
0 0
原创粉丝点击