Spring 创建对象的三种方式
1. 通过构造方法创建
1.1 无参构造创建:默认情况.
1.2 有参构造创建:需要明确配置
1.2.1 需要在类中提供有参构造方法
package com.bjsxt.pojo;
public class People {
private int id;
private String name;
/**
*注意这里一旦使用了有参的构造器之后就必须生成这个
* 无参的构造器不然spring会报错No matching constructor found in class 'People'
*/
public People() {
}
public People(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
}
1.2.2 在 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">
<!-- 使用无参构造器注入对象-->
<bean id="peo" class="com.bjsxt.pojo.People" />
<!--使用有参构造器注入对象 需要set方法和有参构造器支持-->
<bean id="peo2" class="com.bjsxt.pojo.People">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="zhangsan"></constructor-arg>
</bean>
</beans>
1.2.2.1 如果设定的条件匹配多个构造方法执行最后的构造方法
1.2.2.2 index : 参数的索引,从 0 开始
1.2.2.3 name: 参数名
1.2.2.4 type:类型(区分开关键字和封装类 int 和 Integer)
1.2.3测试类
package com.bjsxt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
People peo = ac.getBean("peo",People.class);
System.out.println(peo);
People peo2 = ac.getBean("peo2",People.class);
System.out.println(peo2);
}
}
1.2.4运行结果
二月 27, 2019 10:59:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 22:59:41 CST 2019]; root of context hierarchy
二月 27, 2019 10:59:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People [id=0, name=null]
People [id=1, name=zhangsan]
2. 实例工厂
2.1 工厂设计模式:帮助创建类对象.一个工厂可以生产多个对象.
2.2 实例工厂:需要先创建工厂,才能生产对象
2.3 实现步骤:
2.3.1 必须要有一个实例工厂
package com.bjsxt.pojo;
public class People {
private int id;
private String name;
/**
*注意这里一旦使用了有参的构造器之后就必须生成这个
* 无参的构造器不然spring会报错No matching constructor found in class 'People'
*/
public People() {
}
public People(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
}
package com.bjsxt.pojo;
public class PeopleFactory {
/**
* 注意这里千万不能定义为stataic的
* @return
*/
public People newInstance(){
return new People(1,"测试");
}
}
2.3.2 在 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">
<!--<!– 使用无参构造器注入对象–>
<bean id="peo" class="com.bjsxt.pojo.People" />
<!–使用有参构造器注入对象 需要set方法和有参构造器支持–>
<bean id="peo2" class="com.bjsxt.pojo.People">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="zhangsan"></constructor-arg>
</bean>-->
<bean id="factory" class="com.bjsxt.pojo.PeopleFactory"></bean>
<bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean>
</beans>
2.3.3 测试类
package com.bjsxt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// People peo = ac.getBean("peo",People.class);
// System.out.println(peo);
//
// People peo2 = ac.getBean("peo2",People.class);
// System.out.println(peo2);
People peo = ac.getBean("peo3",People.class);
System.out.println(peo);
}
}
2.3.4 测试结果
二月 27, 2019 11:12:24 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 23:12:24 CST 2019]; root of context hierarchy
二月 27, 2019 11:12:24 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People [id=1, name=测试]
3. 静态工厂
3.1 不需要创建工厂,快速创建对象.
3.2 实现步骤
3.2.1 编写一个静态工厂(在方法上添加 static)
package com.bjsxt.pojo;
public class PeopleFactory1 {
/**
* 注意这里静态工厂需要定义为statis的
* @return
*/
public static People newInstance(){
return new People(1,"测试");
}
}
package com.bjsxt.pojo;
public class People {
private int id;
private String name;
/**
*注意这里一旦使用了有参的构造器之后就必须生成这个
* 无参的构造器不然spring会报错No matching constructor found in class 'People'
*/
public People() {
}
public People(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + "]";
}
}
3.2.2 在 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">
<!--<!– 使用无参构造器注入对象–>
<bean id="peo" class="com.bjsxt.pojo.People" />
<!–使用有参构造器注入对象 需要set方法和有参构造器支持–>
<bean id="peo2" class="com.bjsxt.pojo.People">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="zhangsan"></constructor-arg>
</bean>-->
<!--<bean id="factory" class="com.bjsxt.pojo.PeopleFactory"></bean>-->
<!--<bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean>-->
<!--静态工厂注入-->
<bean id="peo4" class="com.bjsxt.pojo.PeopleFactory1" factory-method="newInstance" />
</beans>
3.2.3 测试类
package com.bjsxt.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.bjsxt.pojo.People;
import com.bjsxt.pojo.PeopleFactory;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// People peo = ac.getBean("peo",People.class);
// System.out.println(peo);
//
// People peo2 = ac.getBean("peo2",People.class);
// System.out.println(peo2);
// People peo3 = ac.getBean("peo3",People.class);
// System.out.println(peo3);
People peo4 = ac.getBean("peo4",People.class);
System.out.println(peo4);
}
}
3.2.4运行结果
二月 27, 2019 11:16:54 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@179d3b25: startup date [Wed Feb 27 23:16:54 CST 2019]; root of context hierarchy
二月 27, 2019 11:16:55 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
People [id=1, name=测试]