小言_互联网的博客

Spring框架|创建Bean对象的三种方式

254人阅读  评论(0)


一、无参构造函数(常用)

在spring的配置文件中使用bean标签,配以id和class属性,且没有其他属性和标签时。采用的就是默认构造函数创建bean对象。此时如果类中没有默认构造函数,则对象无法创建。

	<bean id="p" class="com.gql.entity.Person"></bean>

二、静态工厂

	<bean id="factory" class="com.gql.entity.BeanFactory" factory-method="getInstance"></bean>

使用某个类中的静态方法创建对象,并存入spring容器。

package com.gql.entity;
/**
 * 
 * 类说明:
 *		模拟静态工厂
 * @guoqianliang1998.
 */
public class BeanFactory {
	public static Person getInstance(){
		return new Person();
	}
}

三、实例工厂(鸡肋)

	<bean id="factory" class="com.gql.entity.BeanFactory"></bean>
	<bean id="f" factory-bean="factory" factory-method="getInstance"></bean>

使用某个类中的方法创建对象,并存入spring容器。

package com.gql.entity;
/**
 * 
 * 类说明:
 *		模拟工厂实例化
 * @guoqianliang1998.
 */
public class BeanFactory {
	public Person getInstance(){
		return new Person();
	}
}

测试类

package com.gql.entity;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * 类说明:
 *		三种创建Bean对象的方式
 *		1.无参构造函数创建
 *		2.静态工厂
 *		3.实例工厂
 * @guoqianliang1998.
 */
public class Demo {
	//1.无参构造函数创建
	@Test
	public void testDemo1() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p = (Person) ac.getBean("p");
		System.out.println(p);
	}
	
	//2.静态工厂
	@Test
	public void testDemo2(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p = (Person) ac.getBean("factory");
		System.out.println(p);
	}
	
	//3.实例化工厂
	@Test
	public void testDemo3(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		BeanFactory factory = (BeanFactory)ac.getBean("factory");
		System.out.println(factory);
		Person p = (Person) ac.getBean("f");
		System.out.println(p);
	}
}

实体类

package com.gql.entity;
/**
 * 类说明:
 *		实体类Person
 * @guoqianliang1998.
 */
public class Person {
	private String name;

	//无参构造
	public Person() {

	}

	//有参构造
	public Person(String name) {
		this.name = name;
	}
	public void talk(){
		System.out.println("talk方法...");
	}
}

XML配置

applicationContext_Person.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">
	<!-- 1.无参构造函数 -->
	<bean id="p" class="com.gql.entity.Person"></bean> 
	<!-- 2.静态工厂 -->
	 <bean id="factory" class="com.gql.entity.BeanFactory" factory-method="getInstance"></bean>
	<!-- 3.实例化工厂 -->
	<bean id="factory" class="com.gql.entity.BeanFactory"></bean>
	<bean id="f" factory-bean="factory" factory-method="getInstance"></bean>
</beans>

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">

	<import resource="com/gql/entity/applicationContext_Person.xml"/>
</beans>

转载:https://blog.csdn.net/weixin_43691058/article/details/104083788
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场