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();
}
}
上一篇: 联合权值(树上小操作)