Spring 创建对象的问题,不同构造方法创建对象,使用工厂类类获取对象、单例或多例、延迟创建问题
2016-08-28 23:36
423 查看
创建实体类Student
再创建工厂类,
配置Spring的xml,applicationContext.xml
最后分别测试
package star.july.b_ioc; public class Student { private String name; private int age; public Student() { System.out.println("执行了Student构造方法"); } public Student(String name, int age) { System.out.println("调用了有参构造方法"); this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
再创建工厂类,
package star.july.b_ioc; public class StudentFactory { public StudentFactory() { // new Student(); System.out.println("调用StudentFactory构造方法"); } public boolean setHaha(){ System.out.println("哈哈"); return true; } public Student getStudent(){ System.out.println("进入了StudentFactory的getStudent()方法"); return new Student(); } public static Student getStu(){ System.out.println("调用静态方法"); return new Student(); } }
配置Spring的xml,applicationContext.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" default-lazy-init="true" > <!-- <beanms default-lazy-init="true"/> --> <!-- 配置一个javabean id:对象的唯一标记 class:需要创建的对象 --> <!-- 使用无参构造方法 --> <!-- <bean id = "students" class="star.july.b_ioc.Student"> </bean> --> <!-- 有参方法 index:构造方法的参数索引,从0开始 value:需要传递的参数值 type:参数类型 --> <!-- <bean id ="students" class="star.july.b_ioc.Student"> <constructor-arg index="0" value="小火" type="String" > </constructor-arg> <constructor-arg index="1" value="20" type="int" > </constructor-arg> </bean> --> <!-- 先创建工厂类的成员方法获取对象 --> <!-- 1、先创建工厂类对象 --> <bean id="factory" class="star.july.b_ioc.StudentFactory" > </bean> <!-- 2、调用工厂类的成员方法 factory-bean:使用哪个工厂类的对象 factory-method:使用工厂类的哪个方法 --> <bean id="invocation" factory-bean="factory" factory-method="setHaha" ></bean> <!-- 调用工厂类的静态方法 --> <bean id="invokeStatic" class="star.july.b_ioc.StudentFactory" factory-method="getStu" ></bean> <!-- 对象单例子 单例:singleton(默认) 多例:prototype --> <bean id="s1" class="star.july.b_ioc.StudentFactory" scope="prototype" ></bean> <!-- 对象的延迟创建问题 lazy-init:是否延迟加载(创建)对象 false:在初始化工厂的时候(调用getBean()方法 true:在调用getBean()的时候创建对象 注意:该方法只能用在单例子 --> <bean id="s" class="star.july.b_ioc.StudentFactory" scope="singleton" lazy-init="true" ></bean> </beans>
最后分别测试
package star.july.b_ioc; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("/star/july/b_ioc/applicationContext.xml"); // Student s = (Student)ac.getBean("students"); StudentFactory s = (StudentFactory) ac.getBean("s"); //调用静态方法 // StudentFactory s = (StudentFactory) ac.getBean("invokeStatic"); } }
相关文章推荐
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- C#--反射使用Start!-获取对象的字段/属性/方法/成员/构造方法
- 子类继承父类时,创建对象,构造方法的调用问题
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象
- 使用不同的方法来创建对象和生成原型链
- 不使用构造方法创建Java对象 Objenesis
- 使用代码创建的控件对象,设置的事件监听方法不执行的问题
- 不使用构造方法创建Java对象: objenesis的基本使用方法
- ndk多线程需要获取JNIEnv 或c通过类名+包名创建对象使用如下方法
- ArrayList构造方法详解-使用无参构造方法创建对象
- 通过反射可以获取Class的构造方法 通过构造方法创建对象
- Spring开发之静态工厂创建对象+动态工厂创建对象+构造方法创建对象+set方法注入+构造方法注入
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- 反射机制的概述和字节码对象的获取方式 JavaBean的概述&BeanUtils的使用 反射操作构造方法、成员方法、成员属性
- 不使用构造方法创建Java对象: objenesis的基本使用方法
- 百度 地图 slidingmenu 黑边 使用截图的方式解决黑边问题,步骤: 1.slidingMenu打开的时候调用BaiduMap的snapshot方法截图获取Bitmap对象; 2.使用
- JavaScript对象创建的几种方式及使用方法
- 关于使用CreateUserWinzard控件创建用户时出现连接数据库问题的解决方法
- 使用反射-动态创建对象及调用对象方法