Spring 学习笔记二

三种实例化bean的方式

1.使用类构造器实例化
           <!-- The value of 'id' can't be set to special characters, whereas 'name' can be -->
           <bean id="personService" class="cn.serenest.spring.service.impl.PersonServiceImpl"></bean>

2.使用静态工厂方法实例化
<bean id="personService2" class="cn.serenest.spring.service.impl.PersonServiceImplFactory" factory-method="CreatePersonService"></bean>

3.使用实例工厂方法实例化:
           <bean id="personServiceInstanceFactory" class="cn.serenest.spring.service.impl.PersonServiceImplFactory"></bean>
  		   <bean id="personServiceByInstanceFactory" factory-bean="personServiceInstanceFactory" factory-method="CreatePersonServiceByInstanceFactory"></bean>	



工厂类:

public class PersonServiceImplFactory {
	public static PersonServiceImpl CreatePersonServiceByStaticFactory() {
		return new PersonServiceImpl();
	}
	
	public PersonServiceImpl CreatePersonServiceByInstanceFactory() {
		return new PersonServiceImpl();
	}
}

你可能感兴趣的:(java,spring,bean)