欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring中实例化Bean的3种方式

程序员文章站 2022-03-03 11:33:54
...

实例化Bean的3种方式:

1.使用类构造器实例化:

<bean id="personServiceBean" class="com.spring.service.impl.PersonServiceBean"></bean>

参考Spring环境的搭建及示例文章

 

2.使用静态工厂方法实例化:

<bean id="peronServiceBeanFactory" class="com.spring.service.impl.PersonServiceBeanFactory"
		factory-method="pcreatePersonServiceBanFactory"></bean>

 PersonServiceBeanFactory代码如下:

package com.spring.service.impl;

public class PersonServiceBeanFactory {
	public static PersonServiceBean pcreatePersonServiceBanFactory() {
		return new PersonServiceBean(); 
	}
}

  

 3.使用示例工厂方法实例化:

	<bean id="personServiceBeanFactory" class="com.spring.service.impl.PersonServiceBeanFactory"></bean>
	<bean id="personService" factory-bean="personServiceBeanFactory"
		factory-method="createPersonServiceBanFactory"></bean>

 PersonServiceBeanFactory代码如下:

package com.spring.service.impl;

public class PersonServiceBeanFactory {	
	public PersonServiceBean createPersonServiceBanFactory() {
		return new PersonServiceBean(); 
	}
}

 

相关标签: Bean Spring