关于Spring的搭建可参见:浅析Spring框架的搭建. 在测试以前仍是应该先将环境配置好,将相关Jar包导进来。Spring建立的对象,默认状况下都是单例模式,除非经过scope指定。html
向IOC容器中注入对象,经过配置XML文件的<bean>节点来实现,<bean>中最主要的属性有两个,id和class,id表示标识这个<bean>节点,class表示关联的类文件名称(包名 + 类名)。<property>节点能够调用类中的setter方法,name对应参数名称,value对应传入的参数值。<constructor-arg>节点能够调用类中的构造器,name对应参数名称,value对应参数值。java
最基本的对象建立方式,只须要有一个无参构造函数(类中没有写任何的构造函数,默认就是有一个构造函数,若是写了任何一个构造函数,默认的无参构造函数就不会自动建立哦!!)和字段的setter方法。spring
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配置:app
<?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上下文建立对象的时候便会报错。ide
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) 。。。。。
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>
静态工厂的对象,在容器加载的时候就被建立了。this
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建立对象的三种方式