本文内容纲要:
- 一、通过构造函数创建对象。
- 2.1 利用无参构造函数+setter方法注入值
- 2.2 利用有参构造函数直接注入
- 二、通过静态工厂方式创建对象。
- 三、通过实例工厂方式创建对象。
关于Spring的搭建可参见:浅析Spring框架的搭建. 在测试之前还是应该先将环境配置好,将相关Jar包导进来。Spring创建的对象,默认情况下都是单例模式,除非通过scope指定。
向IOC容器中注入对象,通过配置XML文件的
一、通过构造函数创建对象。
2.1 利用无参构造函数+setter方法注入值
最基本的对象创建方式,只需要有一个无参构造函数(类中没有写任何的构造函数,默认就是有一个构造函数,如果写了任何一个构造函数,默认的无参构造函数就不会自动创建哦!!)和字段的setter方法。
Person类:
package com.mc.base.learn.spring.bean;
public class Person {
private String name; private Integer id; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "Person [name=" + name + ", id=" + id + "]"; } }
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 class="com.mc.base.learn.spring.bean.Person" id="person"> <property name="name" value="LiuChunfu"></property> <property name="id" value="125"></property> </bean> </beans>
其本质为:
SpringContext利用无参的构造函数创建一个对象,然后利用setter方法赋值。所以如果无参构造函数不存在,Spring上下文创建对象的时候便会报错。
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [applicationContext.xml]: Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mc.base.learn.spring.bean.Person]: No default constructor found;
nested exception is java.lang.NoSuchMethodException: com.mc.base.learn.spring.bean.Person.<init>() at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105) 。。。。。
2.2 利用有参构造函数直接注入
Person类:
package com.mc.base.learn.spring.bean;
public class Person {
private String name; private Integer id; public Person(String name, Integer id) { super(); this.name = name; this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Override public String toString() { return "Person [name=" + name + ", id=" + id + "]"; } }
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 class="com.mc.base.learn.spring.bean.Person" id="person"> <constructor-arg name="id" value="123"></constructor-arg> <constructor-arg name="name" value="LiuChunfu"></constructor-arg> </bean> </beans>
二、通过静态工厂方式创建对象。
静态工厂的对象,在容器加载的时候就被创建了。
package com.mc.base.learn.spring.factory;
import com.mc.base.learn.spring.bean.Person;
public class PersonStaticFactory { public static Person createPerson(){ return new Person(); } /** * 工厂方法带有参数如何处理? * @Title: createPerson * @Description: TODO(这里用一句话描述这个方法的作用) * @param @param id * @param @param name * @param @return * @return Person 返回类型 * @throws */ public static Person createPerson(Integer id,String name){ return new Person(name,id); } }
<!--静态的工厂方法核心是class+factory-method --> <bean id="person" class="com.mc.base.learn.spring.factory.PersonStaticFactory" factory-method="createPerson"> <!--通过property方法向createPerson传递参数 --> <property name="name" value="LiuChunfu"></property> <property name="id" value="125"></property> </bean>
测试如下:
@Test
public void testName() throws Exception {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ac.getBean("person3", Person.class);
System.out.println(person);//Person [name=LiuChunfu, id=125]
}
三、通过实例工厂方式创建对象。
实例工厂,就是通过实例来调用对象,但是所得到的对象最终也是单利模式。实例工厂和静态工厂创建的对象都是单例模式,两者的区别就是创建对象的实际不同,静态工厂是在创建容器的时候就创建了,实例工厂是在调用方法的时候才创建。知道Java设计模式中的单例模式设计(饿汉式和懒汉式)的读者,对这里的静态工厂模式和实例工厂模式肯定有所体会。
package com.mc.base.learn.spring.factory;
import com.mc.base.learn.spring.bean.Person;
public class PersonFactory { public Person createInstance() { return new Person(); } }
<bean id="personFactory" class="cn.test.util.PresonFactoryInstance"></bean>
<bean id="person4" factory-bean="personFactory" factory-method="createPerson">
<property name="name" value="LiuChunfu"></property>
<property name="id" value="125"></property>
</bean>
@Test
public void testName() throws Exception {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=ac.getBean("person4",Person.class);
System.out.println(person);//Person [name=LiuChunfu, id=125]
}
当然上面的创建对象传递参数,除了能够在创建对象的时候通过传入默认的参数,也可以在后面通过setter方法进行传参。
本Blog传递的参数只是简单的几种,关于向IOC容器中传入参数更详细请参见Sprinng之依赖注入传递参数的方式
原文连接:Spring创建对象的三种方式
本文内容总结:一、通过构造函数创建对象。,2.1 利用无参构造函数+setter方法注入值,2.2 利用有参构造函数直接注入,二、通过静态工厂方式创建对象。,三、通过实例工厂方式创建对象。,
原文链接:https://www.cnblogs.com/HDK2016/p/7146705.html