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
- Spring 4.0 学习日记(2) --IOC 创建对象方式小记
- js对象的创建方式(小记)
- 重温spring框架1--ioc创建对象的方式
- spring IOC容器创建对象的三种方式
- Spring IOC创建对象的三种方式
- 【Spring】IOC容器--创建对象(六)
- Spring 4.0 学习日记(3)--- Spring依赖注入方式
- 【Spring】IOC容器创建对象
- Spring - 开始 , IOC创建对象,别名 和 创建对象的方式
- 三.Spring4学习-----IOC (控制反转)创建对象的三种方式
- Spring 4.0 学习日记(1) --IOC 控制反转概念解释
- Spring(IOC) 对象创建三种方式、对象bean起别名
- Spring IoC(2) --- 注解方式
- Spring IOC 创建bean实例的方式
- 使用ioc来创建对象有三种方式
- 第二讲 使用Spring IoC创建对象的3种方式
- spring容器IOC创建对象<二>
- Spring IOC 容器研究日记(一)
- Unity3D Entitas 谷歌的ECS Entity Component System入门学习2
- _String类
- 小公司程序员怎么进大公司--容易的路越走越难走
- Centos7提示" xxx 不在 sudoers 文件中。此事将被报告。"
- Kotlin-47.Kotlin调用JavaScript(Call JavaScript from Kotlin)
- Spring 4.0 学习日记(2) --IOC 创建对象方式小记
- 机器学习基石 4.5 作业一
- git--搭配git服务器
- sql
- Ajax技术--引入jQuery不起作用(细心)
- 网易云课堂JAVA进阶程序设计题一
- 并查集的扩展 poj 2492
- Codeforces55D_Beautiful numbers_记忆化搜索版数位DP
- cs231n-2017-assignments2-TensorFlow.ipynb 心得体会